0% found this document useful (0 votes)
1 views

Lecture 5

Inheritance is a mechanism in object-oriented programming where a new class derives features and properties from an existing class, promoting code reusability. There are five types of inheritance: single, multiple, multi-level, hierarchical, and hybrid, each with distinct structures and rules. Key concepts include method overriding, the use of the 'super' keyword to access superclass members, and considerations such as complexity and tight coupling in inheritance hierarchies.

Uploaded by

karan22k4755
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views

Lecture 5

Inheritance is a mechanism in object-oriented programming where a new class derives features and properties from an existing class, promoting code reusability. There are five types of inheritance: single, multiple, multi-level, hierarchical, and hybrid, each with distinct structures and rules. Key concepts include method overriding, the use of the 'super' keyword to access superclass members, and considerations such as complexity and tight coupling in inheritance hierarchies.

Uploaded by

karan22k4755
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 29

Inheritance

Presented By: Shereen Sikander


Definition
• When we create a new class from an existing class in such a way that the new
class accesses all the features(functions/methods) and properties(variables)
of the existing class, then that mechanism is called inheritance
Note:
1. In JAVA extends keyword is used to perform inheritance.
2. Inheritance gives an advantage of code reusability.
3. We can't access private members of a class through inheritance.
4. A sub- class can access features of super class so we should make an object
of a sub- class.
5. Method overriding is possible via Inheritance.
Types of Inheritance
• 5 types
• Single
• Multiple
• Multi-level
• Hierarchial
• Hybrid
Single
• The inheritance in which there is one Super class and one Sub-
class is called Single Inheritance.
Super class
• Syntax:
Class Super {
/*
Line of code
*/ } Sub class

Class Sub-class extends Super {


/*
Line of code
*/
}
Single inheritance Contd..
Single Inheritance Contd..
Single Inheriatnce Contd with
Private access Modifiers…
Multi-level
• In multi-level Inheritance we have a single Super Super Class
Class and multiple Sub Classes.
• Syntax:
Class Super {
/* Sub Class 1
Line of code
*/ }
Class Sub-class 1 extends Super { Sub Class 2
/*
Line of code
*/}
Class Sub-class 2 extends Sub-class 1 { Sub Class 3
/*
Line of code */}
Multi-Level Inheritance Contd..
Multi-Level Inheritance Contd..
Activity
• Make a Super class named arithmetic and having Sub-classes named
add, subtract, multiply and result.
• ADD class should have a function which adds numbers given by user.
• Subtract class should have a function which Subtracts numbers given
by user.
• Multiply class should have a function which multiply numbers given
by user.
• Result should display answers of Add, Subtract and Multiply.
Multiple Inheritance
• Sub class inherits the properties of two or more Super classes with the same methods, Java compiler
can’t decide which class method it should inherit. There might be a chance of memory duplication i.e. a
reason why Java doesn’t support Multiple inheritance through classes.
• Can be done via Interfaces.
• Syntax:
Class A { Super Class A Super Class B
Void m1()
{} }

Class B {
Void m1() Sub Class
{} }

Class C extends A,B{


// Object of class C got confused that it should call m1 method of class A or B
}
Hierarchical Inheritance
• Contains only one Super class and multiple sub-classes. All sub-classes directly extends from
Super class.
• Syntax:
Class A{ Super Class
// line of code
}
Sub Class 1 Sub Class 2 Sub Class 3
Class B extends A
{ // line of code}

Class C extends A
{ // line of code}

Class D extends A
{ // line of code}
Hierarchical Inheritance Contd..
Hierarchical Inheritance Contd..
Hybrid
• It is a mix of two or more types of inheritance.
• Combination of Hierarchical + Multi-level
• Combination of Single + Hierarchical
• Combination of Single + Multi-level

• Note : No combination with multiple inheritance is possible because


java doesn’t support it.
Hybrid(Combination of Hierarchical + Multi-level)
Hybrid (Combination of Single +
Hierarchical)
Hybrid (Combination of Single +
Multi-level)
• Activity
Inheritance Continued
Disadvantages of Inheritance
• Complexity: Code is harder to understand if inheritance hierarchy is
deep or if multiple inheritance is used.

• Tight Coupling: There is a Tight coupling between the Super and


Subclasses so it is hard to change code in the super class without
affecting the subclass
Considerations While Using
Inheritance
• Overriding Methods
• Child classes can override methods of parent class to provide specialized
implementation. Comes handy in hierarchical inheritance.

• Access modifiers
• Access levels (private, public, protected) determine which attributes and
methods are accessible to child classes.
Assignment
• Diamond problem in inheritance.
Interview Questions
1. In java Multiple inheritance is achieved via _______ .
2. How can we access any variable/method of Super class via object of
sub class?
Super keyword
• It is used when we want to call variables, methods and constructor of
super class via object of a sub-class.
Note:
Whenever the super class and sub-class variables or method names are
same then Super keyword is used to refer to the variables/methods of
Super class.
Use of Super Keyword to Access
Variables
Use of Super Keyword to Access
Methods
Use of Super Keyword to Access
Constructors

The compiler adds Super keyword on its


own if the user doesn’t write it.
Super Keyword Contd..

You might also like