Skip to main content

Programming in Java Netbeans - A Step by Step Tutorial for Beginners: Lesson 30

Danson Wachira is a certified Trainer in Computer Science, Information Technology and related studies.

Table of Contents

<< Lesson 29 | Lesson 31 >>

Lesson 30: Java and Inheritance

Inheritance is one important aspect of Object Oriented Programming. Inheritance is having one class as a parent class (also called a super class) and another class as a child of the parent (also called a sub class).

The child class is called a derived class because it is derived from the parent class. The main use of a child class in an application is to separate classes’ information. The child class can inherit all the methods and variables of its parent class but can also have its own methods and variables.


What is Inheritance in Object Oriented Programming?

To explain the concept of inheritance among classes, let us have class employee and manager. Class manager inherits from class employee if its objects (methods and variables) form a subset of employee's objects. Class manager objects must support all of class employee's operations.

Class manager may support additional operations and an extended state (i.e. more information fields). Class manager is called a sub class, child class, heir class or derived class. Class employee is called a super class, parent or base class.

How to implement inheritance in Java

Using the Java program we had in Lesson 29, we’ll learn the concept of inheritance by creating a sub class that handles information about certificates. If the student gets a "Distinction" grade, we'll award the student with a Certificate of Excellence; if the student gets a "Credit" or a "Pass" we'll award a Certificate of Achievement. However, if the student gets a “Fail” grade, no certificate will be awarded.

The reason why we need to create a sub class is to keep the certificate information separate from the examination information. By doing so, we are not limiting the access of examination details. The visibility to examination data can still be achieved as we can access examination variables and methods from the sub class.

In the examination project we had in Lesson 29, create another class, you can call it Certificate.


To create a sub class from a super class in Java NetBeans, we have to use the keyword “extends.” This keyword is then followed by the class name we want to inherit from or if you like, the class we want to extend. We want the sub class “Certificate” to inherit from the super class “ExamDetails.” The inheritance statement should look like shown below:

Public class Certificate extends ExamDetails{


We also need a string variable in the Certificate class to store the certificate information. The Certificate class should now look like shown:

Scroll to Continue

From the above image, we have now created a sub class that inherits from the super class ExamDetails. What that means is that we can now access the variables and methods in ExamDetails beside having additional methods and variable in the Certificate sub class.

Just like we did in ExamDetails class, we can set up a constructor to initialize the default values in the Certificate class. When Java executes, it calls the constructor first. We need to be careful here not to overwrite the default values we set in the ExamDetails class.

To avoid overwriting the default values we set in the ExamDetails class, we are going to initialize the default values of the ExamDetails class first before we initialize the default values of the Certificate class. In that case, we are are going to use the statement super() in the constructor as shown below:


Up to this point, our Certificate sub class has no functional method. We are going to add a method that will award specific certificate to student depending on the grade. To get student grade, we’ll call the method ExamGrade in the parent class i.e. ExamDetails to return the grade. Add the following method into Certificate sub class.

package examination;

public class Certificate extends ExamDetails {
    private String certInfo;
        certInfo = "No certificate";
String AwardCertificate(int Escore){
    String grade = ExamGrade(Escore);
    if(grade.equals("Grade: DISTINCTION")){
        this.certInfo = "Certificate of Excellence";
    else if(grade.equals("Grade: CREDIT")||grade.equals("Grade: PASS")){
        this.certInfo = "Certificate of Achievement";
        this.certInfo = "No Certificate awarded";
    return this.certInfo;

Notice sub class Certificate is receiving an integer values, Escore, which is consequently passed to ExamGrade method to return the grade. The Certificate sub class then uses the grade to return the certificate awarded to a student. Now, in the main class, Examination class, create an object of the Certificate sub class using the following statement:

Certificate studentCert = new Certificate();

We are calling the object studentCert, We’ll use this object to access the method of the Certificate sub class which will return the awarded certificate. Modify the main class so that it will look as shown:


In the above image, we have included two more statements, one to award the certificate and another to display the certificate information on the console.

String cert = studentCert.AwardCertificate(56);


We have used inheritance to separate the ExamDetails super class information from the Certificate sub class information yet, we can still access methods in the parent class. By doing so, we have extended the usefulness of the parent class. In the next lesson, we shall look at types of programming errors.

<< Lesson 29 | Lesson 31 >>

Want to make money online?

Related Articles