Overview:
In Lesson 1, we introduce the foundational concepts of Object-Oriented Programming (OOP), a paradigm widely used for designing and organizing code. OOP is centered around four key principles: encapsulation, inheritance, polymorphism, and abstraction. Understanding these principles is essential for creating modular, reusable, and maintainable code.
Key Concepts:
- Encapsulation:
- Definition: Encapsulation is the bundling of data (attributes) and the methods (functions) that operate on the data into a single unit known as a class.
- Purpose: It helps in hiding the internal details of how an object works and exposing only what is necessary.
// Example of Encapsulation
public class Person {
private String name;
private int age;
// Getter and Setter methods
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;
}
}
Inheritance:
- Definition: Inheritance is a mechanism that allows a new class (subclass/derived class) to inherit the properties and behaviors of an existing class (superclass/base class).
- Purpose: It promotes code reusability and establishes a relationship between classes.
// Example of Inheritance
public class Animal {
public void eat() {
System.out.println("Animal is eating.");
}
}
public class Dog extends Animal {
public void bark() {
System.out.println("Dog is barking.");
}
}
Polymorphism:
- Definition: Polymorphism allows objects to be treated as instances of their parent class, enabling the use of a single interface to represent different types of objects.
- Purpose: It simplifies code and enhances flexibility by allowing methods to work with objects of various types.
// Example of Polymorphism
public interface Shape {
void draw();
}
public class Circle implements Shape {
public void draw() {
System.out.println("Drawing a circle.");
}
}
public class Square implements Shape {
public void draw() {
System.out.println("Drawing a square.");
}
}
Abstraction:
- Definition: Abstraction involves simplifying complex systems by modeling classes based on the essential properties and behaviors they share.
- Purpose: It focuses on the essential features of an object while ignoring the non-essential details.
// Example of Abstraction
public abstract class Shape {
// Abstract method (no implementation)
public abstract void draw();
}
public class Circle extends Shape {
public void draw() {
System.out.println("Drawing a circle.");
}
}
public class Square extends Shape {
public void draw() {
System.out.println("Drawing a square.");
}
}
Example:
Let’s create a simple program that demonstrates the principles of OOP. We’ll define a Person
class using encapsulation, create a Student
class that inherits from Person
, showcase polymorphism with a displayInfo
method, and use abstraction with an abstract Shape
class.
// Encapsulation Example
public class Person {
private String name;
private int age;
// Constructor
public Person(String name, int age) {
this.name = name;
this.age = age;
}
// Getter and Setter methods
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;
}
}
// Inheritance and Polymorphism Example
public class Student extends Person {
private int studentId;
// Constructor
public Student(String name, int age, int studentId) {
super(name, age);
this.studentId = studentId;
}
// Polymorphism: Display information
public void displayInfo() {
System.out.println("Student ID: " + studentId);
System.out.println("Name: " + getName());
System.out.println("Age: " + getAge());
}
}
// Abstraction Example
public abstract class Shape {
// Abstract method (no implementation)
public abstract void draw();
}
// Concrete classes implementing Shape
public class Circle extends Shape {
public void draw() {
System.out.println("Drawing a circle.");
}
}
public class Square extends Shape {
public void draw() {
System.out.println("Drawing a square.");
}
}
This example showcases encapsulation, inheritance, polymorphism, and abstraction, laying the foundation for a deeper exploration of OOP principles in Java.