OOP LEC4 Inheritance
OOP LEC4 Inheritance
Inheritance
Class & Object
• A class is to an Object what a blueprint is to a
house
• A class establishes the characteristics and the
behaviors of the object
• Classes are the plan; objects are the
embodiment of that plan
• Many houses can be built from the same
blueprint
Inheritance
• Inheritance allows a software developer to
derive a new class from an existing one
• The existing class is called the parent class, or
super class, or base class
• The derived class is called the child class or
subclass
• As the name implies, the child inherits
characteristics of the parent
Inheritance
– Software reusability
– Create new class from existing class
• Absorb existing class’s data and behaviors
• Enhance with new capabilities
– Subclass extends superclass
• Subclass
– More specialized group of objects
– Behaviors inherited from superclass
» Can customize
– Additional behaviors
Hierarchy
Bird
call: ?
color:?
FlyingBird
food:?
call: ?
WalkingBird movement:?
color:?
call: ? food:?
color:? movement:flew
food:?
movement:walked
Parrot Owl
call: Squawk call:?
Goose Ostrich color:? color:?
call: honk call: neek-neek food: fruit food:mice
color: gray color: brown
food: bugs food: grass
TalkingParrot
...
Inheritance hierarchy for university CommunityMembers.
CommunityMember
Faculty Staff
Administrator Teacher
Inheritance hierarchy for Shapes.
Shape
TwoDimensionalShape ThreeDimensionalShape
+ pageMessage() : void
Words Dictionary
- definitions : int
+ main (args : String[]) : void
+ definitionMessage() : void
Inheritance
• A programmer can tailor a derived class as needed
by adding new variables or methods, or by
modifying the inherited ones
• Software reuse is a fundamental benefit of
inheritance
• By using existing software components to create
new ones, we capitalize on all the effort that went
into the design, implementation, and testing of the
existing software
The protected Modifier
• Visibility modifiers affect the way that class
members can be used in a child class
• Variables and methods declared with private
visibility cannot be referenced by name in a child
class
• They can be referenced in the child class if they
are declared with public visibility -- but public
variables violate the principle of encapsulation
• There is a third visibility modifier that helps in
inheritance situations: protected
The protected Modifier
– All methods & variables (even those declared
private) are inherited by the child class
– Their definitions exist and memory is reserved for
the variables
– However they CANNOT be referenced by name
The super Reference
• Constructors are not inherited, even though
they have public visibility
• Yet we often want to use the parent's
constructor to set up the "parent's part" of the
object
• The super reference can be used to refer to
the parent class, and often is used to invoke
the parent's constructor
The super Reference
• A child’s constructor is responsible for calling
the parent’s constructor
• If the child constructor invokes the parent
(constructor) by using the super reference,
it MUST be the first line of code of the
constructor
• The super reference can also be used to
reference other variables and methods
defined in the parent class
Overloading vs. Overriding
• Overloading deals with multiple methods with the same
name in the same class, but with different signatures
• Overriding deals with two methods, one in a parent
class and one in a child class, that have the same
signature
• Overloading lets you define a similar operation in
different ways for different parameters
• Overriding lets you define a similar operation in different
ways for different object types
Multiple Inheritance
• Java supports single inheritance, meaning that a derived
class can have only one parent class
• Multiple inheritance allows a class to be derived from two
or more classes, inheriting the members of all parents
• Collisions, such as the same variable name in two parents,
have to be resolved
• Java does not support multiple inheritance
– Therefore following is illegal:
• public class extends Animal, Mammal{}