Chapter 3
Chapter 3
Inheritance:
• Inheritance can be defined as the process where one class acquires the properties (methods
and fields) ofanother.
• The class which inherits the properties of other is known as subclass (derived class, child
class) and the class whose properties are inherited is known as superclass (base class,
parentclass).
• In the terminology of Java, a class that is inherited is called a superclass.
• The class that does the inheriting is called a subclass.
• Therefore, a subclass is a specialized version of a superclass.
• It inherits all of the members defined by the superclass and adds its own, unique
elements.
• To inherit a class, you simply incorporate the definition of one class into another by
using the extends keyword.
Types of Inheritance
1) Single Inheritance
2) Multilevel Inheritance
3) Hierarchical Inheritance
Inheritance Features:
1) Helps in reduced code
2) It makes use of reusability of code
3) It enhances readability of code
4) Execution of code is efficient.
5) For method overriding (so runtime polymorphism be achieved).
Creating Inheritance
The class can derived from another class by following syntax:
class sub_class extends super_classname
{
// code
}
Available Only: Vyankatesh student corner Mrs.Atole J.A.
NOTES: Java Programming
The keyword extends specifies that the properties of super class name are extended to sub
class name.
After this the subclass will contain all the methods of super class and it will add the members
of super class and it will add the members of its own.
1) Single heritance:
➢ When a subclass is derived simply from its parent’s class then this mechanism is
known as single inheritance.
➢ In this type there is only one child class and one parent class.
Example:
class A
{
//methods and fields
}
class B extends A
{
//methods and fields
}
class Faculty
{
float salary=30000;
}
class Science extends Faculty
{
float bonous=2000;
void display()
{
System.out.println("Salary is:"+ salary);
System.out.println("Bonous is:"+ bonous);
}
}
class SingleDemo
{
class Square
{
int length;
Square(int x)
{
length=x;
}
void area( )
{
int area=length * length;
System.out.println("Area of Square="+area);
}
}
class Rectangle extends Square
{
int breadth;
Rectangle(int x, int y)
{
super(x);
breadth=y;
}
void rectArea( )
{
int area1=length * breadth;
System.out.println("Area of Rectangle="+area1);
}
}
class Shape
{
public static void main(String args[ ])
{
Rectangle r=new Rectangle (10,20);
r . rectArea( );
r . area( );
}
}
2) Multilevel inheritance
➢ When a class is derived from already derived class then this mechanism is known as
multilevel inheritance.
➢ The derived class is called as subclass or child class for its parents' class and this
parent class work as child class for its just above parent class.
Multilevel inheritance can go up to any level.
class Faculty
{
float salary=30000;
}
class Science extends Faculty
{
float bonous=2000;
}
class College extends Science
{
String designation;
void display()
{
System.out.println("Salary is:"+ salary);
System.out.println("Bonous is:"+ bonous);
System.out.println("Bonous is:"+ designation);
}
}
class MultilevelDemo
{
public static void main(String args[ ])
{
College obj=new College();
obj.display();
}
}
class Square
{
int length;
Square(int x)
{
length=x;
}
void area( )
{
int area=length * length;
System.out.println("Area of Square="+area);
}
}
class Rectangle extends Square
{
int breadth;
Rectangle(int x, int y)
{
super(x);
breadth=y;
}
void rectArea( )
{
int area1=length * breadth;
System.out.println("Area of Rectangle="+area1);
}
}
class Box extends Rectangle
{
int height;
Box(int x, int y,int z)
{
super(x,y);
height=z;
Available Only: Vyankatesh student corner Mrs.Atole J.A.
NOTES: Java Programming
}
void volume( )
{
int volume=length*breadth*height;
System.out.println("Volume of Box="+volume);
}
}
class Shape
{
public static void main(String args[ ])
{
Box b=new Box(10,20,30);
b.volume( );
b.rectArea( );
b.area( );
}
}
3) Hierarchical inheritance
• In hierarchical inheritance one class is extended by many subclasses.
• It is one to many relationships.
• Many programming problems can be tasked into a hierarchy where certain features of one
level
are shared by many others below the level
class Faculty
{
float salary=30000;
}
class Science extends Faculty
{
float bonous=2000;
void display()
{
System.out.println("Salary is:"+ salary);
System.out.println("Bonous is:"+ bonous);
}
}
class College extends Faculty
{
String designation;
void display()
{
System.out.println("Salary is:"+ salary);
System.out.println("Bonous is:"+ designation);
}
}
class HierarchicalDemo
{
public static void main(String args[])
{
Science obj1=new Science();
obj1.display();
College obj2=new College();
obj2.display();
}
}
Method overloading
• Method Overloading means to define different methods with the same name but different
parameters lists and different definitions.
• It is used when objects are required to perform similar task but using different input
parameters that may vary either in number or type of arguments.
• Overloaded methods may have different return types.
• Method overloading allows user to achieve the compile time polymorphism.
Method overriding
• In a class hierarchy ,when a method in a subclass has the same name,same arguments
and same return type as a method in super class , then the method in the subclass is said
to be override the method in the super class.
• When an overridden method is called from within a subclass ,it will always refer to
subclass method .The method defined in super class is hidden.
• That is , when that method is called, the method defined in the subclass is invoked and
executed instead of the one in the superclass. This is known as overriding.
• In overriding return types and constructor parameters of method should match.
Program 1:
class Math
{
int a=50,b=20,c;
void calculate( )
{
c=a+b;
System.out.println("Addition:"+ c);
}
}
class Arithmetic extends Math
{
void calculate()
{
c=a-b;
System.out.println("Subtraction:"+ c);
}
}
class MethodOverride
{
public static void main(String args[ ])
{
Arithmetic a=new Arithmetic ();
a.calculate ();
}
}
Output: Subtraction: 30
Program 2:
class A
{
int i;
A(int a, int b)
{
i=a+b;
}
void add()
{
System.out.println(“Sum of a & b =” +i);
}
}
class B extends A
{
int j;
B(int a, int b, int c)
{
super(a,b);
j=a+b+c;
}
void add()
{
super.add();
System.out.println(“Sum of a, b & c=”+j);
}
}
class MethodOverride
{
public static void main(String args[ ])
{
B b=new B(10,20,30);
b.add();
}
}
class B extends A
{
// overriding m1()
void m1()
{
System.out.println("Inside B's m1 method");
}
}
class C extends A
{
// overriding m1()
void m1()
{
System.out.println("Inside C's m1 method");
}
}
class Dispatch
{
public static void main(String args[])
{