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

CPE207 Object Oriented Programming (Week 9)

Uploaded by

mertbalaban58
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

CPE207 Object Oriented Programming (Week 9)

Uploaded by

mertbalaban58
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 26

Week 9

OOP Concepts: Polymorphism

Dr. Nehad Ramaha,


Computer Engineering Department
Karabük Universities These Slides mainly adopted from Assist. Prof. Dr. Ozacar Kasim lecture notes
1
The class notes are a compilation and edition from many sources. The instructor does not claim intellectual property or ownership of the lecture notes.
 Definition: Polymorphism
 Types of Polymorphism
 Polymorphism and Arrays
 Object Type-casting
 Exercises

2
class A {
public void method1() { System.out.println(“A1”); }
public void method3() { System.out.println(“A3”); }
}
class B extends A {
public void method2() { System.out.println(“B2”); }
public void method3() { System.out.println(“B3”); }
}
Class C {
public static void main(String[] args){
A a = new A();
B b = new B();
a.method1(); //
a.method2(); //
a.method3(); //
b.method1();//
b.method2();//
b.method3();//
}
}
2/10/2023 3
 Polymorphism (having multiple forms: poly + morph")
is the characteristic of being able to assign a different
meaning or usage to something.
 In OOP, it allows a variable, a method, or an object to
have more than one form

4
 Java supports 2 types of polymorphism:
1.Compile-time (Static)
 Method overloading
2.Run-time (Dynamic)
 Method overriding

5
Example

Overloading is compile-time
polymorphism where more than one
methods share the same name with
different parameters or signature and
different return type.

6
Example

superclass
Method Overriding is run time
polymorphism having same
method with same parameters or
signature but associated in a
class & its subclass.

subclass
2/10/2023
7
 Example: The problem we have is, we have 3 different classes (Cat,
Dog, Horse) all with the same behavior: eat().
 An array can be only one type, how can we put them all in an array?
◦ TypeOfArray[] myArray = new TypeOfArray[]{cat, dog, horse};
 Do we need 3 types of arrays?
 That's the problem we can solve using polymorphism.
 Using Polymorphism, we can deal with different sub classes as the
same super class
◦ Animal[] myArray = new Animal[]{cat, dog, horse};

8
 Let's create Dog, Horse, and Cat classes. And using an
array, to call eat() foreach animal.

9
converting one type to another type  Reference type casting
is known as type casting
 Casting from a subclass to a superclass is called
upcasting

 Upcasting is closely related to inheritance


 Primitive type casting
 Cat cat = new Cat();
double myDouble = 1.1;
int myInt = (int) myDouble;  Animal animal = cat;
//we’re “turning” one type into another.  animal = (Animal) cat;

 Casting from a superclass to a subclass is called


downcasting

 Animal animal = new Cat()

 ((Cat) animal).meow();

Upcasting narrows the list of methods and properties


available to this object, and downcasting can extend it.

10
 Not every Animal "is-a" Cat
Cat cat = new Animal(); //wont compile
cannot call a Cat method on a variable of type Animal

 Animal a = new Cat();


a.eat(); //will compile
a.meow(); //wont compile, object a can only use Animal
behaviors.

11
Animal c = new Cat();

Check this out!


c.eat();

 Will it call the eat method in Cat Class?

or

 Will it call the eat method in Animal Class?

12
dummy implementations

subclass 14
2/10/2023 subclass
What if we don’t want to implement a method in a super class?

14
Using abstract class

 An abstract class is a class that is declared abstract—it may or may not include abstract methods.
Abstract classes cannot be instantiated, but they can be subclassed.
 An abstract class can be used as a type of template for other classes. The abstract class will hold
common functionality for all classes that extend it.
 Without abstract classes, you would have to provide dummy implementations of the methods you
intend to override.

no body, no implementations
dummy implementations

15
ANOTHER EXAMPLE:
EMPLOYEE CLASSES

16
17
18
 abstract superclass Employee
– earning is declared abstract
 No implementation can be given for earnings in the Employee
abstract class
– An array of Employee variables will store references to
subclass objects
 earning method calls from these variables will call the
appropriate version of the earnings method

19
• You cannot create an instance from
Employee, because it is an abstract class

• But you can create an instance from its


subclasses

20
HourlyEmployee.java

 HourlyEmployee
class extends
Employee.

21
 SalariedEmployee class extends Employee.

2/10/2023 22
CommissionEmployee.java
 CommissionEmployee class extends Employee.

23
 BasePlusCommissionEmployee class extends CommissionEmployee.

24
MainClassTest {

2/10/2023 25
26
27

You might also like