Oop Lecture 07
Oop Lecture 07
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.
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
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
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
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 }
Lecture 5 9 / 24
Polymorphism Overloading
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 }
Lecture 5 10 / 24
Polymorphism Overloading
Output
int: 5
double: 5.0
Lecture 5 11 / 24
Polymorphism Overloading
Lecture 5 12 / 24
Polymorphism Overloading
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 }
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 }
Overriding (3)
super keyword
Lecture 5 17 / 24
Polymorphism Overriding
Overriding (4)
@Override annotation
Abstraction (1)
Abstract classes and methods
Lecture 5 19 / 24
Abstraction Abstract classes and methods
Abstraction (2)
Abstract classes and methods
Lecture 5 20 / 24
Abstraction Interfaces
Abstraction (3)
Interfaces
Abstraction (4)
Interfaces – Multiple inheritance
Abstraction (4)
Interfaces – Important notes
Lecture 5 23 / 24
Abstraction Interfaces
practice
Lecture 5 24 / 24