0% found this document useful (0 votes)
32 views12 pages

Java Multilevel Inheritance Example

1. The document describes a Java program that uses inheritance to calculate the areas of different shapes. 2. It defines a Figure class with dimensions, and Rectangle and Triangle classes that inherit from Figure and override the area() method to calculate areas using their specific formulas. 3. The main method creates objects for Rectangle and Triangle, calls their area() methods to output the calculated areas.

Uploaded by

Kavibharath R
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)
32 views12 pages

Java Multilevel Inheritance Example

1. The document describes a Java program that uses inheritance to calculate the areas of different shapes. 2. It defines a Figure class with dimensions, and Rectangle and Triangle classes that inherit from Figure and override the area() method to calculate areas using their specific formulas. 3. The main method creates objects for Rectangle and Triangle, calls their area() methods to output the calculated areas.

Uploaded by

Kavibharath R
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

Multilevel Inheritance

Aim:
To write a Java program to implement multilevel inheritance with real
time example.

Algorithm:
1. Declare the class named student.
2. The class should contain the name and roll no of the student, with suitable
method to get the name and roll number.
3. Declare the class named marks which inherits the class student.
4. The inherited class “mark” should contain the mark which is scored by the
student.
5. The class should include the suitable method to get the mark of the student.
6. Declare the class named sports which inherits the class mark.
7. The inherited class “sports” should contain the score which is scored by the
student.
8. In the main class, Create the object for the classes which is declared above.
9. Name the object as s1.
10. The details of the student is given in the main method using getData()
method.
11. The mark scored by the student is given using getMark() method.
12. The score of the student in sports is given using getScore() method.
13. Terminate the program and run the program the output will be displayed.
Program:
class Student
{
protected int regid;
protected String name;
void getdata(int r, String n)
{
regid=r;
name=n;
}
void putdata()
{
[Link]("The Regno is "+regid);
[Link]("The Name is "+name);
}
}
class Mark extends Student
{
protected int mark;
void getmark(int m)
{
mark=m;
}
void putmark()
{
[Link]("The Mark is is "+mark);
}
}

class Sports extends Mark


{
protected int score;
void getscore(int s)
{
score=s;
}
void putscore()
{
[Link]("The Score is "+score);
}
}
public class Main
{
public static void main(String[] args)
{
Sports s1=new Sports();
[Link](1, "Allen");
[Link]();
[Link](100);
[Link]();
[Link](8);
[Link]();
}

Output:

Result:
Thus, the Java program implement multiple inheritance was executed
successfully.
Abstract class

Aim:
To write a Java implement the concept of abstract class with real time
example.

Algorithm:
1. Declare the class named figure as an abstract type.
2. The abstract should contain the dimensions of the figure and dimensions are
mentioned as dim1 and dim2.
3. Initialize the method area() as an abstract.
4. The method area() can be accessed using “super” keyword in the inherited
classes.
5. Declare the class named rectangle which inherits the class figure.
6. Dimensions of the rectangle is given by the method area() which is declare in
the class figure.
7. The method area() is overrided in the class rectangle.
8. This method is used to calculate the area of the rectangle using the suitable
formula.
9. Declare the class named triangle which inherits the class figure.
10. Dimensions of the triangle is given by the method area() which is declare in
the class figure.
11. The method area() is overrided in the class triangle.
12. This method is used to calculate the area of the triangle using the suitable
formula.
13. In the main method create the objects for the classes rectangle and triangle.
14. The dimensions of the rectangle and triangle is given by calling the area()
method in the main function.
15. Terminate the program and run the program the output will be displayed.
Program:
abstract class Figure
{
double dim1;
double dim2;
Figure(double a, double b)
{
dim1 = a;
dim2 = b;
}
// area is now an abstract method
abstract double area();
}
class Rectangle extends Figure
{
Rectangle(double a, double b)
{
super(a, b);
}
// override area for rectangle
double area()
{
[Link]("Inside Area for Rectangle.");
return dim1 * dim2;
}
}
class Triangle extends Figure
{
Triangle(double a, double b)
{
super(a, b);
}
// override area for right triangle
double area()
{
[Link]("Inside Area for Triangle.");
return dim1 * dim2 / 2;
}
}
class AbstractAreas
{
public static void main(String args[])
{
// Figure f = new Figure(10, 10); // illegal now
Rectangle r = new Rectangle(9, 5);
Triangle t = new Triangle(10, 8);
Figure figref; // this is OK, no object is created
figref = r;
[Link]("Area is " + [Link]());
figref = t;
[Link]("Area is " + [Link]());
}
}
Output:

Result:
Thus, the Java program to implement the abstract class was executed
successfully.
Area

Aim:
To write a Java program to find the area of the given figures using
inheritance.

Algorithm:
1. Declare the class named figure.
2. The abstract should contain the dimensions of the figure and dimensions are
mentioned as dim1 and dim2.
3. Initialize the method area().
4. This method has no operations to find the area.
4. The method area() can be accessed using “super” keyword in the inherited
classes.
5. Declare the class named rectangle which inherits the class figure.
6. Dimensions of the rectangle is given by the method area() which is declare in
the class figure.
7. The method area() is overrided in the class rectangle.
8. This method is used to calculate the area of the rectangle using the suitable
formula.
9. Declare the class named triangle which inherits the class figure.
10. Dimensions of the triangle is given by the method area() which is declare in
the class figure.
11. The method area() is overrided in the class triangle.
12. This method is used to calculate the area of the triangle using the suitable
formula.
13. In the main method create the objects for the classes figure, rectangle and
triangle.
14. The dimensions of the figure, rectangle and triangle is given by calling the
area() method in the main function.
15. Terminate the program and run the program the output will be displayed.

Program:
class Figure
{
double dim1;
double dim2;
Figure(double a, double b)
{
dim1 = a;
dim2 = b;
}

double area()
{
[Link]("Area for Figure is undefined.");
return 0;
}
}
class Rectangle extends Figure
{
Rectangle(double a, double b)
{
super(a, b);
}
// override area for rectangle
double area()
{
[Link]("Inside Area for Rectangle.");
return dim1 * dim2;
}
}
class Triangle extends Figure
{
Triangle(double a, double b)
{
super(a, b);
}
// override area for right triangle
double area()
{
[Link]("Inside Area for Triangle.");
return dim1 * dim2 / 2;
}
}
class FindAreas
{
public static void main(String args[])
{
Figure f = new Figure(10, 10);
Rectangle r = new Rectangle(9, 5);
Triangle t = new Triangle(10, 8);
Figure figref;
figref = r;
[Link]("Area is " + [Link]());
figref = t;
[Link]("Area is " + [Link]());
figref = f;
[Link]("Area is " + [Link]());
}
}

Output:

Result:
Thus, the Java program to find the area of the given figures was
executed successfully.

Common questions

Powered by AI

The use of multilevel inheritance in the Java program follows the design principle of 'DRY' (Don't Repeat Yourself) by reusing code from the parent classes in subclasses. In the multilevel inheritance example, the 'Sports' class inherits from the 'Mark' class, which in turn inherits from the 'Student' class. This allows each subclass to build upon and extend the functionality provided by the parent class without duplicating code. Similarly, the use of an abstract class 'Figure' and overriding methods in 'Rectangle' and 'Triangle' follow the 'Open/Closed Principle', as the abstract class is open for extension by its subclasses through method overriding, yet closed for modification .

Inheritance in object-oriented programming can both enhance and challenge encapsulation. The document demonstrates that inheritance allows subclasses to reuse functionality from parent classes, which supports encapsulation by hiding complexity within the inherited modules. However, if not managed carefully, it can lead to tight coupling between base and derived classes, potentially exposing internal implementation details. This can make the system more fragile in terms of changes to parent classes affecting derived classes. Proper encapsulation requires thoughtful design to ensure that inheritance structures do not inadvertently expose or depend on the internal workings of their parent classes, affecting maintainability and robustness .

The benefits of using multilevel inheritance, as seen in the Java example, include the ability to build a natural hierarchy where each derived class builds on the functionality of its parent, such as how 'Sports' builds on 'Mark', which in turn extends 'Student'. This can lead to a more organized and modular codebase. However, potential downsides include the increased complexity of the inheritance chain, making the system harder to understand and maintain. It also introduces the risk of the 'fragile base class problem', where changes in a base class can have unpredictable effects on its subclasses. In Java, using interfaces or composition can sometimes be more flexible alternatives to deep inheritance hierarchies .

Abstract classes in Java, as illustrated in the document's example, serve as a blueprint for other classes. For instance, 'Figure' is an abstract class that mandates its subclasses to implement the 'area()' method. While a regular class can be instantiated and provides complete implementations, an abstract class cannot be instantiated directly. Its main role is to define abstract methods that subclasses must override and potentially offer common functionality to all derived classes. Abstract classes are beneficial when you have families of classes that share common structures but differ in some implementations .

The document primarily demonstrates inheritance and abstract classes rather than interfaces. Interfaces in Java are used to define a contract that implementing classes must fulfill without dictating a class hierarchy, differing from abstract classes, which can have implemented methods and fields. Although interfaces are not explicitly illustrated, the use of abstract classes like 'Figure' serves a similar purpose in providing a common method for area calculation that must be implemented by subclasses. However, unlike interfaces, abstract classes can provide a partial implementation and hold state. If interfaces were used, they could define the method signature for 'area()' without any implementation, allowing even more flexibility in the class hierarchy .

Selecting between abstract classes and interfaces involves considering the specific requirements of the program design. Abstract classes are suitable when you want to provide a common base class with shared state or behavior (as seen with 'Figure'), and when you desire some degree of code reuse among subclasses. Interfaces are ideal when you wish to define a contract that multiple classes can implement without enforcing a class hierarchy. For example, if the document's program needed to model multiple unrelated behaviors across classes, interfaces might be more appropriate. Another consideration is that a class can implement multiple interfaces but only inherit from one class, so choosing interfaces allows for more flexibility regarding multiple inheritance of type .

The concept of method overriding in the given examples is applied when subclasses provide specific implementations for methods declared in a parent class. In the context of abstract methods, the document shows the class 'Figure' with the abstract method 'area()', which is then overridden by the subclasses 'Rectangle' and 'Triangle', providing specific logic for area calculations. For concrete methods, although not directly shown in the provided examples, overriding allows a subclass to replace or extend the functionality of a parent's non-abstract method. This is useful to tailor behavior to subclasses while maintaining a common interface .

In the 'Rectangle' and 'Triangle' classes, the 'super' keyword is used within constructors to call the constructor of their abstract superclass 'Figure'. This demonstrates the use of 'super' to initialize the 'dim1' and 'dim2' fields defined in the superclass. By calling 'super(a, b);' in their constructors, both subclasses ensure that the dimensions are properly initialized before any specific subclass behavior is implemented. This is a common technique to ensure proper initialization of inherited fields .

Polymorphism is demonstrated in the provided Java program through the use of an abstract class named 'Figure' and the overriding of its 'area()' method in its subclasses 'Rectangle' and 'Triangle'. Polymorphism allows the program to treat objects of these subclasses as objects of the 'Figure' type, enabling the invocation of the overridden 'area()' method without knowing the specific subclass. The line 'Figure figref;' declares a reference of type 'Figure', and it is assigned objects of type 'Rectangle' and 'Triangle'. When 'figref.area()' is called, the correct overridden method for the specific object type is invoked, showcasing polymorphism .

The implementation of abstract classes contributes to encapsulation by hiding the implementation details of the method 'area()' in the abstract class 'Figure', and providing a standard interface for all derived classes to implement. This forces subclasses like 'Rectangle' and 'Triangle' to provide their specific logic for the 'area()' method, thus encapsulating the specific details of how the area is calculated inside each subclass. This allows users of these classes to rely on the 'area()' method without knowing how each specific figure calculates its area .

You might also like