Site icon FahmidasClassroom

Java Inheritance Examples

Title

Inheritance is a very important feature of object oriented programming. The way of creating single and multi-level inheritance has been shown in this tutorial.

Example-1: Single level Inheritance

Create a Java file with the following code that will create a parent class named Student and a child class named StudentDetails. Another class named JavaContructor1 has been used in the code to create object of the child class to access the properties of the child and parent classes.

import java.util.*;

//Define the parent class 
class Student {
    public int id;
	protected String name;
	
	public Student(int s_id, String s_name)
	{
		this.id = s_id;
		this.name = s_name;
	}	
	protected void display_basic()
	{
		System.out.println("Student ID: " + id);
		System.out.println("Student Name: " + name);
	}
}

//Define the child class
class StudentDetails extends Student
{
	public String dept;
	public String[] CourseCode = {"","",""};
	public int[] Marks = {0,0,0};

	public StudentDetails(int s_id, String s_name, String s_dept)
	{
		super(s_id, s_name);
		dept = s_dept;
	}
	public void InsertMarks()
	{
		for(int i=0; i < 3; i++)
		{
			//Define scanner object
			Scanner scanner = new Scanner(System.in);
			//Take the course code and marks
			System.out.print("Enter the course code:");
			this.CourseCode[i] = scanner.next();
			System.out.print("Enter the marks:");
			this.Marks[i] = scanner.nextInt();
		}
	}
	
	protected void display_marks()
	{	
		//Print the marks with the course code
		System.out.println("\nMarks Details:");
		for(int i=0; i < 3; i++)
		{
			System.out.println(CourseCode[i] + ":" + Marks[i]);
		}
	}
}

//Define class to access the properties of the child class
public class JavaConstructor1
{
	public static void main(String[] args) 
	{
		//Create object of the child class
		StudentDetails obj = new StudentDetails(78,"Mehrab Hossain", "CSE");
		obj.InsertMarks();
		obj.display_basic();
		obj.display_marks();
	}
}

Output:

Example-2: Multi-level Inheritance

Create a Java file with the following code that will create a parent class named Employee and a child class of this class named EmpAttendance. Next, create another child class of the EmpAttendance class named EmpDetails. Another class named JavaContructor2 has been used in the code to create object of the last child class to access the properties of the child and parent classes.

import java.util.*;

//Define the parent class 
class Employee {
    public int id;
	public String name, post;
	
	public Employee(int e_id, String e_name, String e_post)
	{
		this.id = e_id;
		this.name = e_name;
		this.post = e_post;
	}	
}

//Define the child class of Employee
class EmpAttendance extends Employee
{
	public String month;
	public int present;

	public EmpAttendance(int e_id, String e_name, String e_post)
	{
		super(e_id, e_name, e_post);
	}
	public void Attendance(String c_month, int p)
	{
		month = c_month;
		present = p;
	}
}
//Define the child class of EmpAttendance
class EmpDetails extends EmpAttendance
{
	public double salary;
	public EmpDetails(int e_id, String e_name, String e_post, double e_salary)
	{
		super(e_id, e_name, e_post);
		salary = e_salary;
	}
	public double Bonus()
	{
		return salary*0.15;
	}
}

//Define class to access the properties of the EmpDetails class
public class JavaConstructor2
{
	public static void main(String[] args) 
	{
		//Create object of the child class
		EmpDetails obj = new EmpDetails(102,"Mila Chowdhury", "CEO", 100000);
		obj.Attendance("August",28);
		obj.salary = Math.round((obj.present*obj.salary)/30 + obj.Bonus());
		System.out.println("ID: " + obj.id);
		System.out.println("Name: " + obj.name);
		System.out.println("Month of salary: " + obj.month);
		System.out.println("Present: " + obj.present);
		System.out.println("Total salary: " + obj.salary);
	}
}

Output:

Excercise:

Create a java class named ‘Department’ that will contain id and name. Create two child classes by inheriting this class named ‘CSE‘ and ‘BBA‘. Both child classes will contain total number of students, total number of faculties, and two methods to add the names of the faculties in a array and display the details of each department.

Exit mobile version