Basics
8.1 Introduction to OOP:
- Object-Oriented Programming (OOP) is a programming paradigm that focuses on organizing code into objects that represent real-world entities.
- It provides a way to structure and modularize code, making it more manageable and reusable.
8.2 Classes and Objects:
- In OOP, a class is a blueprint for creating objects.
- It defines the properties (attributes) and behaviors (methods) that objects of that class will have.
- An object is an instance of a class.
- Here's an example:
Car.java File🖹
public class CarRunner {
public static void main(String[] args) {
// Creating objects
Car car1 = new Car();
Car car2 = new Car();
// Accessing properties and invoking methods
car1.color = "Red";
car1.year = 2020;
car1.startEngine(); // Output: "Engine started!"
car2.color = "Blue";
car2.year = 2018;
car2.stopEngine(); // Output: "Engine stopped!"
}
}
CarRunner.java File🖹
public class CarRunner {
public static void main(String[] args) {
// Creating objects
Car car1 = new Car();
Car car2 = new Car();
// Accessing properties and invoking methods
car1.color = "Red";
car1.year = 2020;
car1.startEngine(); // Output: "Engine started!"
car2.color = "Blue";
car2.year = 2018;
car2.stopEngine(); // Output: "Engine stopped!"
}
}
8.3 Encapsulation and Data Hiding:
- Encapsulation is the practice of hiding internal details of an object and providing public methods to interact with the object.
- It helps maintain data integrity and enhances code modularity. Access modifiers (e.g., private, public) control the visibility of class members.
public class Person {
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
if (age > 0) {
this.age = age;
}
}
public static void main(String[] args) {
Person person = new Person();
person.setName("CM");
person.setAge(-20); // Invalid age will be ignored
System.out.println("Name: " + person.getName()); // Output: "Name: CM"
System.out.println("Age: " + person.getAge()); // Output: "Age: 0"
}
}
8.4 Inheritance:
- Inheritance allows you to create a new class (subclass) based on an existing class (superclass).
- The subclass inherits the properties and behaviors of the superclass and can add its own unique features.
Animal.java File🖹
public class Animal {
public void eat() {
System.out.println("Animal is eating.");
}
public static void main(String[] args) {
Dog dog = new Dog();
dog.eat(); // Output: "Animal is eating."
dog.bark(); // Output: "Dog is barking."
}
}
Dog.java File🖹
public class Dog extends Animal {
public void bark() {
System.out.println("Dog is barking.");
}
}
8.5 Polymorphism:
- Polymorphism allows objects of different classes to be treated as objects of a common superclass.
- It provides flexibility and extensibility in designing and working with objects.
Shape.java File🖹
public class Shape {
public void draw() {
System.out.println("Drawing a shape.");
}
public static void main(String[] args) {
Shape shape1 = new Circle();
Shape shape2 = new Rectangle();
shape1.draw(); // Output: "Drawing a circle."
shape2.draw(); // Output: "Drawing a rectangle."
}
}
Circle.java File🖹
public class Circle extends Shape {
@Override
public void draw() {
System.out.println("Drawing a circle.");
}
}
Rectangle.java File🖹
public class Rectangle extends Shape {
@Override
public void draw() {
System.out.println("Drawing a rectangle.");
}
}