🚀 Abstract Classes in Java: Human Example Explained! (Part 2)
Take your understanding of abstract classes to the next level with a real-world human example! Learn how to design an abstract class Person and extend it to subclasses like Student and Teacher. See how abstract methods enforce specific behaviors and how concrete methods provide shared functionality. Perfect for hands-on Java learners! 💡👩🏫👨🎓
Step 1: Abstract Class Person
java
abstract class Person {
protected String name;
protected int age;
// Constructor to initialize common fields
public Person(String name, int age) {
this.name = name;
this.age = age;
}
// Abstract method — each subclass defines how they work
abstract void work();
// Concrete method — shared among all persons
public void introduce() {
System.out.println("Hi, my name is " + name + " and I am " + age + " years old.");
}
}
Step 2: Subclass Student
java
class Student extends Person {
private String course;
public Student(String name, int age, String course) {
super(name, age);
this.course = course;
}
// Implement abstract method
@Override
void work() {
System.out.println(name + " is studying " + course + ".");
}
}
Step 3: Subclass Teacher
java
class Teacher extends Person {
private String subject;
public Teacher(String name, int age, String subject) {
super(name, age);
this.subject = subject;
}
// Implement abstract method
@Override
void work() {
System.out.println(name + " is teaching " + subject + ".");
}
}
Step 4: Main Class to Test
java
public class HumanDemo {
public static void main(String[] args) {
Person student = new Student("Alice", 20, "Mathematics");
Person teacher = new Teacher("Mr. Bob", 45, "Physics");
student.introduce();
student.work();
teacher.introduce();
teacher.work();
}
}
Expected Output
Hi, my name is Alice and I am 20 years old.
Alice is studying Mathematics.
Hi, my name is Mr. Bob and I am 45 years old.
Mr. Bob is teaching Physics.
Explanation
Person is an abstract class with a constructor and an abstract method work().
Both Student and Teacher extend Person and must implement the work() method.
The concrete method introduce() is shared, so no need to override it in subclasses.
This design ensures common properties (name, age) and behavior (introduce()) are reused.
At the same time, it forces subclasses to provide their own specific behavior via work().
Why Use Abstract Classes Here?
To avoid creating plain Person objects (which makes no sense).
To enforce that every type of person must define how they work.
To share common code (like introduction) between all person types.
Catchy Summary
Abstract class Person sets the stage! Students and Teachers bring it to life with their own unique behaviors. This pattern keeps your Java code clean, reusable, and easy to maintain! 🚀👩🎓👨🏫
Hashtags
#JavaAbstractClass, #OOPBasics, #JavaProgramming, #LearnJava, #CodingExamples, #JavaTutorials
Видео 🚀 Abstract Classes in Java: Human Example Explained! (Part 2) канала QA_AI_WIZARDS
Step 1: Abstract Class Person
java
abstract class Person {
protected String name;
protected int age;
// Constructor to initialize common fields
public Person(String name, int age) {
this.name = name;
this.age = age;
}
// Abstract method — each subclass defines how they work
abstract void work();
// Concrete method — shared among all persons
public void introduce() {
System.out.println("Hi, my name is " + name + " and I am " + age + " years old.");
}
}
Step 2: Subclass Student
java
class Student extends Person {
private String course;
public Student(String name, int age, String course) {
super(name, age);
this.course = course;
}
// Implement abstract method
@Override
void work() {
System.out.println(name + " is studying " + course + ".");
}
}
Step 3: Subclass Teacher
java
class Teacher extends Person {
private String subject;
public Teacher(String name, int age, String subject) {
super(name, age);
this.subject = subject;
}
// Implement abstract method
@Override
void work() {
System.out.println(name + " is teaching " + subject + ".");
}
}
Step 4: Main Class to Test
java
public class HumanDemo {
public static void main(String[] args) {
Person student = new Student("Alice", 20, "Mathematics");
Person teacher = new Teacher("Mr. Bob", 45, "Physics");
student.introduce();
student.work();
teacher.introduce();
teacher.work();
}
}
Expected Output
Hi, my name is Alice and I am 20 years old.
Alice is studying Mathematics.
Hi, my name is Mr. Bob and I am 45 years old.
Mr. Bob is teaching Physics.
Explanation
Person is an abstract class with a constructor and an abstract method work().
Both Student and Teacher extend Person and must implement the work() method.
The concrete method introduce() is shared, so no need to override it in subclasses.
This design ensures common properties (name, age) and behavior (introduce()) are reused.
At the same time, it forces subclasses to provide their own specific behavior via work().
Why Use Abstract Classes Here?
To avoid creating plain Person objects (which makes no sense).
To enforce that every type of person must define how they work.
To share common code (like introduction) between all person types.
Catchy Summary
Abstract class Person sets the stage! Students and Teachers bring it to life with their own unique behaviors. This pattern keeps your Java code clean, reusable, and easy to maintain! 🚀👩🎓👨🏫
Hashtags
#JavaAbstractClass, #OOPBasics, #JavaProgramming, #LearnJava, #CodingExamples, #JavaTutorials
Видео 🚀 Abstract Classes in Java: Human Example Explained! (Part 2) канала QA_AI_WIZARDS
Комментарии отсутствуют
Информация о видео
5 июня 2025 г. 15:09:51
00:03:24
Другие видео канала