Chapter Five
Chapter Five
POLYMORPHISM
AND
INTERFACES
3/1/2020
Polymorphism
2
2. Late/dynamic Binding/Overriding
Binding that takes place at a runtime
3/1/2020
Overloading
class Test {
public static void main(String args[]) {
myPrint(5);
myPrint(5.0);
}
static void myPrint(int i) {
System.out.println("int i = " + i);
}
static void myPrint(double d) { // same name, different parameters
System.out.println("double d = " + d);
}
}
int i = 5
double d = 5.0
3
Overriding
This is called
class Animal {
public static void main(String args[]) { overriding a method
Animal animal = new Animal();
Method print in Dog
Dog dog = new Dog();
animal.print(); overrides method
dog.print(); print in Animal
}
void print() { A subclass variable
System.out.println("Superclass Animal"); can shadow a
} superclass variable,
}
but a subclass
public class Dog extends Animal {
void print() {
method can
System.out.println("Subclass Dog"); override a
} Superclass Animal superclass method
} Subclass Dog
4
Example
5
3/1/2020
6
C1 C2 C3
3/1/2020
Example
7
3/1/2020
Abstract class
8
3/1/2020
Example
9
3/1/2020
Abstract Method
10
3/1/2020
Example
11
3/1/2020
Interface
12
3/1/2020
Interface: Syntax
13
interface is a reserved word
A semicolon immediately
follows each method header
No method in an
interface has a definition (body)
Conti…
14
3/1/2020
Example
15
interface Shape{
String baseClass=“Shape”;
public void draw();
}
public class Circle implements Shape{
public void draw(){
Syste.out.println(“Drawing Circle Here”);
}}
public class Interfacedemo{
public static void main(String [] args){
Shape CircleShape=new Circle();
CircleShape.draw();
}
}
3/1/2020
Abstract class Versus Interface
16
3/1/2020
Chapter Six
17
EXCEPTION
HANDLING
3/1/2020
Exception Handling
18
3/1/2020
Types Of Errors
19
3/1/2020
Types of Exceptions
20
ClassNotFoundException
IOException
ArithmeticException
Exception AWTException
NullPointerException
RuntimeException
IndexOutOfBoundsException
Object Throwable Several more classes
IllegalArgumentException
VirtualMachineError
Error
AWTError
System.out.println(“Enter a Numerator”);
n=input.nextInt();
System.out.println(“Enter a Denominator”);
d=input.nextInt();
r=n/d;
System.out.println(n+”/”+d+”=”+r);
}
}
3/1/2020
Try…Catch Mechanism
22
Syntax:
try{
//Statement that generate an exception
}
catch(Exception someException)
{
//action to be taken if Exception occurs
}
3/1/2020
Example
23
}
}
3/1/2020
Notes
24
3/1/2020
Example
25
3/1/2020
Note
26
3/1/2020
Example
27
3/1/2020
Finally Block
28
3/1/2020
Example Write output
29
public class Division { catch(Exception E){
int a=5; System.out.println("Division Error");
int b=0; }
public void Java(){
finally{
System.out.println(“Finally Block");
}
System.out.println(“Finally Block”);
public void Division() }
{ }}
System.out.println(a/b); Public class Test{
}} public static void main(String[] args)
class A extends Division{ {
public void Division(){ A b=new A();
try{ super.b=5; System.out.println("Your Output Must
super.Division(); Be Correct!");
System.out.println(a*b); b.Division();
} }}
3/1/2020
Last Quiz
30
public class AA { public class Quiz2 {
public int Max(int a,int b){ public static void main(String[] args)
if(a>b){ {
return a; } CC c= new CC();
else { System.out.println("I Miss You");
return b; } System.out.println(c.Max(10, 15));
} } }}
Public class BB extends AA
{ public int Max(int a,int b){
System.out.println("I Miss You");
return super.Max(a+7, b);
}}
Public class CC extends BB
{ public int Max(int a,int b){
return (super.Max(a, b))+10;
}}
3/1/2020
Thank you!