0% found this document useful (0 votes)
17 views36 pages

Lecture 3 Polymorphism and Inheritance

Uploaded by

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

Lecture 3 Polymorphism and Inheritance

Uploaded by

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

Polymorphism and

Inheritance
Object-Oriented Programming (CPE104L)
POLYMORPHISM

➢ Polymorphism is the ability of objects


belonging to different types to respond to
methods of the same name, each one
according to the right type-specific behavior.

➢ It is the ability to redefine methods for derived


classes.

2
IMPLEMENTING POLYMORPHISM

1. Method Overloading
Using one method identifier to refer to multiple
functions in the same class, In the Java
programming language, methods can be
overloaded but not variables or operators.

3
Method Overloading

➢ Constructor Overloading
- creating more than one constructor in a class

➢ Method Overloading
- creating multiple methods having same
name in one class.

4
Example : Constructor Overloading

public Student(){

}

public Student(String name, int studNo){


[Link] = “Anonymous”;
}
//more constructor here…

5
Example : Method Overloading

public void eat(){



}

public void eat(String food){


[Link](“The animal is eating “ +food);
}
//more methods here…

6
Example : Method Overloading

7
IMPLEMENTING POLYMORPHISM

2. Method Overriding
Providing a different implementation of a
method in a subclass of the class that
originally defined a method.

8
Example : Method Overriding
public class ElectronicDevice{

public void on(){
[Link](“The device is turned on!”);
}
}

public class Computer extends ElectronicDevice{


public void on(){
[Link](“The computer boots…”);
[Link](“The computer loads drivers…”);
[Link](“Welcome to Windows XP!”);
}
}

9
Overloading VS. Overriding
Overloading Overriding
Overloaded methods supplement Overriding method replaces the
each other. method it overrides.
Overloaded methods can exist, in Each method in a base class can
any number, in the same class. be overridden at most once in any
one derived class.
Overloaded methods must have Overriding methods must have
different argument lists. argument lists of identical type
and order.
The return type of an overloaded The return type of an overriding
method may be chosen freely. method must be identical to the
method it overrides.

10
Method
Overriding

Output:

11
Method
Overloading

Output:

12
INHERITANCE

➢Inheritance is the technique of deriving


new class definitions from an existing class
definition.

13
➢The following are the benefits of using class
inheritance in OOP:
•Re-use of predefined and well-tested classes
•Standardization of behaviors across a group
of classes
•Ability to use members of a family of classes
interchangeably in methods

14
15
Superclasses and Subclasses

➢Superclass is the class from which another


class inherits properties. This is a class that is
on top of a hierarchy.

16
Superclasses and Subclasses

➢Subclass is a class that inherits all the non-


private attributes and methods, except
constructors from a superclass. This class
has the ability to override methods of the
superclass.

17
Vehicle Class Hierarchy

Vehicle

Water Vehicle Land Vehicle Air Vehicle

Truck Car

Family Car Luxury Car Sports Car

18
Syntax for Implementing Inheritance:

19
Types of Inheritance:

20
Types of Inheritance:

21
TAKE NOTE:

•Java does not support multiple inheritance


however, Java provides same effects and
benefits of multiple inheritance through the
use of interface.

•An interface is like a contract that says: “Any


class that implements me must provide these
methods.”
•A class can implement multiple interfaces, so
it can behave like it inherited from many
sources.
22
23
package classes;
import [Link].*;
void read(){
public class MyBookExtends{ [Link]("Reading...page
public static void main(String[] args)throws no."+pg+" of the book "+title+" Edition
IOException{ "+edition+" written by "+author);
Workbook WB = new Workbook(); }
[Link](); }
}
}
class Workbook extends Book{
class Book{
String author;
public void turnpage(){
String title; title = "Simplifying Java";
int edition; author = “MML";
int pg; edition = 4;
pg = 20;
void open(){ read();
[Link]("Book opened!"); }
}
}
void close(){
[Link]("Book closed!");
}

24
The super Keyword

➢super – contains a reference to the parent class


(superclass) object. It is used to access members of
a class inherited by the class in which it appears.

25
public class BookInterface {

public static void main(String[] args) {


Workbook wb = new Workbook();
Textbook tb = new Textbook();
[Link]();
[Link]();
[Link]();
[Link]();
[Link]();
[Link]();
}
}

interface Book{
void open();
void close();
void read(String title, String author, int edition);
}

26
class Workbook implements Book{
@Override
public void open(){
[Link]("\nWorkBook opened!");
}
@Override
public void close(){
[Link]("\nWorkBook closed!");
}
@Override
public void read(String title, String author, int edition){
[Link]("\nReading the workbook "+title+" Edition
"+edition+" written by "+author);
}
public void readlt(){
Workbook wb= new Workbook();
[Link]("Simplifying Java", “MML", 3);
}
}
27
class Textbook implements Book{
@Override
public void open(){
[Link]("\nTextBook opened!");
}
@Override
public void close(){
[Link]("\nTextBook closed!");
}
@Override
public void read(String title, String author, int edition){
[Link]("\nReading the textbook "+title+" Edition
"+edition+" written by "+author);
}
public void readlt(){
Textbook tb= new Textbook();
[Link]("Secrets of Java", "JLopez", 2);
}
}
28
THE ABSTRACT CLASS AND INTERFACE

➢Abstract Class – contains one or more


abstract methods and, therefore, can never
be instantiated. It is defined so that other
classes can extend them and make them
concrete by implementing the abstract
methods.

29
30
Syntax for Declaring Abstract Class:

31
32
33
THE ABSTRACT CLASS AND INTERFACE

➢Interface – an abstract class that


represents a collection of method definitions
and constant values. It can later be
implemented by classes that define the
interface using the implements keyword.

34
35
TAKE NOTE:

All abstract classes are public by


default and cannot be instantiated.
Constructors and public methods cannot be
declared as abstract.

36

You might also like