Object Oriented
Programming
Lecture 6
BSCS 2(Afternoon)
Spring 2024
sqayuum@numl.edu.pk
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:
• Inheritance is one of the cornerstones of object-oriented
programming.
• It allows the creation of hierarchical classifications.
• Using inheritance, you can create a general class that defines
traits common to a set of related items. This class can then be
inherited by other, more specific classes, each adding those things
that are unique to it.
• In the terminology of Java, a class that is inherited is called a
super class/parent class. The class that does the inheriting is
called a sub class/child class.
• A subclass is a specialized version of a superclass. It inherits all of
the members defined by the superclass and adds its own, unique
elements.
• Inheritance represents “IS-A” or “IS-A-KIND-OF” relationship
among classes
Example 1: Inheritance
Student “IS-A” Person
Teacher “IS-A” Person
Doctor “IS-A” Person
Example 2: Inheritance
Circle “IS-A-KIND OF” Shape
Line “IS-A-KIND OF” Shape
Triangle “IS-A-KIND OF” Shape
Examples: Inheritance
Examples: Inheritance
Output:
Box.Java
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”
• In previous example, the constructor of BoxWeight explicitly
initializing the width, height, and depth fields of Box.
• This duplicates the code that is already in its superclass,
• It requires that a superclass must grant access to these members in its
child class.
• However, if a superclass wants to keep the details of its
implementation to itself (if superclass keeps its data members
private) then?
• In this case, there would be no way for a subclass to directly access or
initialize these variables on its own.
• Whenever a subclass needs to refer to its immediate superclass, it
can do so by use of the keyword super.
Keyword “Super”
• Super use to calls the superclass’ constructor.
super(arg-list);
• Here, arg-list specifies any arguments needed by the constructor in the
superclass.
• super( ) must always be the first statement executed inside a subclass’
constructor.
• Super is used to access a member of the superclass that has
been hidden by the member (having same name as in superclass)
of a subclass.
super.member
Example
Example
Output:
Advantages of Inheritance
• Reusability
• Reduce redundancy
• Increase Maintainability
Multilevel Inheritance
• Up to this point, we have been using simple class hierarchies that
consist of only a superclass and a subclass.
• However, you can build hierarchies that contain as many layers of
inheritance as you like.
• As mentioned, it is perfectly acceptable to use a subclass as a
superclass of another.
• For example, given three classes called A, B, and C, C can be a subclass
of B, which is a subclass of A.
• When this type of situation occurs, each subclass inherits all of the traits
found in all of its super classes. In this case, C inherits all aspects of B
and A.
Example: Multilevel Inheritance
Box
BoxWeight
Shipment
Method Overriding
• In a class hierarchy, when a method in a subclass has the same
name and type signature as a method in its superclass, then
the method in the subclass is said to override the method in the
superclass.
• When an overridden method is called from within its subclass, it
will always refer to the version of that method defined by the
subclass. The version of the method defined by the superclass will
be hidden.
When show( ) is invoked on an object of type B,
the version of show( ) defined within B is used.
That is, the version of show( ) inside B overrides
the version declared in A. If you wish to access the
superclass version of an overridden method, you
can do so by using super.
Overriding vs. Overloading
• 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