Object Classes in Programming

Please complete the following exercises and questions related to object classes in programming.


Exercise 1: Class Creation

1. Create a class named “Person” with the following attributes:

  • Name
  • Age
  • Address

2. Implement a constructor method that initializes these attributes.

3. Create an instance of the “Person” class and populate it with sample data.


Example

Do not copy (we can tell)

public class Person {
    private String name;
    private int age;
    private String address;

    public Person(String name, int age, String address) {
        this.name = name;
        this.age = age;
        this.address = address;
    }

    public void displayInfo() {
        System.out.println("Name: " + name);
        System.out.println("Age: " + age);
        System.out.println("Address: " + address);
    }

    // Getter and setter methods for encapsulation
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }
}


Exercise 2: Class Inheritance

1. Create a subclass named “Student” that inherits from the “Person” class.

2. Add an attribute to the “Student” class for their student ID.

3. Implement a constructor for the “Student” class that includes the student ID along with the attributes inherited from the “Person” class.


Example

Do not copy (we can tell)

public class Student extends Person {
    private int studentId;

    public Student(String name, int age, String address, int studentId) {
        super(name, age, address);
        this.studentId = studentId;
    }

    public void displayStudentInfo() {
        super.displayInfo();
        System.out.println("Student ID: " + studentId);
    }
}


Exercise 3: Polymorphism

1. Create a function that takes an instance of either a “Person” or “Student” as an argument and displays their information using the appropriate method (either “display_info” or “display_student_info”).


Example

Do not copy (we can tell)

public class Main {
    public static void displayPersonInfo(Person person) {
        person.displayInfo();
    }

    public static void main(String[] args) {
        // Creating instances of Person and Student
        Person person = new Person("John Doe", 30, "123 Main St");
        Student student = new Student("Alice Smith", 20, "456 Elm St", 12345);

        // Displaying information using polymorphism
        displayPersonInfo(person);
        displayPersonInfo(student); // Calls the appropriate method based on the object type
    }
}


Questions:

1. What is the concept of inheritance in object-oriented programming, and how does it relate to classes?

2. In what situations might you choose composition (object composition) over inheritance when designing classes in OOP? Provide an example scenario.


Quiz

Click HERE to take the quiz. Please include your score and your email in your Hacks.