Saturday, January 05, 2008

What is a Java superclass

A Java superclass is a class which gives a method or methods to a Java subclass.

A Java class may be either a subclass, a superclass, both, or neither!

The Cat class in the following example is the subclass and the Animal class is the superclass.

public class Animal {

public static void hide() {
System.out.println("The hide method in Animal.");
}

public void override() {
System.out.println("The override method in Animal.");
}
}

public class Cat extends Animal {

public static void hide() {
System.out.println("The hide method in Cat.");
}

public void override() {
System.out.println("The override method in Cat.");
}

public static void main(String[] args) {
Cat myCat = new Cat();
Animal myAnimal = (Animal)myCat;
myAnimal.hide();
myAnimal.override();
}
}

No comments: