CPE207 Object Oriented Programming (Week 9)
CPE207 Object Oriented Programming (Week 9)
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
((Cat) animal).meow();
10
Not every Animal "is-a" Cat
Cat cat = new Animal(); //wont compile
cannot call a Cat method on a variable of type Animal
11
Animal c = new Cat();
or
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
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