OOP Concepts Part 2 - Inheritance

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 26

Object-oriented

Programming Features
Part 2
Inheritance
Learning Objectives
 In this lesson, you will learn to:
◦ understand the concept of software reuse
◦ distinguish the types of inheritance
◦ explain the benefits of using inheritance
◦ apply inheritance in a Java application
◦ identify the rules and restrictions in using
inheritance
Inheritance
 Inheritance allows child classes to inherit the
characteristics of an existing parent class
◦ Attributes (fields and properties)
◦ Operations (methods)
 Child class can extend the parent class
◦ Add new fields and methods
◦ Redefine methods (modify existing behavior)
 The aim of inheritance is to provide the reusability
of code so that a class has to write only the unique
features and rest of the common properties and
functionalities can be extended from the another
class.
Types of Inheritance
 Inheritance terminology
Inheritance – Benefits
 Inheritance has a lot of benefits
◦ Extensibility
◦ Reusability
◦ Provides abstraction
◦ Eliminates redundant code
 Use inheritance for building “is-a”
relationships
◦ E.g. dog is-a animal (dogs are kind of animals)
 Don't use it to build “has-a” relationship
◦ E.g. dog has-a name (dog is not kind of name)
Inheritance – Example
Types of Inheritance

Java supports only single-inheritance


so this one is not applicable
Class Hierarchies
 Inheritance leads to a
hierarchy of classes
and/or interfaces in
an application:
 The general

characteristics are
specified high up in
the hierarchy
 More specific

characteristics are
specified below the
hierarchy
Inheritance in Java
 A class can inherit only one base class
◦ Example: public class B extends A

 A class can implement several interfaces


◦ Example: public class B implements IX, IY, IZ

 A class can extend one class and implement


several interfaces
◦ Example: public class B extends A implements IX,
IY, IZ
How to Define Inheritance?
 We must specify the name of the base class after
the name of the derived
public class Shape
{...}

public class Circle extends Shape


{...}
 In the constructor of the derived class we use the
keyword super to invoke the constructor of the
base class
public Circle (int x, int y)
{

super(x);
...

}
Simple Inheritance Example
public class Mammal public class Dog extends Mammal
{ {
public int age; public String breed;

public void Sleep() public Dog(String breed)


{ {
System.out.println("Shhh! I'm this.breed = breed;
sleeping!"); }
}
} public void WagTail()
{
System.out.println("Tailwagging...");
}
}
Inheritance: Adding Functionality
 Subclasses have all of the data members and
methods of the superclass
 Subclasses can add to the superclass
◦ Additional data members
◦ Additional methods
 Subclasses are more specific and have more
functionality
Inheritance: Important Aspects
 In Java, there is no multiple inheritance
◦ Only multiple interfaces can be implemented
 Constructors are not inherited
 Inheritance is transitive relation
◦ If C is derived from B, and B is derived from A, then
C inherits A as well
Inheritance: Important Features
 A derived class extends its base class
◦ It can add new members but cannot remove derived
ones
 Declaring new members with the same name
or signature hides the inherited ones
Inheritance and Accessibility
class Creature class Dog extends Mammal
{ {
protected String name; String breed;

public String getName(){ public String getBreed() {


return name; return breed;
} }

private void Talk() // super.Talk() cannot be invoked here (it is private)


{ }
System.out.println("I am creature ...");
} class InheritanceAndAccessibility
{
protected void Walk() static void Main()
{ {
System.out.println("Walking ...");
} Dog joe = new Dog();
} System.out.println(joe.getBreed());
--------------------------------------------- // joe.Walk() is protected and can not be invoked
// joe.Talk() is private and can not be invoked
class Mammal extends Creature // joe.name = "Rex"; // Name cannot be accessed here
{ // joe.breed = "Shih Tzu"; // Can't modify breed
// super.Talk() can be invoked here }
// name can be read but cannot be modified }
here
}
The Object class
 In Java, all classes are (direct or indirect)
subclasses of the Object class.
 It is the root of the inheritance hierarchy in Java
 If a class is not declared to inherit from another
then it implicitly inherits from the Object class
 It defines a ToString() method which could be
overriden by the subclasses.
 Example: From a class named Book
public String ToString()
{
return super.ToString() + “ by “ + author;
}
Summary
◦ Software reuse
◦ Types of inheritance
◦ Benefits of inheritance
◦ Inheritance in a Java
◦ Inheritance and Accessibility
Polymorphism
 Polymorphism is the ability to take more
than one form (objects have more than
one type)
◦ A class can be used through its parent
interface. E.g. Circle class and Rectangle
class inherit from Shape class
Shape shape = new Circle();
shape = new Rectangle();

◦ A child class may override some of the


behaviors of the parent class
Polymorphism
 Why handle an object of given type as object
of its base type?
 E.g. Shape shape = new Circle();
◦ To invoke abstract operations
◦ To mix different related types in the same collection
 E.g.

ArrayList <Circle> circles = new ArrayList <Circle>();


ArrayList <Rectangle> circles = new ArrayList <Rectangle>();

ArrayList <Shape> circles = new ArrayList <Shape>();


Polymorphism
◦ To pass a more specific object to a method that expects
a parameter of a more generic type
◦ To declare a more generic field which will be initialized
and "specialized" later.
◦ E.g. Circle and Rectangle inherits from Shape
Polymorphism: How it works?
 Polymorphism ensures that the appropriate
method of the subclass is called through its
base class' interface
 Polymorphism is implemented using a

technique called late method binding


◦ Exact method to be called is determined at runtime,
just before performing the call
 Note: Late binding is slower than normal
(early) binding
Example:

You might also like