In: Computer Science
Consider the following class:
class Person
{
String name;
int age;
Person(String name, int age){
this.name = name;
this.age = age;
}
}
My name is: Ahmed
My age is: 35
My salary is: 8000
My name is: Khalid
My age is: 12
My grade is: 6
Program Code Screenshot :
Sample Output :
Program Code to Copy
class Person { String name; int age; Person(String name, int age) { this.name = name; this.age = age; } } class Student extends Person{ int grade; Student(String name, int age, int grade) { super(name, age); this.grade = grade; } void print(){ System.out.println("My name is: "+super.name); System.out.println("My age is: "+super.age); System.out.println("My grade is: "+this.grade); } } class Teacher extends Person{ int salary; Teacher(String name, int age, int salary) { super(name, age); this.salary = salary; } void print(){ System.out.println("My name is: "+super.name); System.out.println("My age is: "+super.age); System.out.println("My salary is: "+this.salary); } } class Main{ public static void main(String[] args) { Teacher t = new Teacher("Ahmed",35,8000); t.print(); Student s = new Student("Khalid",12,6); s.print(); } }
UML Diagrams