You are on page 1of 2

IT1908

POLYMORPHISM o Downcasting is casting from superclass to a subclass.


public class Animal { }
Fundamentals public class Cat extends Animal {
• Polymorphism allows performing of a single action in public static void main(String[] args) {
different ways. Animal a = new Cat();
• Types of Polymorphism: Cat c = (Cat)a;
o Static – Flow of control is decided during compile time. It }
is achieved through method overloading. • Rules in casting objects:
public void draw(String s) { } 1. The compiler will not allow casts to unrelated types.
public void draw(int i) { } public class Dog { }
public void draw(int i, double f) { } public class Cat {
o Dynamic – Flow of control is decided during runtime. It public static void main(String[] args) {
is achieved through method overriding. Cat c = new Cat();
public class Class1 { Dog d = (Dog)c; //does not compile
public String message() { }
return "Method"; Explanation: There is no inheritance between the two (2)
} classes.
} 2. Even when the code compiles without issue, an exception
public class Class2 extends Class1 { may be thrown at runtime if the object being cast is not
public String message() { actually an instance of that class.
return super.message() + " Overriding"; public class Animal { }
} public class Cat extends Animal {
public static void main(String[] args) { public static void main(String[] args) {
System.out.print(new Class2().message()); Animal a = new Animal();
} Cat c = (Cat)a;
} //Throws ClassCastException at runtime
}
Object Casting Explanation: Object a is not an instance of Cat.
• You can change the existing type of an object reference to
another type through casting. Rules in Overriding Methods
o Upcasting is casting from a subclass to a superclass. 1. The method in the child class must have the same signature
public class Animal { } as the method in the parent class.
public class Cat extends Animal { public class ClassA {
public static void main(String[] args) { public String message() {
Cat c = new Cat(); return "Method";
Animal a = c; }
} }
}

09 Handout 1 *Property of STI


 student.feedback@sti.edu Page 1 of 2
IT1908

public class ClassB extends ClassA { //Example 2


public String message() { public class ClassA {
return " Overriding"; public String message() throws ArithmeticException {
} return "Method";
} }
2. The method in the child class must be at least as accessible }
or more accessible than the method in the parent class. public class ClassB extends ClassA {
public class ClassA { public String message() throws Exception {
protected String message() { return " Overriding";
return "Method"; }
} }
} Explanation: The message() method in the subclass throws a
public class ClassB extends ClassA { broader exception than message() in the superclass.
private String message() { //does not compile
return " Overriding";
} Reference:
Oracle Docs (n.d.). Citing sources. Retrieved from
}
https://docs.oracle.com/javase/tutorial/java/javaOO/index.html
Explanation: The message() method is marked protected in
the superclass and private in the subclass. To resolve this
issue, message() in the subclass should be marked public or
protected.
3. The method in the child class may not throw a checked
exception that is new or broader than the class of any
exception thrown in the parent class method.
//Example 1
public class ClassA {
public String message() {
return "Method";
}
}
public class ClassB extends ClassA {
public String message() throws Exception {
return " Overriding";
}
}
Explanation: The message() method in the superclass
doesn't throw an exception.

09 Handout 1 *Property of STI


 student.feedback@sti.edu Page 2 of 2

You might also like