Module 3 Part2
Module 3 Part2
Part-2
It is the mechanism in java by which one class(child class) is allowed to inherit the
features(fields and methods) of another class(parent class).
Inheritance represents the IS-A relationship which is also known as a parent-
child relationship.
Uses of Inheritance
o Super Class/Parent Class: Superclass is the class from where a subclass inherits the
features. It is also called a base class or a parent class.
Example:
Class parent
{
Void method1()
{
[Link](“hello parent”);
}
}
o Sub Class/Child Class: Subclass is a class which inherits the other class using extend
keyword. It is also called a derived class, extended class, or child class.
On the basis of class, there can be three types of inheritance in java: single, multilevel and
hierarchical.
In java programming, multiple and hybrid inheritance is supported through interface only.
We will learn about interfaces later.
Types of inheritance:
SINGLE INHERITANCE
When a class extends another one class only then we call it a single inheritance. The below
flow diagram shows that class B extends only one class which is A. Here A is a Super
class of B and B would be a Sub class of A.
Example Program on Single Inheritance
class A
{ public void methodA()
{ [Link]("Base class method");
}
}
class B extends A
{ public void methodB()
{ [Link]("Child class method");
}
}
class single
{ public static void main(String args[])
{ B obj = new B();
[Link](); //calling super class method
[Link](); //calling local method
}
}
OUTPUT:
Base class method
Child class method
SUBCLASS CONSTRUCTOR
A subclass constructor is used to construct the instance variables of both the subclass and the
superclass. The subclass constructor uses the keyword super to invoke the constructor method
of the superclass. The call to the superclass constructor must appear as the first statement
within the subclass constructor and the parameters in the super call must match the order and
type of the instance variable declared in the superclass.
Example program1
class parent
{
parent()
{
[Link]("p1");
}
}
class child extends parent
{
child()
{
[Link]("p2");
}
}
class single {
public static void main(String[] args) {
child n=new child();
}
}
Output: p1p2
Example program2
class Room
{ int l,b;
Room(int x, int y)
{ l=x;b=y; }
int area()
{ return(l*b); }
}
class Bedroom extends Room //Inheriting Room
{ int h;
Bedroom(int x,int y, int z)
{ super(x,y); // Pass values to superclass
h = z;
}
int volume()
{ return(l*b*h); }
}
class subclasscons
{ public static void main(String args[])
{ Bedroom room1=new Bedroom(10,15,20);
int a=[Link](); //superclass method
int v=[Link](); //subclass method
[Link]("Area= "+a);
[Link]("Volume="+v);
}
}
OUTPUT: Area=150
Volume=3000
SUPER KEYWORD
The super keyword in java is a reference variable that is used to refer immediate parent class
object.
class parent
{
parent()
{
[Link]("p1");
}
void m1()
{
[Link]("parent m1");
}
}
class child extends parent
{
child()
{
[Link]("p2");
}
void m1()
{
[Link]("child m1");
}
void m2()
{
m1();
super.m1();
}
}
class single {
public static void main(String[] args) {
child n=new child();
n.m2();
}
}
Output: p1
p2
child m1
parent m1
class parent
{
parent()
{
[Link]("p1");
}
}
class child extends parent
{
child()
{
super();
[Link]("p2");
}
}
class single {
public static void main(String[] args) {
child n=new child();
}
}
Output:
p1
p2
MULTILEVEL INHERITANCE
Multilevel inheritance refers to a mechanism in Object Oriented technology where one can
inherit from a derived class, thereby making this derived class the base class for the new
class. As you can see in below flow diagram C is subclass or child class of B and B is a child
class of A.
Example Program on Multilevel Inheritance
class X
{ public void methodX()
{ [Link]("Class X method"); }
}
class Y extends X
{ public void methodY()
{ [Link]("class Y method"); }
}
class Z extends Y
{ public void methodZ()
{ [Link]("class Z method"); }
}
class multilevel
{
public static void main(String args[])
{ Z obj = new Z();
[Link](); //calling grand parent class method
[Link](); //calling parent class method
[Link](); //calling local method
}
}
OUTPUT :
Class X method
Class Y method
Class Z method
HIERARCHICAL INHERITANCE
In this type of inheritance one class is inherited by many sub classes.
Example Program on Hierarchical Inheritance
class A
{ int a=10; }
class B extends A
{ int b=30;
void display()
{ [Link]("The values are:"+a+" "+b); }
}
class C extends A
{ int c=40;
void show()
{ [Link]("The values are:"+a+" "+c); }
}
class hierarchicaldemo
{ public static void main(String args[])
{ B obj1=new B();
[Link]();
C obj2=new C();
[Link]();
}
}
OUTPUT:
The values are:10 30
The values are:10 40
Method Overriding:
If subclass (child class) has the same method as declared in the parent class, it is known
as method overriding in Java.
In other words, If a subclass provides the specific implementation of the method that has been
declared by one of its parent class, it is known as method overriding.
Example:
Design a vehicle class hierarchy in Java, and develop a program to demonstrate
Polymorphism.
import [Link].*;
class Vehicle
{ String regno;
int model;
Vehicle(String r, int m)
{ regno=r;
model=m;
}
void display()
{ [Link]("registration no:"+regno);
[Link]("model no:"+model);
}
}
class Twowheeler extends Vehicle
{ int noofwheel;
Twowheeler(String r,int m,int n)
{ super(r,m);
noofwheel=n;
}
void display()
{ [Link]("Two wheeler tvs");
[Link]();
[Link]("no of wheel" +noofwheel);
}
}
If a class includes abstract methods, then the class itself must be declared abstract, as in:
public abstract class GraphicObject {
// declare fields
// declare nonabstract methods
abstract void draw();
}
When an abstract class is subclassed, the subclass usually provides implementations for all of
the abstract methods in its parent class. However, if it does not, then the subclass must also be
declared abstract.
Example of Abstract class and Abstract Methods
abstract class Bike
{ Bike()
{ [Link]("bike is created"); }
abstract void run();
void changeGear()
{ [Link]("gear changed"); }
}
class Honda extends Bike
{ void run()
{ [Link]("running safely.."); }
}
class abstractdemo1
{ public static void main(String args[])
{ Honda obj = new Honda();
[Link]();
[Link]();
}
}
OUTPUT:
bike is created
running safely..
gear changed
The final keyword in java is used to restrict the user. The java final keyword can be used in
many contexts. Final can be:
1. variable
2. method
3. class
The final keyword can be applied with the variables, a final variable that has no value it is
called blank final variable or uninitialized final variable. It can be initialized in the
constructor only. The blank final variable can be static also which will be initialized in the
static block only.
There is a final variable speedlimit, trying to change the value of this variable is not possible
because final variable once assigned a value can never be changed.
class FinalVariable
{ final int speedlimit=90;//final variable
void run()
{ speedlimit=400; }
}
class Bike1 extends FinalVariable
{ int speedlimit=80;
public static void main(String args[])
{ Bike1 obj=new Bike1();
[Link]();
[Link]("obj"+ [Link]);
}
}
OUTPUT:
[Link]: error: cannot assign a value to final variable speedlimit
speedlimit=400;
^
1 error
class finalmethod
{ final void demo()
{ [Link]("finalmethod Class Method"); }
}