EEI3262 Unit 2 Session 7
EEI3262 Unit 2 Session 7
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
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.
90
Session 7 : Inheritance
CommunityMember
Faculty Staff
91
Session 7 : Inheritance
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
8. Subclass constructors can call superclass constructors via the----- ---- keyword.
93
Session 7 : Inheritance
Write and compile the above program. Explain how the inheritance is done
in the program.
Discussion :
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
2. Define the terms super class, sub class and how they fits in the
Generalization/Specialization relationships.
95