Java Object-Oriented Programming (OOP)
Object-Oriented Programming (OOP) is a programming paradigm that uses “objects” to design applications and computer programs. Java is a fully object-oriented language, which means everything in Java is an object.
Key Concepts of OOP
- Class and Object
- Inheritance
- Polymorphism
- Encapsulation
- Abstraction
1. Class and Object
A class is a blueprint for creating objects. It defines properties (fields) and behaviors (methods) that the objects created from the class can have.
An object is an instance of a class. It is created using the new
keyword.
// Defining a class
public class Animal {
// Fields
String name;
int age;
// Constructor
public Animal(String name, int age) {
this.name = name;
this.age = age;
}
// Method
public void displayInfo() {
System.out.println("Name: " + name + ", Age: " + age);
}
}
// Creating an object
public class Main {
public static void main(String[] args) {
Animal dog = new Animal("Buddy", 5);
dog.displayInfo(); // Output: Name: Buddy, Age: 5
}
}
2. Inheritance
Inheritance is a mechanism in which one class acquires the properties and behaviors of another class. The class that inherits is called the subclass (child class), and the class being inherited from is the superclass (parent class).
// Superclass
public class Animal {
String name;
int age;
public Animal(String name, int age) {
this.name = name;
this.age = age;
}
public void displayInfo() {
System.out.println("Name: " + name + ", Age: " + age);
}
}
// Subclass
public class Dog extends Animal {
String breed;
public Dog(String name, int age, String breed) {
super(name, age); // Calling the constructor of the superclass
this.breed = breed;
}
public void displayBreed() {
System.out.println("Breed: " + breed);
}
}
public class Main {
public static void main(String[] args) {
Dog dog = new Dog("Buddy", 5, "Golden Retriever");
dog.displayInfo(); // Output: Name: Buddy, Age: 5
dog.displayBreed(); // Output: Breed: Golden Retriever
}
}
3. Polymorphism
Polymorphism allows methods to do different things based on the object it is acting upon. It can be achieved through method overriding and method overloading.
Method Overriding
public class Animal {
public void sound() {
System.out.println("Animal makes a sound");
}
}
public class Dog extends Animal {
@Override
public void sound() {
System.out.println("Dog barks");
}
}
public class Main {
public static void main(String[] args) {
Animal myDog = new Dog();
myDog.sound(); // Output: Dog barks
}
}
Method Overloading
public class MathUtils {
// Overloaded methods
public int add(int a, int b) {
return a + b;
}
public double add(double a, double b) {
return a + b;
}
}
public class Main {
public static void main(String[] args) {
MathUtils utils = new MathUtils();
System.out.println(utils.add(5, 3)); // Output: 8
System.out.println(utils.add(5.5, 3.5)); // Output: 9.0
}
}
4. Encapsulation
Encapsulation is the mechanism of wrapping the data (variables) and code (methods) together as a single unit. In encapsulation, the variables of a class are hidden from other classes and can be accessed only through the methods of their current class.
public class Person {
// Private fields
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;
}
}
public class Main {
public static void main(String[] args) {
Person person = new Person();
person.setName("Alice");
person.setAge(30);
System.out.println("Name: " + person.getName() + ", Age: " + person.getAge()); // Output: Name: Alice, Age: 30
}
}
5. Abstraction
Abstraction is the process of hiding the implementation details and showing only functionality to the user. It can be achieved using abstract classes and interfaces.
Abstract Class
abstract class Animal {
// Abstract method (does not have a body)
public abstract void sound();
// Regular method
public void sleep() {
System.out.println("Sleeping...");
}
}
class Dog extends Animal {
public void sound() {
System.out.println("Dog barks");
}
}
public class Main {
public static void main(String[] args) {
Dog dog = new Dog();
dog.sound(); // Output: Dog barks
dog.sleep(); // Output: Sleeping...
}
}
Interface
interface Animal {
void sound(); // Abstract method
}
class Dog implements Animal {
public void sound() {
System.out.println("Dog barks");
}
}
public class Main {
public static void main(String[] args) {
Dog dog = new Dog();
dog.sound(); // Output: Dog barks
}
}