Inheritance




Inheritance is the concept of allowing a class to extend another class or abstract class. It allows us to inherit all of the methods and variables in another class as if we defined them ourselves in our class(but we do not inherit constructors). The class that we extend is called our super class. We can use the keyword super to access a super method if we have defined our own method with the same exact name and parameters (i.e. if we overrode a super method). We can also use the keyword super to call a constructor in the extended class.

Examples:


// this is a comment
// comments are ignored by the compiler (translater)

public class Sample11 {
        
    public Sample11() {
    }
    
    public static void main(String[] args) {
        
        
        // this is the title
        System.out.println("Inheritance");
        System.out.println();
        System.out.println();
        

        Cat cat = new Cat("Cat", "Top Cat", 9);
        
        Animal dog = new Dog("Dog", "Dawg", 12);

        System.out.println("I am a "+cat.getType());
        System.out.println("My name is "+cat.getName());
        System.out.println("I am "+cat.getAge()+" years old.");
        System.out.println(cat.makeSound());
        System.out.println();

        System.out.println("I am a "+dog.getType());
        System.out.println("My name is "+dog.getName());
        System.out.println("I am "+dog.getAge()+" years old.");
        System.out.println(dog.makeSound());

        System.out.println();
        System.out.println();                
        
    }
}


// Base Class for all animals
// We define all instance variables that all
// animals would have and all common methods
class Animal
{
    String type;
    String name;
    int    age;
        
    // Animal constructor to initialize instance variables
    public Animal(String type, String name, int age)    
    {
        this.type = type;
        this.name = name;
        this.age  = age;    
    }
    
    public String getType()
    {
        return type;
    }

    public String getName()
    {
        return name;
    }

    public int getAge()
    {
        return age;
    }
    
    public String makeSound()
    {
        return "I don't know what to say";
    }   
}

// we define a subclass of Animal to represent a Cat
// we add new instance variables that are unique to
// a cat and appropriate methods that are unique to 
// a cat
class Cat extends Animal
{

    
    // Cat constructor to initialize instance variables
    public Cat(String type, String name, int age)
    {
        super(type, name, age); 
    }
    
    public String makeSound()
    {
        return "meow";
    }   
 
}

// we define a subclass of Animal to represent a Dog
// we add new instance variables that are unique to
// a dog and appropriate methods that are unique to 
// a dog

class Dog extends Animal
{
    
    // Dog constructor to initialize instance variables
    public Dog(String type, String name, int age)
    {
        super(type, name, age); 
    }
    
    public String makeSound()
    {
        return "bark, bark";
    }   
    
}