Write a Java program to Create an Student class by inheriting Person class
This Java program illustrates the concept of inheritance, specifically single inheritance. Inheritance is a fundamental object-oriented programming concept that allows a class (subclass or derived class) to inherit properties and methods from another class (superclass or base class). In this program, there are two classes: Person and Student, where Student inherits from Person. Here's an explanation of the program:
- public class InheritingExample: This is the main class that contains the main method, which serves as the entry point of the program.
- public static void main(String[] args): The main method is where the program execution starts. It creates an instance of the Student class and calls the printStudentDetails method to display the details of a student.
- Student stu = new Student(1000, "Ram Kumar", 25, 88.96f); : This line creates an object of the Student class, passing in various parameters, including student ID, name, age, and percentage. The Student class constructor initializes these values and also calls the constructor of the Person class using super(age, name) to set the name and age attributes inherited from the Person class.
- stu.printStudentDetails();: This line calls the printStudentDetails method on the stu object to print the student's details, including ID, name, age, and percentage.
- class Person: This is the parent class (superclass) that represents common attributes and behaviors for a person. It has attributes for the name and age of the person.
- Person(int age, String name): The constructor of the Person class takes parameters for age and name and initializes the respective attributes in the Person class.
- class Student extends Person: This is the child class (subclass) that inherits from the Person class. It represents a specific type of person, a student, and includes additional attributes for student ID and percentage.
- Student(int idn, String name, int age, float percent): The constructor of the Student class takes parameters for student ID, name, age, and percentage. It calls the constructor of the parent class Person using super(age, name) to set the inherited attributes and initializes the student-specific attributes (ID and percentage).
- void printStudentDetails(): The Student class has a method named printStudentDetails that prints the student's ID, name, age, and percentage. This method is specific to the Student class and is not present in the Person class.
This program demonstrates single inheritance, where the Student class inherits attributes (name and age) from the Person class, and the Student class also has its own specific attributes (ID and percentage) and behavior (the printStudentDetails method). This allows you to model and represent student objects with specialized attributes while reusing the common attributes of a person from the Person class.
Source Code
public class InheritingExample
{
public static void main(String[] args)
{
Student stu = new Student(1000, "Ram Kumar", 25, 88.96f);
stu.printStudentDetails();
}
}
class Person
{
String name;
int age;
Person(int age, String name)
{
this.name = name;
this.age = age;
}
}
class Student extends Person
{
int id;
float per;
Student(int idn, String name, int age, float percent)
{
super(age, name);
id = idn;
per = percent;
}
void printStudentDetails()
{
System.out.println("Student ID : " + id);
System.out.println("Student Name : " + name);
System.out.println("Student Age : " + age);
System.out.println("Student Percentage : " + per);
}
}
Output
Student ID : 1000
Student Name : Ram Kumar
Student Age : 25
Student Percentage : 88.96