dkim18
asked on
super and sub classes?
can someone explain these lines to me please?
Why did it cast the myCat with Superclass?
Why hide() was called from the Superclass?
but override() called from the subclass?
Cat myCat = new Cat();
Animal myAnimal = (Animal)myCat;
myAnimal.hide();
myAnimal.override();
http://www.java-tips.org/java-se-tips/java.lang/what-is-a-java-superclass.html
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();
}
}
Why did it cast the myCat with Superclass?
Why hide() was called from the Superclass?
but override() called from the subclass?
Cat myCat = new Cat();
Animal myAnimal = (Animal)myCat;
myAnimal.hide();
myAnimal.override();
http://www.java-tips.org/java-se-tips/java.lang/what-is-a-java-superclass.html
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();
}
}
ASKER
I am really interested on these lines:
Cat myCat = new Cat();
Animal myAnimal = (Animal)myCat;
Why and what situation will you do this?
Cat is the subclass and it is inherited from Animal already.
What was the casting for?
Cat myCat = new Cat();
Animal myAnimal = (Animal)myCat;
Why and what situation will you do this?
Cat is the subclass and it is inherited from Animal already.
What was the casting for?
import java.lang.reflect.Field;
class Animal {
int teeth = 17;
static Animal myAnimal;
static Cat myCat;
public static void main(String[] args){myCat = new Cat();
myAnimal = (Animal)myCat;
System.out.println("Individual called myCat has "+myCat.teeth+" teeth and "+myCat.eyes+" eyes");
System.out.print("Individual myAnimal has "+myAnimal.teeth+" teeth, and ");
try{
try{
Field eyez = Class.forName("Animal").getField("eyes");
}catch(NoSuchFieldException nsfe){System.out.println(" no eyes");}
}catch(ClassNotFoundException cnfe){cnfe.printStackTrace();}
System.out.println();
}
static class Cat extends Animal{
static int eyes = 2;
int teeth = 34;
Cat(){if(this instanceof Cat){System.out.println("I am a Cat ");}if(this instanceof Animal){System.out.println("I am an Animal ");}
System.out.println("As an Animal I have "+super.teeth+" Teeth.");System.out.println("As a Cat I have "+this.teeth+" teeth");
System.out.println("As a Java sub-class I have "+teeth+" teeth");
System.out.println("As a Java superclass I have "+((Animal)this).teeth+" teeth");
}
}
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Refer : http://geekexplains.blogsp