0% found this document useful (0 votes)
16 views24 pages

OOP LEC4 Inheritance

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

OOP LEC4 Inheritance

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

Object Oriented Programming

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

Employee Student Alumnus

Faculty Staff

Administrator Teacher
Inheritance hierarchy for Shapes.

Shape

TwoDimensionalShape ThreeDimensionalShape

Circle Square Triangle Sphere Cube Tetrahedron


Superclasses and Subclasses (Cont.)
Class hierarchy
– Direct superclass
• Inherited explicitly (one level up hierarchy)
– Indirect superclass
• Inherited two or more levels up hierarchy
– Single inheritance
• Inherits from one superclass
– Multiple inheritance
• Inherits from multiple superclasses
– Java does not support multiple inheritance
Class Diagram for Words
Book
# pages : int

+ 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{}

• In most cases, the use of interfaces gives us aspects of


multiple inheritance without the overhead
The Object Class
• A class called Object is defined in the java.lang
package of the Java standard class library
• All classes are derived from the Object class
• If a class is not explicitly defined to be the child of an
existing class, it is assumed to be the child of the
Object class
• Therefore, the Object class is the ultimate root of
all class hierarchies

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy