Java Programming | Introduction to Inheritance

Join Programming and Development Chat Groups on Uphub.io

Introduction

This is a tutorial for introduction to inheritance in java. The program is given below that inherits class from baseclass. The program is extendable. Go enjoy the program. Lets begin…………

Before you begin.Do this

  1. Create a separate java file for baseclass.
  2. Create two other java files for child classes of baseclass.
  3. Create a java file for main class.
  4. Import the created java classes in main class file.

Program for inheritance in java.

1.Create baseclass

//declare the package name if the class file is
//created in other package.
package Myclasses;
 
//import scanner
import java.util.Scanner;
 
//declare class
public abstract class BaseClass {
 
    //declare variables.
    public int a,b,result;
 
    //functions should be public if needed to access from other class
    public void getdata()
    {
        //print message to enter numbers
        System.out.println("Enter 2 Numbers:");
        //Take input
        Scanner input = new Scanner(System.in);
        a = input.nextInt();
        b = input.nextInt();
    }
 
    abstract public void calculate();
 
    abstract public void print();
 
}

2.Create child classes

//declare the package name if the class file is
//created in other package.
package Myclasses;
 
//declare class
public class AddClass extends BaseClass{
 
    public void calculate()
    {
        result = a+b;
    }
 
    public void print()
    {
            System.out.println("Sum = "+result);
    }
}
//declare the package name if the class file is
//created in other package.
package Myclasses;
 
//declare class
public class SubtractClass extends BaseClass{
 
    public void calculate()
    {
        result = a-b;
    }
 
    public void print()
    {
            System.out.println("Subtraction = "+result);
    }
}

3.Create main class.

//import our class
import Myclasses.AddClass;
import Myclasses.SubtractClass;
 
// the name of our class its public
public class Inheritance {
    //void main
        public static void main (String[] args)
        {
            //declare object
            AddClass add = new AddClass();
            SubtractClass sub = new SubtractClass();
            //call functions
            add.getdata();
            add.calculate();
            add.print();
            System.out.println("---------------");
            sub.getdata();
            sub.calculate();
            sub.print();
        }
}

Output

Enter 2 Numbers:
1
2
Sum = 3
—————
Enter 2 Numbers:
2
1
Subtraction = 1

How it Works

  1. AddClass object is created.
  2. SubtractClass object is created.
  3. The getdata function is called from main class.
  4. The calculate function is called from main class.
  5. The print function is called from main class.

Extending it

The program can be extended by using the concept in any program. Like for ex. you can create a same program for multiplication,division,etc. Go ahead extend it……

  • Remember this is a basic part of java which can be used in any program….

Explanation.

Your Baseclass

  1. Create class.
  2. Import the Scanner.
  3. Declare the class as public abstract.
  4. Declare variable.
  5. Create functions.

Your Childclass

  1. Create class.
  2. Declare the class as public that extends Baseclass.
  3. Declare variable.
  4. Create functions.

Main class

  1. Create other class
  2. Import your Child classes.
  3. Declare the class as public.
  4. Add the void main function.
  5. Declare class.
  6. Call functions.

At the end.

You learnt creating the Java program for Introduction to Inheritance . So now enjoy the program.

Please comment on the post and share it.

Sharing options: