login
Home / Papers / Java Programming

Java Programming

36 Citations•2009•
Zheng-Liang Lu
journal unavailable

JVM invokes the appropriate method for the current object by looking up from the bottom of the class hierarchy to the top by preserving the behaviors of the subtype objects and the super-type variables play the role of placeholder.

Abstract

ion, Method Overriding, and Polymorphism • JVM invokes the appropriate method for the current object by looking up from the bottom of the class hierarchy to the top. • These methods are also called virtual methods. • This preserves the behaviors of the subtype objects and the super-type variables play the role of placeholder. • We often manipulate objects in an abstract level; we don’t need to know the details when we use them. • For example, computers, cellphones, driving. Zheng-Liang Lu Java Programming 267 / 291 Exercise • Imagine that we have a zoo with some animals. 1 class Animal { 2 void speak() {} 3 } 4 class Dog extends Animal { 5 void speak() { System.out.println("woof"); } 6 } 7 class Cat extends Animal { 8 void speak() { System.out.println("meow"); } 9 } 10 class Bird extends Animal { 11 void speak() { System.out.println("tweet"); } 12 } 13 14 public class PolymorphismDemo { 15 public static void main(String[] args) { 16 Animal[] zoo = {new Dog(), new Cat(), new Bird()}; 17 for (Animal a: zoo) a.speak(); 18 } 19 } Zheng-Liang Lu Java Programming 268 / 291