Link to home
Start Free TrialLog in
Avatar of dkim18
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();
    }
}
Avatar of Am P
Am P
Flag of India image

Overriding depends on having an instance of a class. The point of polymorphism is that you can subclass a class and the objects implementing those subclasses will have different behaviors for the same methods defined in the superclass (and overridden in the subclasses). A static method is not associated with any instance of a class so the concept is not applicable.


Refer : http://geekexplains.blogspot.in/2008/06/can-you-override-static-methods-in-java.html
Avatar of dkim18
dkim18

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?
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");

			
		
         }

   }

}

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of mccarl
mccarl
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial