Home » Java » Inheritance

Category Archives: Inheritance

Inheritance

Inheritance

Topics:    What is an array?

Super keyword

This keyword

getName() & getClass()

What is inheritance?

In Java, a class can inherit all methods, instance variables etc… from another by extending it. Java allows the keyword extends when dealing with inheritance. Here is how to extend a class from another:

public class Wheels extends Car{
	//code here
}

where the class Wheels will now be the subclass and the class Car will be the super class. In “human terms” this is true since a wheel is part of a larger being called the car.

super keyword

The keyword super will be used when referring to the super class of an object. This will reference the constructor when used like this for an example:

super( argument(s) )

or if accessing another method:

super.methodName( argument(s) )

What is NOT inherited from the super class is the constructors, since they are strictly used by that class.

this keyword

The keyword this will reference the current class the word appears in. It will allow you to use the classes methods if used like this:

this.methodName();

Let’s see a simple example of some inheritance for an Animal class.

Example 1:
Simple array example

public class Dog extends Animal{
	public Dog(String n, int a){
		super(n,a);
	}
}

public class Cat extends Animal{
	public Cat(String n, int a){
		super(n,a);
	}
	public String toString(){
		return "I am a " + getClass().getName();
	}
}

public class Animal{
  static Animal animals[] = new Animal[5];
  String name = "";
  int age = 0;
  static int ct = 0;

  public Animal(String n, int a){
  name = n;
  age = a;
	}

  public String toString(){
   return "I am a " + getClass().getName() +
        ".  My name is " + name + ".  I am "
            + age + " years old!";
	}

  public static void main(String args[]){

    for(int i = 0; i < 5; i++){
         if(i % 2 == 0){
	    animals[i] = new Cat("Fluffy", i+5);
	      }else{
		   animals[i] = new Dog("Jake", i+3);
		   }
		}
    for(int i = 0; i < 5; i++){
	System.out.println(animals[i]);
		}
	}
} //class

Again, a lot has happened here in just a short program. Lets break it down bit by bit.

First, the main Animal class. What will happen here is an array of Animal objects called animals[] will hold information for 5 Animals that will be created in the main method. Each of those animals are either a Dog or Cat object.

Second, both the Dog and Cat class are the subclass of Animal. The constructor of the Dog class will call the Animal constructor and create each Object. The Cat constructor will do the same. However, in the Cat class, there is a toString method that will OVERRIDE the ANIMAL toString method. This is important for later use.

Finally, the Animal class will create the objects of either Dog or Cat based on an if statement in the first for loop. When the last for loop occurs, the output will be:

I am a Cat
I am a Dog.  My name is Jake.  I am 4 years old!
I am a Cat
I am a Dog.  My name is Jake.  I am 6 years old!
I am a Cat

 

getClass() & getName() method

When referring to classes, there is a method called getClass() that will return the current class it is called in. So in the above example, the keyword this is used to refer directly to the Animal or Cat class but that can be left out.

Inside the getClass() method is another method called getName(). This will return a String representing the name of that class. The previous example shows this in both the Animal and Cat classes in the second example.

When dealing with inheritance, do not be frightened. The first thing to note is what class will be the super class. Then, look for methods that are overridden from the super class. Finally, observe the super class and the main method to trace the output. Inheritance takes practice so please be patient.