You are on page 1of 13

NOTES: Java Programming

Unit 3: Inheritance, Interface and Package

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.

Que: What is the use of extends keyword?


Answer :
extends is the keyword used to inherit the properties of a class to another class.

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.

Syntax of single inheritance:


class BaseClass
{
//methods and fields
}
class DerivedClass extends BaseClass
{
//methods and fields
}

Example:
class A
{
//methods and fields
}
class B extends A
{
//methods and fields
}

Available Only: Vyankatesh student corner Mrs.Atole J.A.


NOTES: Java Programming

Program of single Inheritance:

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
{

public static void main(String args[])


{
Science obj=new Science();
obj.display();
}
}

Write a program to execute following inheritance

class Square
{
int length;
Square(int x)
{
length=x;
}

Available Only: Vyankatesh student corner Mrs.Atole J.A.


NOTES: Java Programming

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.

Available Only: Vyankatesh student corner Mrs.Atole J.A.


NOTES: Java Programming

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();
}
}

Available Only: Vyankatesh student corner Mrs.Atole J.A.


NOTES: Java Programming

Write a program to execute following inheritance

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

Available Only: Vyankatesh student corner Mrs.Atole J.A.


NOTES: Java Programming

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();
}
}

Available Only: Vyankatesh student corner Mrs.Atole J.A.


NOTES: Java Programming

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.

Example of method overloading


class Math
{
int cal;
float val;
void addition(int x, int y)
{
cal=x+y;
System.out.println("Addition:"+ cal);
}
void addition(int z)
{
cal=z+10;
System.out.println("Addition:"+ cal);
}
void addition(float x, float y)
{
val=x+y;
System.out.println("Addition:"+ val);
}
}
class AddOperation
{
public static void main(String args[ ])
{
Math m = new Math();
m.addition(10,20);
m.addition(40);
m.addition(1.2f,9.2f);
}
}

Available Only: Vyankatesh student corner Mrs.Atole J.A.


NOTES: Java Programming

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

Available Only: Vyankatesh student corner Mrs.Atole J.A.


NOTES: Java Programming

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();
}
}

Output: Sum of a & b =30


Sum of a ,b &c =60

Available Only: Vyankatesh student corner Mrs.Atole J.A.


NOTES: Java Programming

Dynamic Method Dispatch:

➢ Dynamic method dispatch is the mechanism by which a call to an overridden method


is resolved at run time, rather than compile time.
➢ When an overridden method is called through a superclass reference, Java determines
which version (superclass/subclasses) of that method is to be executed based upon the
type of the object being referred to at the time the call occurs. Thus, this
determination is made at run time.
➢ At run-time, it depends on the type of the object being referred to that determines
which version of an overridden method will be executed
➢ A superclass reference variable can refer to a subclass object. This is also known as
upcasting. Java uses this fact to resolve calls to overridden methods at run time.
➢ Therefore, if a superclass contains a method that is overridden by a subclass, then
when different types of objects are referred to through a superclass reference variable,
different versions of the method are executed. Here is an example that illustrates
dynamic method dispatch:

// A Java program to illustrate Dynamic Method Dispatch


class A
{
void m1()
{
System.out.println("Inside A's m1 method");
}
}

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[])
{

A a = new A(); // object of type A

Available Only: Vyankatesh student corner Mrs.Atole J.A.


NOTES: Java Programming

B b = new B(); // object of type B

C c = new C(); // object of type C

A ref; // obtain a reference of type A

ref = a; // ref refers to an A object

ref.m1(); // calling A's version of m1()

ref = b; // now ref refers to a B object

ref.m1(); // calling B's version of m1()

ref = c; // now ref refers to a C object

ref.m1(); // calling C's version of m1()


}
}

Available Only: Vyankatesh student corner Mrs.Atole J.A.

You might also like