Java .

Java Inheritance

extending classes

class Cat {
   public String className;
   public String name;
 
   public Cat() 
   {
      className = "Cat";
      name = "no name in";
   }
   public Cat(String nameIn) 
   {
      className = "Cat";
      name = nameIn;
   }

public String getName() { return(name + " the " + className); } }

class Himalayan extends Cat{

public Himalayan() { className = "Himalayan"; }

public Himalayan(String nameIn) { className = "Himalayan"; name = nameIn; }

public static void main(String[] args) {

Cat percy = new Cat("Percy"); Himalayan cappuccino = new Himalayan("Cappuccino"); System.out.println(percy.getName()); //output is: Percy the Cat System.out.println(cappuccino.getName()); //output is: Cappuccino the Himalayan } }
Comments
Sign in to be the first to comment on Java Inheritance.