0% found this document useful (0 votes)
4 views7 pages

oop1

Chapter 9 discusses the concept of inheritance in object-oriented programming, specifically in Java, detailing its definition, advantages, and various types including single, multilevel, hierarchical, and multiple inheritance. It emphasizes the importance of reusability, flexibility, and data hiding in inheritance, and provides examples to illustrate each type. The chapter also explains how to implement inheritance using the 'extends' keyword and addresses the limitations of multiple inheritance in Java, which can only be achieved through interfaces.

Uploaded by

bprajna64
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)
4 views7 pages

oop1

Chapter 9 discusses the concept of inheritance in object-oriented programming, specifically in Java, detailing its definition, advantages, and various types including single, multilevel, hierarchical, and multiple inheritance. It emphasizes the importance of reusability, flexibility, and data hiding in inheritance, and provides examples to illustrate each type. The chapter also explains how to implement inheritance using the 'extends' keyword and addresses the limitations of multiple inheritance in Java, which can only be achieved through interfaces.

Uploaded by

bprajna64
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/ 7

CHAPTER 9

OOP CONCEPTS: INHERITANCE


Syllabus
Inheritance concept; types; Inheritance in java; Examples;
Inheritance: Extending a Class
 It is a mechanism in which one class acquires the property and behaviour of another class. OR The
mechanism of deriving a new class from an old one is called inheritance.
 Ex: a child inherits the traits of his/her parents.
 Class – it is a group of objects which have common properties.
 Super class – the class whose features are inherited.
 Sub class – the derived class that inherits the variables or methods of other classes. It is also known
as child class.
 Reusability – it is a mechanism which facilitates to reuse the fields and methods of existing class
when a new class is created.

Advantages of Inheritance
 Minimizing duplicate code: Key benefits of Inheritance include minimizing the identical code as it
allows sharing of the common code among other subclasses.
 Flexibility: Inheritance makes the code flexible to change, as you will adjust only in one place, and
the rest of the code will work smoothly.
 Overriding: With the help of Inheritance, you can override the methods of the base class.
 Data Hiding: The base class in Inheritance decides which data to be kept private, such that the
derived class will not be able to alter it.

Fig: Different forms of Inheritance

Types of Inheritance
There are different forms of inheritance in java

Subject: SEPP Notes/20CS43P pg. 1


1. Single inheritance: A derived class with only one super class is called single inheritance.
2. Multi-level inheritance: It is a process of deriving a class from previously derived class is called
multi-level inheritance.
3. Hierarchical inheritance: More than one derived classes from a single base class is called
hierarchical inheritance.
4. Multiple inheritance: It is the process of deriving a class from a single base class is called multiple
inheritance.

Implementing Inheritance in Java


To inherit a class, we simply incorporate the definition of one class into another by
using“extends”keyword.
Syntax for extending a class is as follows,
Class Subclass-name extends Superclass-name
{
Variable declaration;
Method declaration;
}

 The extends keyword indicates that making a new class that derives from an existing class.
 The subclass will now contain its own variables and methods as well those of the superclass.
 This kind of situation occurs when we want to add some more properties to an existing class
without actually modifying it.
Example,
class A extends B {
// Here Class A will inherit the features of Class B
}

Single Inheritance [ simple inheritance]


 Derives a subclass from a single superclass.
 For example, subclass B inherit the properties of a single superclass A.
 The following figure shows the structure of single level inheritance.

 Syntax,
class A
{
------

Subject: SEPP Notes/20CS43P pg. 2


}
class B extends A
{
-------
}
Example,
In the example given below, Dog class inherits the Animal class
class Animal
{
void eat()
{
System.out.println("eating...");
}
}
class Dog extends Animal
{
void bark()
{
System.out.println("barking...");
}
}
class TestInheritance
{
public static void main(String args[])
{
Dog d=new Dog();
d.bark();//method of Dog class
d.eat();//method of Animal class
}}
Output: barking…
eating…

Multilevel Inheritance
 Multilevel inheritance is derived from a derived class.
 Multilevel inheritance allows building a chain of classes as shown in figure.
 We can include any number of levels in multilevel inheritance.
 For example, Class A is a superclass for the Class Band Class B is a superclass for the subclass,
Class C.

Subject: SEPP Notes/20CS43P pg. 3


Syntax,
class A
{
-----
}
class B extends A // first level
{
---------
}
class C extends B // second level
{
------
}

Example,
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...");

Subject: SEPP Notes/20CS43P pg. 4


}
}
class TestInheritance2
{
public static void main(String args[])
{
BabyDog d=new BabyDog();
d.weep();//method of BabyDog class
d.bark();//method of Dog class
d.eat();//method of Animal class
}
}

Output:
weeping
barking
eating
In the example given above ,BabyDog class inherits the Dog class which again inherits the Animal class,
so there is a multilevel inheritance.

Hierarchical Inheritance
 In Hierarchical inheritance one parent class will be inherited by many sub classes.
 Some features of one level are shared by many others below the level.
 One super class and many sub classes

 As per the above example Class A will be inherited by Class B, Class C and Class D.
 Class A will be acting as a parent class for Class B, Class C and Class D.

Syntax,
ClassA
{
-------
}
Class B extends A
{
-------

Subject: SEPP Notes/20CS43P pg. 5


}
class C extends A
{
-------
}
class D extends A
{
-------
}

Example,
In the example given below, Dog and Cat classes inherits the Animal class, so there is Hierarchical
inheritance.
class Animal
{
void eat()
{
System.out.println("eating...");
}
}
class Dog extends Animal
{
void bark()
{
System.out.println("barking...");
}
}
class Cat extends Animal
{
void meow()
{
System.out.println("meowing...");
}
}
class TestInheritance3
{
public static void main(String args[])
{
Cat c=new Cat();
c.meow();
c.eat();
//c.bark();//gives error
}
}

Subject: SEPP Notes/20CS43P pg. 6


Output:
Meowing
eating

Multiple Inheritance (Through Interfaces)


 In Multiple inheritances, one class can have more than one superclass and inherit features from all
parent classes.
 Java does not support multiple inheritances with classes.
 In Java, we can achieve multiple inheritances only through Interfaces.
 In the image below, Class C is derived from interfaces A and B.

Hybrid Inheritance
 It is a mix of two or more of the above types of inheritance.
 Since Java doesn’t support multiple inheritances with classes, hybrid inheritance involving multiple
inheritance is also not possible with classes.
 In Java, we can achieve hybrid inheritance only through Interfaces if we want to involve multiple
inheritance to implement Hybrid inheritance.
 It can be achieved through a combination of Multilevel Inheritance and Hierarchical Inheritance
with classes, Hierarchical and Single Inheritance with classes.

Subject: SEPP Notes/20CS43P pg. 7

You might also like