Transcripted Summary

Java is an object-oriented programming language. As we've seen in the past few chapters, we use the paradigm of objects to represent a concept, and to hold state and behavior.



There are four major principles of object-oriented programming:

  • encapsulation (which we learned about in chapter 6)
  • inheritance
  • polymorphism
  • abstraction

In this chapter, we will focus solely on inheritance.

Inheritance

Inheritance is where one class becomes an extension of another class, therefore inheriting the members of that class.

There are two parties involved in an inheritance — a parent and a child. The parent is known as the superclass, or sometimes also referred to as the parent class or base class. The child is known as the subclass, or sometimes referred to as the child class or the derived class.

When an inheritance relationship is created between these two classes, then the child class inherits the members of the parent class.

This allows classes to reuse data that already exists within other classes.

Subclasses should be a more specialized form of the superclass that they inherit from.

Let's look at an example where we have a class called Person.



This class can contain fields for name, age, and gender. If we later decided to create an Employee class, then the Employee would also need a name, age, and gender.

Instead of defining all of that all over again, we could have the Employee class inherit from the Person class.

When this happens, the Employee class now has the members of the Person class, and the programmer can focus on only defining things that are specific to the Employee class.

An inheritance relationship is known as a “is a” relationship. An Employee is a Person — and being a subclass, it's a specialized type of Person.

Let's demonstrate inheritance with code.

I'm going to create a new class in a chapter9 package. We'll call this Person and let's say that a Person has a name, an age, and a gender.

public class Person {

    private String name;
    private int age;
    private String gender;
}

Now, let's make getter and setter methods for each of these.



Time-Saving Tip

A little trick: we can right-click and use this Generate menu to create Getter and Setter methods.

I select all three fields to have getter and setter methods generated for them.



Now we have these created.


# Person.java

package chapter9;

public class Person {

    private String name;
    private int age;
    private String gender;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

}

Now let's create the Employee class which will inherit from Person.

To inherit from another class, you use the keyword extends within the subclasses header.

extends forms an inheritance relationship. After extends, you name the class that you want to inherit from. So, we're inherited from the Person class.

public class Employee extends Person {

}

That's all it takes to create an inheritance relationship.

With this, now the Employee class has access to all of the methods that we created in the Person class.


In addition to what's inherited, the Employee class can have additional things that are specific to an Employee.

So, as the programmer, now I don't have to worry about setting the name or the age or the gender. I can focus on things specific to Employee, such as maybe the employee ID and the employee's title.

public class Employee extends Person {

    private String employeeId;
    private String title;
}

We’ll use the Generate Setter and Getter trick again to create the methods.


# Employee.java

package chapter9;

public class Employee extends Person {

    private String employeeId;
    private String title;

    public String getEmployeeId() {
        return employeeId;
    }

    public void setEmployeeId(String employeeId) {
        this.employeeId = employeeId;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }
}

So, now we have the Person class, which is serving as the superclass, and the Employee class, which is serving as the subclass.

There's nothing that I need to do to the Person class to make this inheritance relationship. That is only done in the subclass with this keyword, extends.

Now let's see this in action.


I'm going to open this InheritanceTester class that I have.

Let's first just create a Person object. Notice what's available to a Person object.



All of the methods that we defined in that Person class are there as expected.

If I create an Employee object, not only do I get the getter and setter methods for that existed in the Person class, but now I also get the new methods that were defined in the Employee class.



So, that is inheritance.

Even though I did not specify a setName() method or getAge() method in the Employee class, I have access to it because it was inherited from Person.

# Employee.java

public class InheritanceTester{

    public static void main(String[] args){

        Person person = new Person();
        Employee employee = new Employee();
    }
}



Resources



© 2024 Applitools. All rights reserved. Terms and Conditions Privacy Policy GDPR