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

Lecture 06 Inheritence

Uploaded by

Ahmed Mehmood
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)
5 views

Lecture 06 Inheritence

Uploaded by

Ahmed Mehmood
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/ 27

Object Oriented Programming

Lecture 06

1
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
2
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

3
Hierarchy

4
Inheritance hierarchy for university CommunityMembers.

CommunityMember

Employee Student Alumnus

Faculty Staff

Administrator Teacher

5
Inheritance hierarchy for Shapes.

Shape

TwoDimensionalShape ThreeDimensionalShape

Circle Square Triangle Sphere Cube Tetrahedron

6
Superclasses and Subclasses (Cont.)

7
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

8
9
8-10
8-11
Class Diagram for Words
Book

# pages : int

+ pageMessage() : void

Words Dictionary

- definitions : int
+ main (args : String[]) : void + ComputeRatio() : double
+setdefinition() : void
+getdefinition() : int
12
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
13
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 and data hiding
• There is a third visibility modifier that helps in
inheritance situations: protected

14
The protected Modifier
– All methods & variables are inherited by the child
class
– Their definitions exist and memory is reserved for
the variables

15
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
16
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
17
8-18
8-19
8-20
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

21
Overloading vs. Overriding
• Overriding implements Runtime Polymorphism whereas
Overloading implements Compile time polymorphism.
• The method Overriding occurs between superclass and
subclass. Overloading occurs between the methods in the same
class.
• Overriding methods have the same signature i.e. same name
and method arguments. Overloaded method names are the
same but the parameters are different.
• With Overloading, the method to call is determined at the
compile-time. With overriding, the method call is determined at
the runtime based on the object type.
• If overriding breaks, it can cause serious issues in our program
because the effect will be visible at runtime. Whereas if
overloading breaks, the compile-time error will come and it’s
Overloading and Overriding Example

import java.util.Arrays;
public class Processor {
public void process(int i, int j)
{ System.out.printf("Processing two integers:
%d, %d", i, j); }
public void process(int[] ints)
{ System.out.println("Adding integer array:" +
Arrays.toString(ints)); }
public void process(Object[] objs)
{ System.out.println("Adding integer array:" +
Arrays.toString(objs)); }
}
Overloading and Overriding Example

class MathProcessor extends Processor {


@Override
public void process(int i, int j) {
System.out.println("Sum of integers is " + (i + j)); }
@Override
public void process(int[] ints) {
int sum = 0;
for (int i : ints) {
sum += i; }
System.out.println("Sum of integer array elements is " +
sum); }
}
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 dog extends Animal, Cars{}

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


multiple inheritance without the overhead
25
The Object Class

26
27

You might also like