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

Lab - 5 - Inheritance

The document outlines the creation of two sets of classes: Shapes (Triangle and Circle) and Cities (Tabuk and Madinah). Each set includes a base class with common attributes and methods, and subclasses that inherit from the base class, overriding methods to provide specific functionality. The main methods in both sets demonstrate polymorphism by creating objects and calling overridden methods to display area calculations and location information.

Uploaded by

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

Lab - 5 - Inheritance

The document outlines the creation of two sets of classes: Shapes (Triangle and Circle) and Cities (Tabuk and Madinah). Each set includes a base class with common attributes and methods, and subclasses that inherit from the base class, overriding methods to provide specific functionality. The main methods in both sets demonstrate polymorphism by creating objects and calling overridden methods to display area calculations and location information.

Uploaded by

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

1) Shapes, Triangle and Circle:

A. Create a class named Shapes with the following attributes and methods:
1. A string attribute name
2. A constructor to initialize the attribute
3. A method named printArea() which prints the message “The formula
for area of shape”.
B. Create a class named Triangle which inherits from the Shapes class. The class
has the following attributes and methods:
1. A double attributes base and height.
2. A constructor to initialize the attributes
3. Override a method named printArea() which prints the area of
Triangle which calculates as "0.5 * base * height ".
C. Create a class named Circle which inherits from the Shapes class. The class has
the following attributes and methods:
1. A double attributes radius.
2. A constructor to initialize the attributes
3. Override a method named printArea() which prints the area of Circle
which calculates as "3.14 * radius * radius".
D. In the main method create three objects of type Shapes and call the method
printArea() polymorphically.

class Shapes {
String name;
public Shapes(String n){
name=n;
}
public void printArea() {
System.out.println("The formula for area");
}
}
class Triangle extends Shapes {
double base;
double height;
public Triangle(String n,double b, double h){
super(n);
base=b;
height=h;
}
public void printArea() {
double a=0.5*base*height;
System.out.println("Area of a triangle is: "+a);
}
}
class Circle extends Shapes {
double radius;
public Circle(String n,double r){
super(n);
radius=r;
}
public void printArea() {
double a=3.14 * radius * radius;
System.out.println("Area of a Circle is: "+a);
}
}
public class ShapesTest {
public static void main(String[] args) {
Shapes s1=new Shapes("General Shape");
Shapes s2=new Triangle("Triangle",1,2);
Shapes s3=new Circle("Circle",1);
s1.printArea();
s2.printArea();
s3.printArea();
}
}
2) Cities, Tabuk and Madinah:
A. Create a class named Cities which has the following attributes and methods:
1. A public string attribute location.
2. A constructor to initialize the attribute
3. A void method named print() which print the message "Location is:"
and the value of the attribute location
B. Create a class named Tabuk which inherits from the class cities and has the
following attributes and methods:
1. A public string attribute famousFor
2. A constructor to initialize the attributes
3. Override the method print() so it prints two message "Location is:"
and the value of the attribute location, and “Famous For:” and the
value of the attribute famousFor.
C. Create a class named Madinah which inherits from the class cities and has the
following attributes and methods:
1. A public string attribute famousFor
2. A constructor to initialize the attributes
3. Override the method print() so it prints two message "Location is:"
and the value of the attribute location, and “Famous For:” and the
value of the attribute famousFor.
D. Create an application with the main method and create 3 objects and call the
method print() polymorphically.

Answer:
class Cities{
public String location;
public Cities(String lo){
location=lo;
}
public void print(){
System.out.println("Location is:"+location );
}
}
class Tabuk extends Cities {
String famousOf;
public Tabuk(String lo, String f) {
super(lo);
famousOf=f;
}
public void print( ){
System.out.println("Location is: Tabuk");
System.out.println("Famous for: "+famousOf);
}
}
class Makkah extends Cities {
String famousOf;
public Makkah(String lo, String f) {
super(lo);
famousOf=f;
}
public void print( ){
System.out.println("Location is: Makkah");
System.out.println("Famous for: "+famousOf);
}
}
public class CitiesTest {
public static void main(String[] args) {
Cities obj1=new Cities("Saudi Arabia Cities");
Cities obj2=new Makkah("Makkah","Alkabah");
Cities obj3=new Tabuk("Tabuk","Madian Saleh");
obj1.print();
obj2.print();
obj3.print();
}
}

You might also like