Info-ScoopInfo-Scoop
Home
Articles
FLL Robotics
SponsorsOur Achievements
Facts
Space FactsTechnology FactsAnimal FactsShort ReadsFood FactsGeography FactsLanguage Facts
Hobbies
Raspberry PiSketches
Coding
Python ProjectsJava Projects
About
Quizzes
Books
Tongue Twisters
Sign In
This is a new site! Contact us if there are bugs

Contact Us

mail.infoscoop@gmail.com

© Info-Scoop 2026
← Back to Java Projects

Java Inheritance

About This Project

This project demonstrates inheritance in Java. A base class called Office stores common employee details, and two subclasses — Teaching and Non_teaching — inherit those fields and add their own designation.

Features

  • Base class Office with employee number, name, and salary
  • Subclasses Teaching and Non_teaching inherit from Office
  • setData() reads employee details from the user
  • getData() prints the stored employee details
  • Two objects created and displayed in main()

Source Code

import java.util.Scanner;

class Office
{
    int empNo;
    String empName;
    int salary;
}
class Teaching extends Office
{
    String designation;

    void setData()
    {
        Scanner sc = new Scanner(System.in);
        
        System.out.println("Enter Employee Number: ");
        empNo = sc.nextInt();

        System.out.println("Enter Employee Name: ");
        empName = sc.next();

        System.out.println("Enter Employee Salary: ");
        salary = sc.nextInt();

        System.out.println("Enter Employee Designation: ");
        designation = sc.next();
    }
    void getData()
    {
        System.out.println("Employee Name: " + empName + "\t\tEmployee Number: " + empNo);
        System.out.println("Employee Salary: " + salary + "\t\tEmployee Designation: " + designation);
    }
}

class Non_teaching extends Office
{
    String designation;

    void setData()
    {
        Scanner sc = new Scanner(System.in);
        
        System.out.println("Enter Employee Number: ");
        empNo = sc.nextInt();

        System.out.println("Enter Employee Name: ");
        empName = sc.next();

        System.out.println("Enter Employee Salary: ");
        salary = sc.nextInt();

        System.out.println("Enter Employee Designation: ");
        designation = sc.next();
    }
    void getData()
    {
        System.out.println("Employee Name: " + empName + "\t\tEmployee Number: " + empNo);
        System.out.println("Employee Salary: " + salary + "\t\tEmployee Designation: " + designation);
    }
}
public class Inheritance
{
    public static void main(String[] args)
    { 

        Teaching t1 = new Teaching();
        Non_teaching nt1 = new Non_teaching();

        System.out.println("Data about Teaching Class: \n");
        t1.setData();
        t1.getData();

        System.out.println("\nData about Non Teaching Class: \n");
        nt1.setData();
        nt1.getData();

    }
}

How It Works

  1. The base class Office stores common employee fields: number, name, and salary
  2. Teaching and Non_teaching extend Office, inheriting those fields and adding a designation
  3. setData() prompts the user and stores the employee details
  4. getData() prints the employee details to the console
  5. main() creates one teaching employee and one non-teaching employee, then shows both

Concepts Used

Inheritance
Scanner Input
Methods
Class Hierarchy