Skip to content

Inheritance

trekitch edited this page Apr 22, 2017 · 12 revisions

What is Inheritance

Inheritance is the concept that one class can receive all of the behaviors and attributes of another class. This is helpful because instead of making multiple classes that may share or use the same attributes you can have one parent class that passes on its attributes to a child class as necessary. This can be seen when you create objects from classes. Based on what constructors you have created the object will inherit those data fields.

For example, let's say wee have a Student class. The student class has attributes such as stuId, name, and age. Obviously, every student in a school would have these attributes. Now let's say we have a Senior class. This class has the same attributes as the student class except it has an extra gradDate attribute which will hold a senior student's graduation date. Instead of creating separate attributes for the Senior we could make the Senior class a child of the Student class. This way we can pass the common attribute to the senior class without having the create new ones.

In terms of inheritance, the parent class is the class that all of the inheritance will be derived from. This is also known as the base class or the superclass. The child class, or the derived or subclass, is the class the inherits from the parent class.

Extending Classes

In order to create an inheritance, you must use the extends keyword. Let's say for example I have a Worker class that I want to be the parent of LineWorker class. This may be because the worker class may be the more general class and the LineWorker class may be more specific.

public class LineWorker extends Worker
{
   public int LineNumber;
   public int getLine()
   {
      return LineNumber;
   }
   public void setLineNumber(int LnNumber)
   {
      LineNumber = LnNumber;
   }
} 

In this example the extends keyword is telling the program that the LineWorker class is an extension of the Worker class. This allows the LineWorker class to access fields and methods that may be in the Worker class such as EmpID or Name. One thing to note is that inheritance is a one-way street. A child can inherit from a parent but a parent can never inherit from a child. So while the LineWorker class can inherit EmpID and Name from the Worker class, the worker class will never be able to inherit LineNumber from the LineWorker class. It is also import to note that you always want to make the parent class before you make the child class.

Using Superclass constructors

While using super and subclasses you may run into situations where you may need to use constructors. Sometimes you may even need to use constructors that require arguments. Let's say for example you have a Student superclass that has a constructor that requires are a string, an int and a double. Let's also say that you have a Freshman subclass.

public Freshman()
{
   super("New", 12, 2.65);
   //rest of the statements go here
}

The super keyword calls the super class constructor and set the specified values for it. The super keyword line must also be the first line in any subclass constructor. Also, note that super is just another way to reference the parent class. Recall that super and parent, when it comes to inheritance, are synonymous with each other.

Overriding a Parent Class' Methods

Overriding is when you use the child classes version of the method instead of the parent classes version. This can be helpful when you have the data fields inherited from the parent class but you do not need to use them in the same way as the parent methods did. An example of this would be if we had a Worker class that had a rateOfPay() method that displays how much the worker gets paid. If you had a child class of this method called NewWorker then it uses the rateOfPat() method differently. By overriding this method we can specify how the child class will use this method. To override the parent method we will need to give each child class its own rateOfPay() method. This way that when we call the object of the class the appropriate method is used. On thing to note is that there is a thin line between overriding a method and overloading a method. Overloading a method happens when you have a method with the same name but a different set of parameters as the parent class.

class Worker
{
 public  void rateOfPay()
 {
   System.out.println("This worker is paid $12 an hour");
 }
}

class NewWorker extends Worker
{
  public void rateOfPay()
  {
    System.out.println("This worker is paid $8 an hour");
  }
}

class WorkerTest
{
  public static void main(String args[])
  {
    Worker firstGuy = new Worker(); //worker object but using worker class
    Worker secondGuy = new NewWorker(); //worker object but using the NewWorker class

   firstGuy.rateOfPay();
   secondGuy.rateOfPay();
  }
}

Output

This worker is paid $12 an hour
This worker is paid $8 an hour

Using the @Override operator

To further help with overrides you should use the @Override tag. This tells the compiler that you have the intentions to use the method below to override the parent classes method. It also issues a warning if you do not.

@Override
public void rateOfPay()
{
   System.out.println("Your rate of pay is: " + payRate);
}

It is also very important to not that subclass can not override methods that:

  • are static

If you were to make a method called rateOfPay and make it static then make another method of the same name that is non-static then the static method would not be overridden because it takes precedence.

  • are final methods

Final methods can have one and only one value and cannot be overwritten, much like final variables.

  • within final methods

When you declare a class to be final then all of its methods automatically become final as well.

Clone this wiki locally