You are on page 1of 14

INHERITANCE,

POLYMORPHISM AND
INTERFACE
Object Oriented Programming

Inheritance
2

The class that is used to define a new


class is called a parent class (or
superclass or base class).
A class created to inherit from a base
class is called a child class (or subclass
or derived class) .

Inheritance, Polymorphism and Interface

Inheritance
3

Benefits of Inheritance in
OOP:Reusability

Once a behavior (method) is defined in a


superclass, that behavior is automatically
inherited by all subclasses.
Therefore, you can encode a method only
once and they can be used by all
subclasses.
A subclass only needs to implement its
differences with its parent.
Inheritance, Polymorphism and Interface

Inheritance
4

The extends Keyword

This is used to set the relationship between


a child class and a parent class.
class childClass extends parentClass
{
//characteristics of the child
here
}

Inheritance, Polymorphism and Interface

Inheritance
5

Using the super Keyword


If

your method overrides one of its superclass's


methods, you can invoke the overridden method
through the use of the keywordsuper

Things to consider when using the super


constructor call:
The

super() call must occur as the first statement in a


constructor.
The super() call can only be used in a constructor
definition.
These imply that the this() construct and the super()
calls cannot both occur in the same constructor.
Inheritance, Polymorphism and Interface

Method Overriding
6

Overriding is the process of superseding


a superclass method by defining a
method in the subclass that has the
same name and parameters as a
method in the superclass.
It modifies the implementation of a
particular piece of behavior for a
subclass.

Advanced Object-Oriented Concepts (Part 1)

Method Overriding
7

Rules apply to overridden methods:


1.

2.

3.

An overriding method cannot be less


accessible than the method it overrides.
An overriding method cannot throw more
exceptions than the method it overrides.
A static method cannot be overridden to
be non-static.

Advanced Object-Oriented Concepts (Part 1)

Final Classes and Methods


8

You use thefinalkeyword in a method


declaration to indicate that the method
cannot be overridden by subclasses
A class that is declared final cannot be
subclassed
public final class ClassName {

Advanced Object-Oriented Concepts (Part 1)

Abstract Classes and


Methods

An abstract class is a class that cannot


be instantiated.
It is declared abstract it may or may
not include abstract methods.

Advanced Object-Oriented Concepts (Part 1)

10

Abstract Classes and


Methods

An abstract method is a method that is


declared without an implementation (no
curly braces and no method
statements).
To create an abstract method, write the
method declaration without the body
and use the abstract keyword.

Advanced Object-Oriented Concepts (Part 1)

11

Abstract Classes and


Methods

Coding Guideline:

Use abstract classes to define broad types


of behaviors at the top of an objectoriented programming class hierarchy, and
use its subclasses to provide
implementation details of the abstract
class.

Advanced Object-Oriented Concepts (Part 1)

Interfaces
12

collection of constants and method


declarations

method declarations do not include an


implementation (there is no method body)

Advanced Object-Oriented Concepts (Part 1)

Interfaces
13

Reasons for using interfaces:

To have unrelated classes implement


similar methods
To reveal an objects programming interface
without revealing its class
To model multiple inheritance which allows
a class to have more than one superclass

Advanced Object-Oriented Concepts (Part 1)

Interfaces
14

To use an interface, you include the


keyword implements and the interface
name in the class header.
The syntax for implementing interfaces is:
public class ClassName implements
interfaceName {
//code here
}

Advanced Object-Oriented Concepts (Part 1)

You might also like