The Object-Oriented Programming (OOP) concepts are fundamental principles that form the basis of object-oriented programming languages such as Java. The OOP concepts include:
1. Encapsulation
Encapsulation is the technique of hiding the internal details of an object and exposing only the necessary information to the outside world. It allows objects to be treated as a single entity, providing a clear separation between the implementation details and the public interface.
Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
class Car { private String make; private String model; private int year; public Car(String make, String model, int year) { this.make = make; this.model = model; this.year = year; } public String getMake() { return make; } public String getModel() { return model; } public int getYear() { return year; } } |
2. Abstraction
Abstraction is the technique of representing the essential features of an object, hiding its implementation details. It provides a general view of an object and reduces the complexity of the system.
Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
abstract class Vehicle { protected int wheels; public Vehicle(int wheels) { this.wheels = wheels; } public abstract void start(); public abstract void stop(); } class Car extends Vehicle { public Car() { super(4); } @Override public void start() { System.out.println("Starting the car"); } @Override public void stop() { System.out.println("Stopping the car"); } } |
3. Inheritance
Inheritance is a mechanism that allows a new class to inherit the properties and behavior of an existing class. It provides code reuse and enables the creation of a hierarchy of classes.
Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
class Vehicle { protected int wheels; public Vehicle(int wheels) { this.wheels = wheels; } public void start() { System.out.println("Starting the vehicle"); } public void stop() { System.out.println("Stopping the vehicle"); } } class Car extends Vehicle { private String make; private String model; private int year; public Car(String make, String model, int year) { super(4); this.make = make; this.model = model; this.year = year; } public String getMake() { return make; } public String getModel() { return model; } public int getYear() { return year; } } |
4. Polymorphism
Polymorphism is the ability of an object to take on many forms. It allows objects of different classes to be treated as objects of the same class, providing a single interface to the outside world.
Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
class Shape { public void draw() { System.out.println("Drawing a shape"); } } class Circle extends Shape { @Override public void draw() { System.out.println("Drawing a </code>circle"); } } class Rectangle extends Shape { @Override public void draw() { System.out.println("Drawing a rectangle"); } } public class Main { public static void main(String[] args) { Shape shape1 = new Circle(); shape1.draw(); Shape shape2 = new Rectangle(); shape2.draw();<code> </code>} } |
Output:
Drawing a circle Drawing a rectangle
In this example, the main
method creates two objects of different classes Circle
and Rectangle
, both of which extend the Shape
class. Both objects are referenced by the same reference variable Shape
. The polymorphic behavior is demonstrated by the draw
method being executed based on the actual object being referred to, rather than the reference type.