In Java, encapsulation, inheritance, and polymorphism are three fundamental concepts of object-oriented programming. They play a crucial role in designing and implementing robust and flexible software systems. Let’s discuss each of these concepts individually:
To encapsulate data in Java, you typically declare private instance variables in a class and provide public getter and setter methods (also known as accessors and mutators) to interact with the data. By controlling the access to the data through these methods, you can enforce validation, perform actions upon data access, or change the internal representation without affecting other parts of the code.
Encapsulation example:
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; } } }
In the above example, the Person
class encapsulates the data fields name
and age
by declaring them as private. It provides public getter and setter methods (getName()
, setName()
, getAge()
, setAge()
) to access and modify the private fields. The encapsulation allows controlled access to the internal state of the Person
object, ensuring data integrity and providing abstraction.
In Java, you can create a subclass by using the extends
keyword followed by the name of the superclass in the class declaration. The subclass inherits all the accessible members (variables and methods) of the superclass and can add its own specific members or override the inherited ones. Through inheritance, you can create a hierarchy of classes, with each level of the hierarchy inheriting and extending the behavior of the classes above it.
Inheritance example:
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..."); } }
In the above example, the Animal
class serves as the superclass, and the Dog
class extends it, making Dog
the subclass. The Dog
class inherits the eat()
method from the Animal
class. The Dog
class also adds its own method bark()
. With inheritance, the Dog
class inherits the behavior of the Animal
class and can extend it further.
There are two types of polymorphism in Java: compile-time (static) polymorphism and runtime (dynamic) polymorphism.
Polymorphism enables you to write more generic code that can work with objects of different types, as long as they adhere to a common interface or share a superclass.
Polymorphism example:
public class Shape { public void draw() { System.out.println("Drawing a shape"); } } public class Circle extends Shape { @Override public void draw() { System.out.println("Drawing a circle"); } } public class Rectangle extends Shape { @Override public void draw() { System.out.println("Drawing a rectangle"); } } public class PolymorphismExample { 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 } }
In the above example, the Shape
class is a superclass, and the Circle
and Rectangle
classes are subclasses. Each subclass overrides the draw()
method of the superclass with its own implementation. In the PolymorphismExample
class, objects of the Circle
and Rectangle
classes are assigned to variables of type Shape
. The draw()
method is called on these objects, and at runtime, the appropriate overridden method is executed based on the actual type of the object. This demonstrates runtime polymorphism.
These three concepts, encapsulation, inheritance, and polymorphism, form the foundation of object-oriented programming and are extensively used in Java to build modular, maintainable, and scalable applications.
Copyright ©TechOceanhub All Rights Reserved.