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

EEI3262 Unit 2 Session 7

This document discusses inheritance in object-oriented programming, highlighting its role in software reuse by allowing new classes to absorb and enhance existing class members. It explains the concepts of superclasses and subclasses, the is-a relationship, and the use of constructors and the super keyword in Java. The session concludes with learning outcomes and review questions to reinforce understanding of inheritance and its benefits.

Uploaded by

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

EEI3262 Unit 2 Session 7

This document discusses inheritance in object-oriented programming, highlighting its role in software reuse by allowing new classes to absorb and enhance existing class members. It explains the concepts of superclasses and subclasses, the is-a relationship, and the use of constructors and the super keyword in Java. The session concludes with learning outcomes and review questions to reinforce understanding of inheritance and its benefits.

Uploaded by

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

Session 7 : Inheritance

Session 7
Inheritance

Contents
Introduction, p89
7.1 is-a relationship, p90
7.2 super classes and sub classes, p90
7.3 Constructors in sub classes, p92
7.4 Implement Super and Sub classes, p93
Summary, p94
Learning Outcomes, p95
Review Questions, p95

Introduction
This session continues our discussion of object-oriented programming
(OOP) by introducing one of its primary capabilities’ inheritances, which is
a form of software reuse in which a new class is created by absorbing an
existing class’s members and enhancing them with new or modified
capabilities. With inheritance, you can acquire the methods and fields of a
class to another. Inheritance increases the likelihood that a system will be
implemented and maintained effectively.
When creating a class, rather than declaring completely new members, you
can designate that the new class should inherit the members of an existing
class. The existing class is called the superclass, and the new class is the sub
class. (The C++ programming language refers to the superclass as the base
class and the subclass as the derived class.)
A subclass can add its own fields and methods. Therefore, a subclass is
more specific than its superclass and represents a more specialized group of
objects. The subclass exhibits the behaviours of its superclass and can
modify those behaviours so that they operate appropriately for the subclass.
This is why inheritance is sometimes referred to as specialization.
In Java, the class hierarchy begins with Class Object (in package java.lang),
which every class in Java directly or indirectly extends(or “inherits from”).
Java supports only single inheritance, in which each class is derived from
exactly one direct superclass. Unlike C++, Java does not support multiple
inheritance (which occurs when a class is derived from more than one direct
89
Session 7 : Inheritance

superclass). One of the Object-Oriented concepts, Polymorphism, explains


how to use Java interfaces to realize many of the benefits of multiple
inheritance while avoiding the associated problems. You will learn this in
the next session.

7.1 is-a relationship


In object oriented programming is-a is based on class inheritance. In an is-a
relationship, an object of a subclass can also be treated as an object of its
superclass. For an example a car is a vehicle.

In Java we can represent it as given below.

public class Vehicle{


}

public class Car extends Vehicle{


}

In Java every class maintains is-a relationship with the Object class. A
collection of pre-written classes is referred to as a class library. New classes
can inherit from classes in class libraries. Organizations develop their own
class libraries and can take advantage of others available worldwide. Most
of the new software are constructed from standardized reusable components,
just as automobiles and most computer hardware. This will facilitate the
development of more powerful, abundant and economical software.

7.2 Super Classes and Sub Classes


As it was explained above Java supports inheritance that enables a class to
inherit data members and methods from another class. Inheritance enables
you to reuse the functionalities and capabilities of the existing class by
extending a new class from the existing class and adding new features to it.
In inheritance, the class that inherits the data members and methods from
another class is known as the subclass. The class from which the subclass
inherits is known as the superclass.
An object of a sub class is an object of the super class as well. For example,
a car loan is a loan as home improvement loans and mortgage loans.
Therefore, in Java, class CarLoan can be said to inherit from class Loan. In
this context, class Loan is a superclass and class CarLoanis a subclass. A
CarLoan is a specific type of Loan, but it’s incorrect to claim that every
loan is a car loan and the Loan could be any type of loan.

90
Session 7 : Inheritance

Table 7.1 :Super and Sub classes examples


Super class Sub class
Student GraduateStudent, UndergraduateStudent
Shape Circle, Triangle, Rectangle, Sphere, Cube
Loan CarLoan, HomeImprovementLoan, MortgageLoan
Employee Faculty, Staff
BankAccount CheckingAccount, SavingsAccount

Inheritance relationship forms a hierarchical structure. A superclass exists in


a hierarchical relationship with its subclasses. Let’s develop a sample class
hierarchy (Fig. 7.1), also called an inheritance hierarchy. A university
community has thousands of members, including employees, students and
alumni. Employees are either faculty or staff members. Faculty members are
either administrators (e.g., deans and department chairpersons) or teachers.
The hierarchy could contain many other classes. For example, students can
be graduate or undergraduate students. Undergraduate students can be
freshmen or seniors.

CommunityMember

Employee Student Alumnus

Faculty Staff

Figure 7.1 : An example of maintaining hierarchy as a tree


Each arrow in the hierarchy represents an is-a relationship. As we follow the
arrows upward in this class hierarchy, we can state, for instance, that “an
Employee is a CommunityMember” and “a Employee is a Faculty
member.” Community Member is the direct superclass of Employee,
Student and Alumnus and is an indirect superclass of all the other classes in
the diagram. Starting from the bottom, you can follow the arrows and apply
the is-a relationship up to the topmost superclass. For example, an
Administrator is a Faculty member, is an Employee, is
aCommunityMember and, of course, is an Object in Java.

91
Session 7 : Inheritance

7.3 Constructors in Subclasses


As we explained in the preceding section, instantiating a subclass begins a
chain of constructor calls in which the subclass constructor, before
performing its own tasks, invokes its direct superclass’s constructor either
explicitly via the super reference or implicitly calling the superclass’s
default constructor or no-argument constructor. Similarly, if the superclass
is derived from another class as is, of course, every class except Object the
superclass constructor invokes the constructor of the next class up the
hierarchy, and so on. The last constructor called in the chain is always the
constructor for class Object. The original subclass constructor’s body
finishes executing last. Each superclass’s constructor manipulates the
superclass instance variables that the subclass object inherits.

7.4 super keyword


super keyword is used to invoke the constructor of a super class. The
compiler adds super() statement as the first line in constructors for us if we
did not write it. If a default constructor is not present in your super
class(when you add a constructor of your own, the default constructor added
by the compiler vanishes), then you must call the existing constructor of
your super class from your sub classes constructor with the correct
parameters. An example is given below.

Here, default constructors are not present in any of the classes. So the
subclass must call the constructor of the super class by using super(colour,
noSeats)as the first line of the constructor.

92
Session 7 : Inheritance

Activity 7.1 : Inheritance

Fill in the blanks in each of the following statements:


1. ------------is a form of software reusability in which new classes acquire the
members of existing classes and embellish those classes with new capabilities.

2. A superclass’s---------members can be accessed in the superclass declaration and


in subclass declarations.

3. In a(n)----------relationship, an object of a subclass can also be treated as an object


of its superclass.

4. In a(n)-----------relationship, a class object has references to objects of other


classes as members.

5. In single inheritance, a class exists in a(n)---------relationship with its subclasses.

6. A superclass’s--------members are accessible anywhere that the program has a


reference to an object of that superclass or to an object of one of its subclasses.

7. When an object of a subclass is instantiated, a superclass-------is called implicitly or


explicitly.

8. Subclass constructors can call superclass constructors via the----- ---- keyword.

7.5 Implement Super and Subclasses


Let’s consider the example given below to demonstrate the use of
inheritance. In this example the super class is Calculation and the sub class
is My_Calculation.

93
Session 7 : Inheritance

Activity 7.1 : Inheritance

Write and compile the above program. Explain how the inheritance is done
in the program.

Discussion :

Both MyCalculator and Calculator classes contain their own default


constructors (no argument constructors). When you instantiate
MyCalculator class constructor of MyCalculator is called and that calls the
default constructor of Calculator class. When the addition method is
invoked, since there is no addition method given in MyCalculator class, the
method addition in the super class will be executed with the given
parameters and display value 30. The same applies for the method
substraction. When multiplication method is invoked, the method given in
MyCalculator will be executed.

You will be studying Polymorphism in the next session.

Summary
This session introduced inheritance, the ability to create classes by
absorbing an existing class’s members and embellishing them with new
capabilities. You learned the notions of super classes and subclasses and
used keyword extends to create a subclass that inherits members from a
superclass.
You can derive data members and methods from a single superclass that is a
subclass of another superclass. Java does not support multiple inheritance

94
Session 7 : Inheritance

directly. You can use the concept of method overriding to override the
superclass method with the subclass method having same names. You can
use super to access overridden superclass members. Constructors are used in
inheritance hierarchies. Any object in Java inherits the methods from the
class Object.

Learning Outcomes
Now you will be able to:
▪ explain the types of inheritance and the supported types in
Java
▪ differentiate the relationships, Association, Aggregation and
Reflexive between classes
▪ explain the benefits of using inheritance in an application
▪ write Java programs using inheritance

References
You are highly recommended to visit the videos given below before
proceeding to the next session. Link to this video is available in your online
class.
http://tinyurl.com/y7n43vrx
http://tinyurl.com/y7t95x2b
http://tinyurl.com/yauoz3wa

Review Questions

1. Inheritance is a key to reuse. Explain with examples.

2. Define the terms super class, sub class and how they fits in the
Generalization/Specialization relationships.

3. With an example explain the difference between multiple inheritance


and single inheritance.

95

You might also like