Object-Oriented Programming (OOP) is a programming paradigm based on the concept of "objects", which can contain both data and methods. OOP is a core part of Java, and understanding its principles is key for building maintainable and scalable applications. Let's dive into the major OOP concepts.
1. Class and Object
A class is a blueprint or template from which objects are created. An object is an instance of a class, representing real-world entities.
class Car {
String model;
int year;
void displayInfo() {
System.out.println("Model: " + model + ", Year: " + year);
}
}
// Creating an object of the Car class
Car myCar = new Car();
myCar.model = "Toyota";
myCar.year = 2020;
myCar.displayInfo();
2. Inheritance
Inheritance allows one class to inherit the properties and methods of another class. The class that inherits is called the subclass (or derived class), and the class being inherited from is the superclass (or base class).
Types of Inheritance
- Single Inheritance: A class inherits from one superclass.
- Multilevel Inheritance: A class inherits from a class, which in turn inherits from another class.
- Hierarchical Inheritance: Multiple classes inherit from a single superclass.
class Animal {
void sound() {
System.out.println("Animal makes sound");
}
}
class Dog extends Animal {
void sound() {
System.out.println("Dog barks");
}
}
3. Polymorphism
Polymorphism allows one action to behave differently based on the object calling it. There are two types of polymorphism in Java:
Method Overloading (Compile-time Polymorphism)
Method overloading allows a class to have multiple methods with the same name but different parameters.
class Calculator {
int add(int a, int b) {
return a + b;
}
int add(int a, int b, int c) {
return a + b + c;
}
}
Method Overriding (Run-time Polymorphism)
Method overriding allows a subclass to provide its own implementation of a method that is already defined in the superclass.
class Animal {
void sound() {
System.out.println("Animal makes sound");
}
}
class Cat extends Animal {
void sound() {
System.out.println("Cat meows");
}
}
4. Encapsulation
Encapsulation is the concept of bundling data (variables) and methods that operate on the data into a single unit (class). Access to the data is controlled using access modifiers.
class Person {
private String name;
// Getter method
public String getName() {
return name;
}
// Setter method
public void setName(String name) {
this.name = name;
}
}
5. Abstraction
Abstraction is the concept of hiding the internal details and showing only the functionality to the user. In Java, abstraction is achieved using abstract classes and interfaces.
Abstract Classes
An abstract class can have both abstract methods (without implementation) and concrete methods (with implementation).
abstract class Shape {
abstract void draw();
}
class Circle extends Shape {
void draw() {
System.out.println("Drawing a circle");
}
}
Interfaces
An interface is a blueprint of a class that contains abstract methods. A class that implements an interface must provide an implementation for all the methods declared in the interface.
interface Animal {
void eat();
}
class Dog implements Animal {
public void eat() {
System.out.println("Dog eats");
}
}
6. Constructors
A constructor is a special method that is called when an object is instantiated. It initializes the object and can be parameterized or non-parameterized.
class Car {
String model;
int year;
// Constructor
Car(String model, int year) {
this.model = model;
this.year = year;
}
void displayInfo() {
System.out.println("Model: " + model + ", Year: " + year);
}
}
7. Static and Non-static Members
Static members belong to the class rather than any object, while non-static members belong to an instance of the class.
class Example {
static int staticVar = 10;
int instanceVar = 20;
}
8. This and Super Keywords
The this keyword refers to the current object, while the super keyword refers to the parent class.
class Parent {
int value = 100;
}
class Child extends Parent {
int value = 200;
void displayValues() {
System.out.println("Value from child: " + this.value);
System.out.println("Value from parent: " + super.value);
}
}
9. Final Keyword
The final keyword can be used with variables (to make them constant), methods (to prevent overriding), and classes (to prevent inheritance).
final class Constants {
final int MAX_VALUE = 100;
}
Summary
In this section, we covered the main OOP concepts: classes and objects, inheritance, polymorphism, encapsulation, abstraction, constructors, static and non-static members, and important keywords like this
, super
, and final
. These concepts are vital for writing well-structured Java code.