Casting Objects : Cast « Data Type « Java Tutorial






class Animal {
  public String toString() {
    return "This is an animal ";
  }
}
class Dog extends Animal {
  public void sound() {
    System.out.println("Woof Woof");
  }
}
class Cat extends Animal {
  public Cat(String aName) {
    name = aName;
  }
  public String toString() {
    return super.toString() + "\nIt's " + name + " the " + breed;
  }
  public void sound() {
    System.out.println("Miiaooww");
  }
  private String name;
  private String breed;
}
public class MainClass {
  public static void main(String[] a) {
    Cat aPet = new Cat("Fang");
    Animal theAnimal = (Animal) aPet; 
  }
}








2.15.Cast
2.15.1.Casting Objects
2.15.2.Convert an integer into binary, hexadecimal, and octal.