List No 3
List No 3
Reference: https://www.geeksforgeeks.org/object-oriented-programming-oops-concept-
in-java/
Task: Write a paragraph summarizing the four main OOP principles: Encapsulation,
Abstraction, Inheritance, and Polymorphism.
2. Defining a Class
Theory: A class in Java is a blueprint for creating objects, defining attributes (fields) and
behaviors (methods).
Reference: https://www.w3schools.com/java/java_classes.asp
Task: Create a `Person` class with fields for `name` and `age` in a separate `.java` file.
3. Creating Objects
Theory: An object is an instance of a class, created using the `new` keyword. Each object has
its own set of attributes.
Reference: https://docs.oracle.com/javase/tutorial/java/javaOO/objectcreation.html
Task: Instantiate two `Person` objects with different names and ages in a main method.
Reference: https://www.geeksforgeeks.org/fields-in-java/
Task: Add `height` and `weight` fields to the `Person` class and set their values in the main
method.
5. Methods
Theory: Methods define the behaviors of a class and allow objects to interact with their
fields and each other.
Reference: https://www.w3schools.com/java/java_methods.asp
Task: Add a `printDetails` method in the `Person` class that prints the name and age of a
person.
6. Constructors
Theory: Constructors initialize new objects, having the same name as the class and can be
parameterized.
Reference: https://www.javatpoint.com/constructor
Task: Add a parameterized constructor to `Person` to initialize `name` and `age` when
creating a new `Person`.
7. Default Constructors
Theory: If no constructor is provided, Java supplies a default constructor that initializes
objects with default values.
Reference: https://www.geeksforgeeks.org/default-constructors-java/
Task: Remove the parameterized constructor in `Person`, add a default constructor, and
observe default values for `name` and `age`.
Reference: https://www.javatpoint.com/getter-and-setter-in-java
Task: Make `name` and `age` private and add `getName`, `setName`, `getAge`, and `setAge`
methods.
9. Method Overloading
Theory: Method overloading allows multiple methods with the same name but different
parameters, increasing flexibility.
Reference: https://www.javatpoint.com/method-overloading-in-java
Task: Overload `printDetails` to print either just the name or both name and age based on
parameter signatures.
Reference: https://www.w3schools.com/java/ref_keyword_this.asp
Task: Modify the `Person` constructor to use `this` when setting field values.
Reference: https://www.geeksforgeeks.org/static-keyword-in-java/
Task: Add a static `counter` field in `Person` to count the number of `Person` objects created
and a static method to retrieve this count.
Reference: https://www.w3schools.com/java/java_methods.asp
Task: Add a method to `Person` that returns a full description string (name, age, height,
weight).
Reference: https://www.geeksforgeeks.org/constants-in-java/
Task: Add a `static final int` constant for `MAX_AGE` in `Person` and use it to validate `age`.
Reference: https://www.javatpoint.com/encapsulation
Task: Make `name` and `age` private and modify `printDetails` to print `Person` details only
if criteria are met.
Reference: https://www.geeksforgeeks.org/structure-of-a-java-program/
Task: Create an `Employee` class with fields like `id`, `department`, and `salary`. Write
methods to calculate annual salary, print employee details, and compare salaries.