Forwarded from تجمع شيتات واسئلة oop
❤3
class Person {
String name;
int age;
Person(String name, int age) {
this.name = name;
this.age = age;
}
String introduce() {
return "My name is " + name + " and I am " + age + " years old.";
}
}
class Student extends Person {
String studentId;
Student(String name, int age, String studentId) {
super(name, age);
this.studentId = studentId;
}
@Override
String introduce() {
return super.introduce() + " My student ID is " + studentId + ".";
}
}
class Teacher extends Person {
String subject;
Teacher(String name, int age, String subject) {
super(name, age);
this.subject = subject;
}
@Override
String introduce() {
return super.introduce() + " I teach " + subject + ".";
}
}
public class Main {
public static void main(String[] args) {
Student student = new Student("Mariam", 20, "S12345");
Teacher teacher = new Teacher("Ahmed", 40, "Mathematics");
System.out.println(student.introduce());
System.out.println(teacher.introduce());
}
}
output?
interface Identifiable {
String getId();
}
abstract class Person {
private String name;
private int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public String introduce() {
return "My name is " + name + " and I am " + age + " years old.";
}
public abstract String getRole();
}
class Student extends Person implements Identifiable {
private String studentId;
public Student(String name, int age, String studentId) {
super(name, age);
this.studentId = studentId;
}
@Override
public String getId() {
return studentId;
}
@Override
public String getRole() {
return "Student";
}
@Override
public String introduce() {
return super.introduce() + " I am a " + getRole() + ". My student ID is " + getId() + ".";
}
}
class Teacher extends Person implements Identifiable {
private String subject;
private String teacherId;
public Teacher(String name, int age, String subject, String teacherId) {
super(name, age);
this.subject = subject;
this.teacherId = teacherId;
}
@Override
public String getId() {
return teacherId;
}
@Override
public String getRole() {
return "Teacher";
}
@Override
public String introduce() {
return super.introduce() + " I am a " + getRole() + ". I teach " + subject + ". My ID is " + getId() + ".";
}
}
public class Main {
public static void main(String[] args) {
Person student = new Student("Mariam", 20, "S12345");
Person teacher = new Teacher("Ahmed", 40, "Mathematics", "T98765");
System.out.println(student.introduce());
System.out.println(teacher.introduce());
}
}
output!
👍1
interface Role {
String getRole();
}
abstract class Person {
String name;
Person(String name) { this.name = name; }
abstract String details();
}
class Student extends Person implements Role {
String id;
Student(String name, String id) { super(name); this.id = id; }
public String getRole() { return "Student"; }
String details() { return name + " is a " + getRole() + " with ID: " + id; }
}
public class Main {
public static void main(String[] args) {
Person student = new Student("Mariam", "S12345");
System.out.println(student.details());
}
}