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

Oop Lecture 07

Polymorphism allows objects of different classes to be treated as objects of a general parent class. There are two main types of polymorphism: overloading and overriding. Overloading involves having multiple methods with the same name but different parameters. Overriding involves a subclass replacing a method inherited from its parent class with its own implementation. Abstract classes and interfaces allow for abstraction by defining common behaviors without providing concrete implementations.

Uploaded by

Kutemwa Mithi
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)
5 views

Oop Lecture 07

Polymorphism allows objects of different classes to be treated as objects of a general parent class. There are two main types of polymorphism: overloading and overriding. Overloading involves having multiple methods with the same name but different parameters. Overriding involves a subclass replacing a method inherited from its parent class with its own implementation. Abstract classes and interfaces allow for abstraction by defining common behaviors without providing concrete implementations.

Uploaded by

Kutemwa Mithi
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/ 24

Object-Oriented Programming

Lecture 7 – Polymorphism & Abstraction

Ralph Tambala

MUST . CSIT

Lecture 5 1 / 24
Outline

1 Polymorphism
Overloading
Overriding

2 Abstraction
Abstract classes and methods
Interfaces

Lecture 5 2 / 24
Polymorphism

Polymorphism
In any programming language, a signature is what distinguishes one
method from another.

Polymorphism means many (poly) shapes (morph). In Java, polymorphism


refers to the fact that you can have multiple methods with the same name
in the same class. See method signatures examples below:
foo(int i) and foo(int i, int j) are different
foo(int i) and foo(int k) are the same
foo(int i, double d) and foo(double d, int i) are different

There are two kinds of polymorphism:


1 Overloading – Two or more methods with different signatures
2 Overriding – Replacing an inherited method with another having the
same signature
Lecture 5 3 / 24
Polymorphism Overloading

Overloading

1 class Test {
2 public static void main ( String args []) {
3 myPrint (7) ;
4 myPrint (5.0) ;
5 }
6 static void myPrint ( int i ) {
7 System . out . println ( " int i = " + i ) ;
8 }
9 static void myPrint ( double d ) { // same name , difrnt
params
10 System . out . println ( " double d = " + d ) ;
11 }
12 }

Output:

int i = 7
double d = 5.0

Lecture 5 4 / 24
Polymorphism Overloading

Reasons for Overloading a Method (1)


To use the same names for methods that do essentially the same thing
println(int), println(double), println(boolean), ...
To supply defaults for the parameters:
1 int increment ( int amount ) {
2 count = count + amount ;
3 return count ;
4 }
5 int increment () { return increment (1) ; }
NB: One method can call another of the same name
To supply additional information:
1 void printResults () {
2 System . out . println ( " total = " + total ) ;
3 }
4 void printResult ( String message ) {
5 System . out . println ( message + " : " ) ;
6 printResults () ;
7 }
Lecture 5 5 / 24
Polymorphism Overloading

Reasons for Overloading a Method (2)


DRY (Don’t Repeat Yourself)

When you overload a method with another, very similar method, only
one of them should do most of the work:
1 void debug () {
2 System . out . println ( " first = " + first +
3 " , last = " + last ) ;
4 for ( int i = first ; i <= last ; i ++) {
5 System . out . print ( dictionary [ i ] + " " ) ;
6 }
7 System . out . println () ;
8 }
9 void debug ( String s ) {
10 System . out . println ( " At checkpoint " + s + " : " ) ;
11 debug () ;
12 }

Lecture 5 6 / 24
Polymorphism Overloading

Reasons for Overloading a Method (3)

To do “the same thing” but with different kinds of data


1 class Student extends Person {
2 ...
3 void printInfo () {
4 printBiodata () ;
5 printGrades () ;
6 }
7 }
8 class Professor extends Person () {
9 ...
10 void printInfo () {
11 printBiodata () ;
12 pr i n t R e s e a r c h Int er es ts () ;
13 }
14 }

Lecture 5 7 / 24
Polymorphism Overloading

Legal Assignments

1 class Example {
2 public static void main ( String args []) {
3 double d ;
4 int i ;
5 d = 5; // ok
6 i = 3.5; // error
7 i = ( int ) 3.5; // type casting ok
8 }
9 }

Widening is legal
Narrowing is illegal (only if type cast)

Lecture 5 8 / 24
Polymorphism Overloading

Legal Method Calls

1 class Test {
2 public static void main ( String args []) {
3 myPrint (5) ;
4 }
5
6 static void myPrint ( double d ) {
7 System . out . println ( d ) ;
8 }
9 }

Legal because parameter transmission is equivalent to assignment


myPrint(5) is same as
double d = 5;
System.out.println(d);

Lecture 5 9 / 24
Polymorphism Overloading

Illegal Method Calls

1 class Example {
2 public static void main ( String args []) {
3 myPrint (5.0) ;
4 }
5
6 static void myPrint ( int i ) {
7 System . out . println ( i ) ;
8 }
9 }

Illegal because parameter transmission is equivalent to assignment


myPrint(5.0) is same as
int i = 5.0; // error
System.out.println(i);
myPrint(int) in Example cannot be applied to (double)

Lecture 5 10 / 24
Polymorphism Overloading

Java Uses the Most Specific Method


When provided with legal parameters, Java uses the most specific method.
See example:
1 class Example {
2 public static void main ( String args []) {
3 myPrint (5) ; // invokes myPrint ( int )
4 myPrint (5.0) ; // invokes myPrint ( double )
5 }
6 static void myPrint ( double d ) {
7 System . out . println ( " double : " + d ) ;
8 }
9 static void myPrint ( int i ) {
10 System . out . println ( " int : " + i ) ;
11 }
12 }

Output
int: 5
double: 5.0
Lecture 5 11 / 24
Polymorphism Overloading

Overloading Constructors (1)

One can “overload” constructors as well as methods


1 class Counter {
2 int count ;
3 Counter () {
4 count = 0;
5 }
6 Counter ( int start ) {
7 count = start ;
8 }
9 }

As shown in the aboved code, constructors can be overloaded to


provide more custom parameters.

Lecture 5 12 / 24
Polymorphism Overloading

Overloading Constructors (2)


One constructor can “call” another constructor in the same class, but it
must follow these rules
Use this keyword to call the constructor
The call must be the very first thing the constructor does
1 class Point {
2 Point ( int x , int y ) {
3 this . x = x ;
4 this . y = y ;
5 }
6 Point () {
7 this (0 , 0) ; // calls Point ( int , int )
8 }
9 }

A common reason for overloading constructors is to provide default


values for missing parameters.

Lecture 5 13 / 24
Polymorphism Overloading

Shadowing
Shadowing is the practice of using variables in overlapping scopes
with the same name where the variable in low-level scope overrides
the variable of high-level scope.
Variables of the parent class have a high-level scope and variables of
its subclasses have a low-level scope.
1 class Animal {
2 String name = " Animal " ;
3 public static void main ( String args []) {
4 Animal animal = new Animal () ;
5 Dog dog = new Dog () ;
6 System . out . println ( animal . name + " " + dog . name ) ;
7 }
8 }
9 public class Dog extends Animal {
10 String name = " Dog " ;
11 }

In the code above, name in class Dog shadows name in class Animal
Lecture 5 14 / 24
Polymorphism Overriding

Overriding (1)
1 class Animal {
2 public static void main ( String args []) {
3 Animal animal = new Animal () ;
4 Dog dog = new Dog () ;
5 animal . print () ;
6 dog . print () ;
7 }
8 void print () {
9 System . out . println ( " Superclass Animal " ) ;
10 }
11 }
12 public class Dog extends Animal {
13 void print () { // this overrides method print in Animal
14 System . out . println ( " Subclass Dog " ) ;
15 }
16 }

A subclass variable can shadow a superclass variable, but a subclass


method can override a superclass method
Lecture 5 15 / 24
Polymorphism Overriding

Overriding (2)
To override methods in a superclass create a method in a subclass
having the same signature as a method in a superclass
Thus, create a method in a subclass having the same name and the
same number and types of parameters
1 class Animal {
2 void do ( String place ) {
3 System . out . println ( " Move on / in " + place ) ;
4 }
5 }
6 public class Bird extends Animal {
7 void do ( String area ) { // overrides do in Animal
8 System . out . println ( " Fly in the " + area ) ;
9 }
10 }

Parameter names do not matter, just their types.


The return type must be the same
Lecture 5 16 / 24
Polymorphism Overriding

Overriding (3)
super keyword

The super keyword is used when calling a superclass version of an


overridden method.
1 class Animal {
2 void do ( String place ) {
3 System . out . println ( " Move on / in " + place ) ;
4 }
5 }
6 public class Bird extends Animal {
7 void do ( String area ) { // overrides do in Animal
8 super . do ( " land " ) ; // invokes do from parent
9 System . out . println ( " Fly in the " + area ) ;
10 }
11 }

Lecture 5 17 / 24
Polymorphism Overriding

Overriding (4)
@Override annotation

@Override annotation is used when we override a method in subclass.


1 class Animal {
2 void makeSound () {
3 System . out . println ( " Animal - Noise " ) ;
4 }
5 }
6 public class Duck extends Animal {
7 @Override
8 void makeSound () { // overrides makeSound in Animal
9 System . out . println ( " Duck - Quack " ) ;
10 }
11 }

@Override annotation is considered as a best practice for coding because:


a syntax error in the signature while overriding results in a compile
time error. Otherwise, it creates a new method of a subclass.
it improves the readability of the code.
Lecture 5 18 / 24
Abstraction Abstract classes and methods

Abstraction (1)
Abstract classes and methods

Data abstraction is the process of hiding certain details and showing


only essential information to the user.
Abstraction can be implemented using abstract classes or interfaces.
The abstract keyword is a non-access modifier, used for classes and
methods:
◦ Abstract class: is a restricted class that cannot be used to create
objects (to access it, it must be inherited from another class).
◦ Abstract method: can only be used in an abstract class, and it does
not have a body. The body is provided by the subclass.
An abstract class can have both abstract and regular methods:
1 abstract class Animal {
2 public abstract void animalSound () ; // notice syntax
3 public void sleep () {
4 System . out . println ( " Zzz " ) ;
5 }
6 }

Lecture 5 19 / 24
Abstraction Abstract classes and methods

Abstraction (2)
Abstract classes and methods

From the example in the previous slide, it is not possible to create an


object of the Animal class:
1 Animal anim = new Animal () ; // error

To access the abstract class, it must be inherited from another class.


1 class Pig extends Animal {
2 public void animalSound () {
3 System . out . println ( " Wee wee " ) ;
4 }
5 }
6 class Main {
7 public static void main ( String [] args ) {
8 Pig myPig = new Pig () ; // object created , ok
9 myPig . animalSound () ;
10 myPig . sleep () ;
11 }
12 }

Lecture 5 20 / 24
Abstraction Interfaces

Abstraction (3)
Interfaces

An interface is a completely “abstract class” that is used to group


related methods with empty bodies:
1 interface Animal {
2 public void animalSound () ; // method with no body
3 public void sleep () ; // method with no body
4 }
The interface must be ”implemented” by another class with the
implements keyword in order to access the interface methods. The
body of the interface method is provided by the ”implement” class:
1 class Duck implements Animal { // must override all
2 public void animalSound () {
3 System . out . println ( " Quack quack " ) ;
4 }
5 public void sleep () {
6 System . out . println ( " Zzz " ) ;
7 }
8 }
Lecture 5 21 / 24
Abstraction Interfaces

Abstraction (4)
Interfaces – Multiple inheritance

Recall, Java does not support multiple inheritance. However, it can


be achieved with interfaces as shown below:
1 interface Carnivore {
2 public void eatMeat () ; // interface method
3 }
4 interface Herbivore {
5 public void eatPlant () ; // interface method
6 }
7 class Omnivore implements Carnivore , Herbivore {
8 public void eatMeat () {
9 System . out . println ( " Let ’s eat some steak ! " ) ;
10 }
11 public void eatPlant () {
12 System . out . println ( " Let ’s eat some broccoli ! " ) ;
13 }
14 }

Class Omnivore has “inherited” from classes Herbivore and Carnivore


Lecture 5 22 / 24
Abstraction Interfaces

Abstraction (4)
Interfaces – Important notes

Interfaces cannot be used to create objects.


Interface methods do not have a body – the body is provided by the
“implement” class
An implementing class must override all the interface’s methods
Interface methods are by default abstract and public
Interface attributes are by default public, static and final
An interface cannot contain a constructor

Why use interfaces?


To achieve security
To implement multiple inheritance

Lecture 5 23 / 24
Abstraction Interfaces

practice

Lecture 5 24 / 24

You might also like