Chapter - 2
Chapter - 2
To inherit a class, you simply incorporate the definition of one class into
another by using the extends keyword.
Super class /parent class / base class:
A class from which the subclass is derived is called super class
Sub class / derived class / child class:
A class that does the inheriting is called a sub class. Therefore, a sub
class is a specialized version of a super class. It inherits all the instance
variables and methods defined by the super class and adds its own,
unique elements.
Class A
{
---------
---------
}
Class B extends class C
{
--------
}
The advantage of inheritance is reusability which means the
same methods and variables defined in the parent class can be
used in child class
Disadvantage of inheritance is that if we change the code of parent
class then all the child classes which are inheriting/deriving the
parent class are effected and hence, it cannot be independent of
each other.
1.2 Explain different types of inheritance with examples.
The different types of inheritance are
1. Single Inheritance
2. Multilevel Inheritance
3. Hierarchical Inheritance
4. Hybrid Inheritance:
5. Multiple inheritance: is not supported in java. It is implemented
using interface
1. Single inheritance:
Single inheritance is the process of deriving a new class from only
one base class
Example:
class calculator
{
int add(int x,int y)
{
return x+y;
}
int sub(int x,int y)
{
return x-y;
}
}
class single extends calculator
{
int mul(int x,int y)
{
return x*y;
}
int div(int x,int y)
{
return x/y;
}
public static void main(String args[])
{
single cal=new single();
System.out.println(cal.add(3,5));
System.out.println(cal.sub(10,2);
System.out.println(cal.mul(5,6));
System.out.println(cal.div(10,3));
}
}
Multilevel Inheritance:
The process of deriving a new class from already derived class is known
as multilevel inheritance.
Syntax:
Class A
{
------
------
}
Class B extends A
{
-----
-----
}
Class C extends B
{
-----
-----
}
Here, the class A serves as a base class for the derived class B which in
turn serves as a base class for derived class C. Now, class C can inherit
the members of both class A & B.
Program:
class Animal
{
void eat()
{
System.out.println("eating...");
}
}
class Dog extends Animal
{
void bark()
{
System.out.println("barking...");
}
}
class BabyDog extends Dog
{
void weep()
{
System.out.println("weeping...");
}
}
class Test
{
public static void main(String args[])
{
BabyDog d=new BabyDog();
d.weep();
d.bark();
d.eat();
}
}
3.Hierarchical Inheritance:
The process of deriving two or more classes from single base class is
called Hierarchical inheritance.
Here, class A serves as a base class for the derived classes B, C, and D.
Syntax:
Class A
{
------
------
}
Class B extends A
{
-----
-----
}
Class C extends A
{
-----
-----
}
Program:
class A
{
void printA()
{
System.out.println("Class A");
}
}
class B extends A
{
void printB( )
{
System.out.println("Class B"); }
}
class C extends A
{
void printC( )
{
System.out.println("Class C");
}
}
class D extends A
{
void printD( )
{
System.out.println("Class D");
}
}
class Test
{
public static void main(String[] args)
{
B ob1 = new B( );
ob1.printA( );
ob1.printB( );
C ob2 = new C( );
ob2.printA( );
ob2.printC( );
Program:
class A
{
int a;
}
class B extends A
{
int b;
}
class C extends A
{
int c;
C()
{
a=20;
c=10;
}
void sum()
{
System.out.println(a+c);
}
}
class D extends B
{
int d;
D()
{
a=5;
b=2;
d=2;
}
void mul()
{
System.out.println(a*b*d);
}
}
class hybrid
{
public static void main(String args[])
{
C c=new C();
c.sum();
D f=new D();
f.mul();
}
}
A sub class can call a constructor defined by its supper class using the
following form of super.
super(arg-list);
class room
{
int length;
int breadth;
room(int x, int y)
{
length=x;
breadth=y;
}
int area()
{
return length*breadth;
}
}
class bedroom extends room
{
int height;
bedroom(int x,int y,int z)
{
super(x,y);
height=z;
}
int volume()
{
return length*breadth*height;
}
}
class Supconstr
{
public static void main(String args[])
{
bedroom room1=new bedroom(12,13,15);
System.out.println(room1.area());
System.out.println(room1.volume());
}
}
super.member
here, member can be either a variable or method. This form is used when
both subclass and super class member have the same name.
program:
class A
{
int i;
}
class B extends A
{
int i;
B(int a, int b)
{
super.i=a;
i=b;
}
void show()
{
System.out.println("i in super class:"+super.i);
System.out.println("i in subclass:"+i);
}
}
class Supmember
{
public static void main(String args[])
{
B b=new B(20,3);
b.show();
}
}
Method overriding:
In a class hierarchy, when a method in a subclass has the same name and
type signature as a method in its super class, then the method in the
subclass is said to override the method in the super class. In method
overriding we can achieve runtime polymorphism also called as dynamic
method dispatch.
1. The method must have the same name as in the super class
2. The method must have the same parameter as in the super class.
class A
int i,j;
A(int a,int b)
i=a;
j=b;
void show()
}
class B extends A
int k;
super(a,b);
k=c;
void show()
System.out.println("k:"+k);
class methodoverriding
B b=new B(2,3,4);
b.show();
Program:
class A
{
final void demo()
{
System.out.println(“class A”);
}
}
class B extends A
{
Void demo()
{
System.out.println(“class B”);
}
}
class test
{
Public static void main(Strings args[])
{
B b=new B();
b.demo();
}
}
Remember that:
Final methods are inherited but, they are not eligible for overriding.
INTERFACE:
Remember that,
Class Interface
methods.
Like classes, interfaces can also be extended. One interface can inherit
another by using extends keyword. The syntax is the same as for
inheriting classes.
interface A
{
void method1();
void method2();
}
interface B extends A
{
void method3();
}
When a class implements an interface that inherits another interface, it
must provide implementations for all methods defined within the interface
inheritance chain. Otherwise, this will cause a compile time error.
Example:
interface A
{
void method1();
void method2();
}
interface B extends A
{
void method3();
}
class myclass implements B
{
public void method1()
{
System.out.println("implement method1");
}
public void method2()
{
System.out.println("implement method2");
}
public void method3()
{
System.out.println("implement method3");
}
}
class Extend
{
public static void main(String args[])
{
myclass ob=new myclass();
ob.method1();
ob.method2();
ob.method3();
}
}
2.8 Explain the concept of implementing interfaces
Once, an interface has been defined, one or more classes can implement
that interface. To implement an interface include implements keyword in
a class definition, and create the methods defined by the interface. The
general form of a class that includes the implements keyword is
}
class test
{
public static void main(String args[])
{
C s=new C();
s.display();
s.print();
}
}
interface Swimmable
{
void swim();
}
class Duck implements Walkable, Swimmable
{
public void walk()
{
System.out.println("Duck is walking.");
}