0% found this document useful (0 votes)
8 views21 pages

Chapter - 2

Chapter 2 discusses inheritance in Java, defining it as a mechanism where one class inherits properties and behaviors from another. It outlines various types of inheritance including single, multilevel, hierarchical, hybrid, and notes that multiple inheritance is not supported directly but can be achieved through interfaces. The chapter also covers the use of the super keyword, method overriding, and the concept of interfaces, highlighting their differences from classes and how they can be implemented.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views21 pages

Chapter - 2

Chapter 2 discusses inheritance in Java, defining it as a mechanism where one class inherits properties and behaviors from another. It outlines various types of inheritance including single, multilevel, hierarchical, hybrid, and notes that multiple inheritance is not supported directly but can be achieved through interfaces. The chapter also covers the use of the super keyword, method overriding, and the concept of interfaces, highlighting their differences from classes and how they can be implemented.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 21

CHAPTER – 2

2.1 Define inheritance and its types

In java, inheritance is the mechanism in which one class is allowed to


inherit the properties and behaviours of another class.
(or)
One class can acquire properties and behaviours of another class is
called inheritance
Inheritance is an important pillar of OOP.
General form is:

class Subclass-name extends Superclass-name


{
//body of a class
}

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:

A derived class with multilevel base classes is

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

D ob3 = new D();


Ob3.printA( );
Obj3.printD( );
}
}
4. Hybrid Inheritance:

Hybrid inheritance is a combination of more than one types of


inheritance.

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

2.3 Use of super keyword

The super keyword in Java is a reference variable which is used to refer


immediate super class members.
Uses of Super keyword:

 Super keyword is used to access member of immediate super class


that has been hidden by a member of a subclass.
 Super keyword is used to call super class constructors.

Using super keyword to call super class constructors:

A sub class can call a constructor defined by its supper class using the
following form of super.

super(arg-list);

Here, arg-list specifies any arguments needed by the constructor in the


super class. Super() must always be the first statement inside a subclass
constructor.

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

Using super keyword to access super class members:

Super keyword is used to access super class members(variables and


methods). The general form is

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

2.4 Explain method overriding and how to avoid overriding using


final.

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.

Rules for Java Method Overriding

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.

3. Static and final members are not overridden

class A

int i,j;

A(int a,int b)

i=a;

j=b;

void show()

System.out.println("i and j:"+i+" "+j);

}
class B extends A

int k;

B(int a,int b,int c)

super(a,b);

k=c;

void show()

System.out.println("k:"+k);

class methodoverriding

public static void main(String args[])

B b=new B(2,3,4);

b.show();

Avoid method overriding using ‘final’ keyword:

To avoid method overriding declare method as final. Method declared as


final cannot be overridden. The compiler checks and gives error if you try
to override the method.

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.

2.5 Define an interface and explain the concepts of interface.

INTERFACE:

An interface is defined much like a class, but it contains abstract methods


and static final variables only. To define an interface use interface
keyword

General form of an interface is

Access specifier interface <interface_name>


{
return_type method_name(parameter list);
.
.
type variable_name=value;
.
.
}
Here, access specifier is either public or not used. When no access
specifier is included, then default access specifier is public.
Interface_name is any valid java identifier.
The methods which are declared have no bodies. They end with a
semicolon after the parameter list. Because, they are abstract methods.
The class that includes an interface must implement all of the methods.
The variables declared inside the interface are implicitly final and
static, meaning they cannot be changed by the implementing class. They
must be initialized with a constant value.
All the methods and variables declared inside the interface are
implicitly public.
Example:
interface item
{
int code=100;
String name=”fan”
Void display();
}

Uses of java interface:

 It is used to achieve abstraction.


 It is used to implement multiple inheritance.

Remember that,

 An interface implemented by any number of classes. Also one class


can implement any number of interfaces.
 An interface may extends another interface. But, never implements
another interface
 We cannot instantiate an interface.

2.6 Compare Class and interface

Class Interface

The keyword used to create a The keyword used to create an


class is “class” interface is “interface”

A class can be instantiated i.e.,


An Interface cannot be instantiated i.e.
objects of a class can be
objects cannot be created.
created.

Classes do not support multiple The interface supports


inheritance. multiple inheritance.

It can be inherited from


It cannot inherit a class.
another class.

It can be inherited by a class by using


It can be inherited by another
the keyword ‘implements’ and it can
class using the keyword
be inherited by an interface using the
‘extends’.
keyword ‘extends’.

It can contain constructors. It cannot contain constructors.

It cannot contain abstract It contains abstract methods only.


Class Interface

methods.

Variables and methods in a


class can be declared using any All variables and methods in an
access specifier(public, private, interface are declared as public.
default, protected).

Variables in a class can be


All variables are static and final.
static, final, or neither.

2.7 Explain about extending interfaces.

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

access class classname implements interfacename


{
//Class body
}
Here, access is either public or not used. If a class implements more than
one interface, the interfaces are separated with a commas(,).
access class classname implements interfacename1,interfacename2
{
//Class body
}

When you implement an interface methods in a class, that must be


declared as public.
Example:
interface area
{
final static float pi=3.14F;
float compute(float x,float y);
}
class rectangle implements area
{
public float compute(float x,float y)
{
return x*y;
}
}
class circle implements area
{
public float compute(float x,float y)
{
return pi*x*y;
}
}
class imple
{
public static void main(String args[])
{
rectangle r=new rectangle();
circle c=new circle();
System.out.println("area of rectangle
is:"+r.compute(30,10));
System.out.println("area of circle is:"+c.compute(5,6));
}
}
Partial implementation of interface:
If a class includes an interface but does not fully implement the methods
defined by that interface, then that class must be declared as abstract.
Example:
Interface A
{
Void display();
}
abstract class incomplete implements A
{
Void display()
{
System.out.println(“A”);
}
}
Program:
interface A
{
void display();
void print();
}

abstract class B implements A


{
public void display()
{
System.out.println("a");
}
}
class C extends B
{
public void print()
{
System.out.println("b");
}

}
class test
{
public static void main(String args[])
{
C s=new C();
s.display();
s.print();
}
}

2.10 Explain multiple inheritance using interface.


When a class wants to implement more than one interfaces, we use
implements keyword followed by comma-separated list of interfaces
implemented by the class.
The general form is
access class classname implements interfacename1,interfacename2
{
//Class body
}
Ex:
Class test implements A,B
{
//class body
}
Program:
interface Walkable
{
void walk();
}

interface Swimmable
{
void swim();
}
class Duck implements Walkable, Swimmable
{
public void walk()
{
System.out.println("Duck is walking.");
}

public void swim()


{
System.out.println("Duck is swimming.");
}
}
class Multiple
{
public static void main(String[] args)
{
Duck duck = new Duck();
duck.walk();
duck.swim();
}
}

You might also like