Object-Oriented Programming:
Object-oriented programming (OOP) is at the core of Java.
All Java programs are to atleast some extent object-oriented.
OOP is so integral to Java.
Object-Oriented Programming (OOP) revolves around four core
concepts: Encapsulation, Inheritance, Polymorphism, and Abstraction, which enable
modular, reusable, and maintainable code.
1. Encapsulation:
Definition: Encapsulation bundles data (variables) and the methods (functions) that operate
on that data within a class, creating a self-contained unit.
Encapsulation: public methods can be used to protect private data
Example:
Java
class Dog {
private String name; // Data (variable)
private int age;
public String getName() { // Method to access data
return name;
}
public void setName(String name) { // Method to modify data
[Link] = name;
}
}
The name and age variables are private, meaning they can only be accessed within
the Dog class, and the getName() and setName() methods provide controlled access.
2. Inheritance:
Definition: Inheritance allows a class (the "child" or "subclass") to inherit properties and
methods from another class (the "parent" or "superclass"), promoting code reusability.
Example:
Java
class Animal { // Superclass
void makeSound() {
[Link]("Generic animal sound");
}
}
class Dog extends Animal { // Subclass inheriting from Animal
@Override // Overriding the method from superclass
void makeSound() {
[Link]("Woof!");
}
}
The Dog class inherits the makeSound() method from the Animal class and then overrides it
to provide a specific implementation.
3. Polymorphism:
Definition: Polymorphism allows objects of different classes to be treated as objects of a
common type, enabling flexibility and code reusability.
Types:
o Method Overloading (Compile-time Polymorphism): Defining multiple methods with the
same name but different parameters within the same class.
o Method Overriding (Runtime Polymorphism): Subclasses providing their own
implementations of methods inherited from their superclasses.
Examples:
Java
// Method Overloading
class Calculator {
int add(int a, int b) {
return a + b;
}
double add(double a, double b) {
return a + b;
}
}
// Method Overriding
class Animal {
void makeSound() {
[Link]("Generic animal sound");
}
}
class Dog extends Animal {
@Override
void makeSound() {
[Link]("Woof!");
}
}
4. Abstraction:
Definition: Abstraction hides complex implementation details and exposes only the essential
functionalities, simplifying the user interface.
Syntax:
abstract class
abstract class Shape {
abstract void draw(); // Abstract method
}
interface
interface Drawable {
void draw(); // Abstract method
}
Example:
Java
abstract class Shape { // Abstract class
abstract void draw(); // Abstract method
}
class Circle extends Shape { // Concrete class implementing abstract method
@Override
void draw() {
[Link]("Drawing a circle");
}
The Shape class is astract, meaning it cannot be instantiated, and the draw() method is
abstract, meaning it must be implemented by its concrete subclasses.
Polymorphism, Encapsulation, and Inheritance Work Together:
When properly applied, polymorphism, encapsulation, and inheritance combine to
produce a programming environment that supports the development of far more
robust and scalable programs than does the process-oriented model.
A well-designed hierarchy of classes is the basis for reusing the code in which you
have invested time and effort developing and testing.
Encapsulation allows you to migrate your implementations over time without
breaking the code that depends on the public interface of your classes.
Polymorphism allows you to create clean, sensible, readable, and resilient code.
Filename: [Link]
// Java program for demonstrating the features and functionalities of OOPs cncepts in Java.
class Animal {
private String name;
// Constructor
public Animal(String name) {
[Link] = name;
}
// Encapsulation: Getter method
public String getName() {
return name;
}
// Polymorphism: Overridden method
public void makeSound() {
[Link]("Some sound");
}
}
// Derived class (Inheritance)
class Dog extends Animal {
// Constructor
public Dog(String name) {
super(name);
}
// Polymorphism: Overriding method
@Override
public void makeSound() {
[Link]("Woof");
}
}
// Derived class (Inheritance)
class Cat extends Animal {
// Constructor
public Cat(String name) {
super(name);
}
// Polymorphism: Overriding method
@Override
public void makeSound() {
[Link]("Meow");
}
}
public class OOPs {
public static void main(String[] args) {
// Creating objects of Dog and Cat classes
Dog dog = new Dog("Buddy");
Cat cat = new Cat("Whiskers");
// Accessing methods of base class through objects of derived classes
[Link]("Dog name: " + [Link]());
[Link]();
[Link]("Cat name: " + [Link]());
[Link]();
}
}
Output:
Dog name: Buddy
Woof
Cat name: Whiskers
Meow