Lecture 6 Inheritance
Lecture 6 Inheritance
Programming
Lecture 6
BSCS 2(Afternoon)
Spring 2024
[email protected]
Topics to be covered according to
outline
• Types of relation
among classes
• Inheritance
• Association (will be
discussed later)
• Access Control
• Default
• Public
• Private
• Protected
• Purpose and use of
keyword “Super”
• Multilevel Inheritance
• Function Overriding
• Overriding vs.
Overloading
Inheritance:
DemoBoxWeight.Java
Output:
Multilevel Hierarchy (Multilevel
Inheritance )
Multilevel Hierarchy (Multilevel
Inheritance)
Multilevel Hierarchy (Multilevel
Inheritance)
Box
WightBox ColorBox
Multilevel Inheritance vs. Multiple
Inheritance
• You can only specify one superclass for any subclass that you
create.
• Java does not support the inheritance of multiple super classes
into a single sub class.
• You can, as stated, create a hierarchy of inheritance in which a
subclass becomes a superclass of another subclass. However, no
class can be a superclass of itself.
Access Control
Keyword “Super”
Output:
Advantages of Inheritance
• Reusability
• Reduce redundancy
• Increase Maintainability
Multilevel Inheritance
Box
BoxWeight
Shipment
Method Overriding
• Method overriding occurs only when the names and the type
signatures of the two methods are identical.
• If they are not, then the two methods are simply overloaded. For
example,
Overriding vs. Overloading
• Function Overloading
• Functions with same name but different parameter list exist within the
same class or exist in superclass and its child class
• Inheritance is not compulsory for overloading to occur
• Function Overriding
• Functions with same name and same parameter list exist in a superclass
and in its child class
• Inheritance is compulsory for overloading to occur
Questions Are Welcome