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

BSCS 208 - AOOP - Lecture 5a - Single Inheritance

The document discusses single inheritance in object-oriented programming using Java. It explains that a subclass inherits and can override methods from its superclass, and that a subclass is a subtype that can be used wherever the superclass is expected. It also notes that Java only allows single inheritance of classes for simplicity.

Uploaded by

craigcarlos95
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

BSCS 208 - AOOP - Lecture 5a - Single Inheritance

The document discusses single inheritance in object-oriented programming using Java. It explains that a subclass inherits and can override methods from its superclass, and that a subclass is a subtype that can be used wherever the superclass is expected. It also notes that Java only allows single inheritance of classes for simplicity.

Uploaded by

craigcarlos95
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 9

BSCS 208: Advanced

Object Oriented
Programming

Lecture 5a - Single inheritance

1
Single Inheritance
 The Java model of programming makes
extensive use of Inheritance
 Normal inheritance plays two roles in
programming.
• When class B inherits from class A, it “reuses”
all the non-private methods and members of
class A.
• B also becomes a subtype of A.

2
 Inheritance Hierarchies
• The standard way of drawing out inheritance
is through a tree-like hierarchy.
• In UML the arrows point from the subclass to
the superclass. This is because the
superclass doesn’t generally know of all of its
subclasses but the subclasses know of the
superclass.

3
 Inheritance for Code Reuse
• The first side effect of inheritance is gaining
“copies of” non-private members.
• This means that if A had a public method get()
then B will also have a public method get().

4
 Virtual methods
• One of the powers of Java is that you don’t
always have to use the methods defined by
the superclass. You can override them in the
subclass.
• Methods that can be overridden are called
virtual methods. By default all methods in
Java are virtual, which means they can all be
overriden.

5
 Inheritance for Subtyping
• Inheritance also provides subtyping. This is
because the subclass has all the public
methods and members of the superclass.
• Formally, when we say that B is a subtype of
A, what we are saying is that any place in the
code where an A is expected, a B can be
used, or a B can always take the place of an
A.

6
 Single Inheritance of Classes
• Java only allows single inheritance of classes
i.e. a class can only inherit from one
superclass
• This greatly simplifies code by reducing
ambiguity. C++ has multiple inheritance which
causes one to frequently need to specify
which superclass of a given class a method
should be called through.

7
Inheritance examples

Superclass Subclasses
Student GraduateStudent, UndergraduateStudent

Shape Circle, Triangle, Rectangle

Loan CarLoan, HomeLoan, MortgageLoan

Employee Faculty, Staff

BankAccount CurrentAccount, SavingsAccount

8
Sample UML inheritance
hierarchy

You might also like