0% found this document useful (0 votes)
24 views6 pages

Object Oriented Programing: Name System Id Instuctor Program Section

The document discusses object oriented programming concepts in Java. It includes: 1. A Java class called "Figure" that takes double data members for dimensions 1 and 2. It has a constructor to initialize the members and a method to calculate area. 2. Subclasses "Triangle" and "Rectangle" that extend the Figure class and override the area method to calculate areas for each shape. 3. A "FigureAreas" class with a main method that creates objects of the Triangle and Rectangle classes and calls their area methods to display the results.

Uploaded by

BIM 1234
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views6 pages

Object Oriented Programing: Name System Id Instuctor Program Section

The document discusses object oriented programming concepts in Java. It includes: 1. A Java class called "Figure" that takes double data members for dimensions 1 and 2. It has a constructor to initialize the members and a method to calculate area. 2. Subclasses "Triangle" and "Rectangle" that extend the Figure class and override the area method to calculate areas for each shape. 3. A "FigureAreas" class with a main method that creates objects of the Triangle and Rectangle classes and calls their area methods to display the results.

Uploaded by

BIM 1234
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

OBJECT ORIENTED PROGRAMING

NAME Saad Irfan

SYSTEM ID NUML S22 15306 (SP21289)

INSTUCTOR Mis. Iqra Shahzad

PROGRAM BSSE-31 (Morning)

SECTION II -A

Assignment #2
Q.1
Create a Java Class “Figure”. Class Figure takes two data members as
dim 1 and dim2 of double datatype. And a Constructor to initialize these
data members. Create a member function “Area ()” in figure class. Create
two sub classes Triangle and rectangle. Have the same members function
as Area (). That calculate the area for rectangle and triangle. Write
another class Figure Areas which test these classes by making objects and
display areas for different figures respectively.

PROGRAM
import java.util.Scanner;

public class figure {


double dim1;
double dim2;
figure(){
Scanner sc = new Scanner(System.in);
System.out.println("Enter the dimension-1:");
dim1 = sc.nextDouble();
System.out.println("Enter the dimension-2");
dim2 = sc.nextDouble();
}
void Area() {
System.out.println("The dim1 is:" +dim1);
System.out.println("The dim2 is:" +dim2);
}
}
class triangle extends circle{
double base;
double height;
double dim1=base;
double dim2=height;
double Area= (base*height)/2;
void Area() {
System.out.println("The area of triangle is :" +Area);
}
}
class rectangle extends circle{
double base;
double height;
double dim1=base;
double dim2=height;
double Area=base*height;
void Area() {System.out.println("The area of Rectangle is :" +Area);
}
}
class figureareas{
public static void main(String[] args) {
figure f;
f=new triangle();
f.Area();
f=new rectangle();
f.Area();
}
}
Output

Q.2
What do you understand the concept dynamic method
dispatch? Explain with example
Dynamic Method Dispatch
Dynamic method dispatch is the mechanism in which a call to an overridden method
is resolved at run time instead of compile time. This is an important concept because
of how Java implements run-time polymorphism.
Advantages of dynamic method dispatch
1. It allows Java to support overriding of methods, which are important for
runtime polymorphism.
2. It allows a class to define methods that will be shared by all its derived classes,
while also allowing these sub-classes to define their specific
implementation of a few or all of those methods.
3. It allows subclasses to incorporate their own methods and define their
implementation.
Example
class Phone{
public void showTime(){
System.out.println("Time is 8 am");
}
public void on(){
System.out.println("Turning on Phone...");
}
}
class SmartPhone extends Phone{
public void music(){
System.out.println("Playing music...");
}
public void on(){
System.out.println("Turning on SmartPhone...");
}
}
public class CWH {
public static void main(String[] args) {
Phone obj = new SmartPhone();

obj.showTime();
obj.on();
}
}
Output

You might also like