Exercise – 4
Implementing Single Inheritance
a) Write a JAVA program to implement Single Inheritance
AIM: To write a JAVA program to implement Single Inheritance.
DESCRIPTION:
Single inheritance in Java refers to a mechanism where a class (child or subclass) inherits the
properties and behaviors of another class (parent or superclass). This approach promotes code reusability,
allowing shared characteristics to be defined once in the parent class and reused by the subclass. In single
inheritance, a subclass can only inherit from one superclass, ensuring a simple and linear inheritance
hierarchy.
The extends keyword is used to implement inheritance. When a subclass extends a superclass, it inherits all
the accessible members (fields and methods) of the parent class. The subclass can also introduce its own
members or override inherited methods to provide specific behavior. While public and protected members
of the parent class are accessible to the child class, private members are not directly inherited but can be
accessed through public or protected methods in the parent class.
SOURCE-CODE:
class A
{
A()
{
[Link]("Inside A's Constructor");
}
}
class B extends A
{
B()
{
[Link]("Inside B's Constructor");
}
}
class singledemo
{
public static void main(String args[])
{
B obj=new B();
}
}
Output:
Result:
This Java program to implement single Inheritance. is successfully executed
Implementing Multi-level Inheritance
(b)Write a JAVA program to implement multi-level inheritance
AIM: To write a JAVA program to implement multi-level inheritance.
DESCRIPTION:
Multi-level inheritance in Java is a form of inheritance where a class is derived from another class,
which itself is derived from a superclass, creating a chain of inheritance. This allows a subclass to inherit
properties and behaviors from multiple levels of classes in the hierarchy. It promotes code reusability and
logical organization of classes.
In multilevel inheritance, the extends keyword is used to establish inheritance at each level. The subclass
inherits the features of its direct superclass and, indirectly, the features of all its ancestors. This means a
class at the end of the chain can access fields and methods from all previous levels of classes.
SOURCE-CODE:
class A
{
A()
{
[Link]("Inside A's Constructor");
}
}
class B extends A
{
B()
{
[Link]("Inside B's Constructor");
}
}
class C extends B
{
C()
{
[Link]("Inside C's Constructor");
}
}
class multidemo
{
public static void main(String args[])
{
C obj=new C();
}
}
Output:
Result:
This Java program to implement Multi Level Inheritance. is successfully executed.
Abstract Class
c) Write a JAVA program for abstract class to find areas of different shapes
AIM: To write a Java program for abstract class to find areas of different shapes.
DESCRIPTION:
An abstract class in Java is a class that cannot be instantiated and is meant to be extended by other
classes. It provides a blueprint for subclasses, allowing the definition of abstract methods (methods without
a body) that must be implemented by the subclasses, as well as concrete methods (methods with a body) that
can be inherited. This approach helps in defining common behavior while forcing subclasses to provide
specific implementations.
An abstract class is declared using the abstract keyword. It can contain fields, constructors, and
both abstract and non-abstract methods. While an abstract class cannot be directly instantiated, it can be
used as a reference type for objects of its subclasses.
SOURCE-CODE:
abstract class shape
{
abstract double area();
}
class rectangle extends shape
{
double length=12.5, breadth=2.5;
double area()
{
return length*breadth;
}
}
class triangle extends shape
{
double breadth=4.2, height=6.5;
double area()
{
return 0.5*breadth*height;
}
}
class square extends shape
{
double side=6.5;
double area()
{
return 4*side;
}
}
class shapedemo
{
public static void main(String args[])
{
rectangle r=new rectangle();
triangle t=new triangle();
square s=new square();
[Link]("The area of rectangle is: "+[Link]());
[Link]("The area of triangle is: "+[Link]());
[Link]("The area of square is: "+[Link]());
}
}
Output:
Result:
This Java program to implement abstract class to find areas of different shapes is successfully executed.