Object-Oriented Programming (OOP) in Java is a paradigm based on the concept of "objects," which
are instances of classes. OOP principles include encapsula on, inheritance, polymorphism, and
abstrac on. Understanding these core concepts and how they relate to Java will help you in exams
and real-world coding.
1. Class and Object
Class: A blueprint for crea ng objects. It defines a ributes (fields) and behaviors (methods)
that the objects created from the class will have.
Object: An instance of a class. It contains the state (fields) and behavior (methods) defined
by the class.
Example:
java
Copy code
class Car {
String model;
int year;
void displayInfo() {
System.out.println("Model: " + model + ", Year: " + year);
public class Main {
public sta c void main(String[] args) {
Car car1 = new Car(); // Crea ng an object (car1) from the class Car
car1.model = "Toyota";
car1.year = 2020;
car1.displayInfo(); // Output: Model: Toyota, Year: 2020
2. Encapsula on
Encapsula on is the mechanism of bundling the data (variables) and the methods (func ons) that act
on the data into a single unit, the class. It also involves hiding the internal details (data) from outside
access, providing controlled access through methods (ge ers and se ers).
Example:
java
Copy code
class Employee {
private int salary; // Private field (data is hidden)
// Se er method to assign value
public void setSalary(int salary) {
this.salary = salary;
// Ge er method to access value
public int getSalary() {
return salary;
public class Main {
public sta c void main(String[] args) {
Employee emp = new Employee();
emp.setSalary(50000); // Set salary using se er
System.out.println(emp.getSalary()); // Get salary using ge er
3. Inheritance
Inheritance allows a class (child/subclass) to inherit proper es and behaviors (methods) from
another class (parent/superclass). It promotes code reuse.
Example:
java
Copy code
class Animal {
void eat() {
System.out.println("This animal eats food.");
class Dog extends Animal { // Dog inherits Animal
void bark() {
System.out.println("The dog barks.");
public class Main {
public sta c void main(String[] args) {
Dog dog = new Dog();
dog.eat(); // Inherited method from Animal
dog.bark(); // Dog-specific method
4. Polymorphism
Polymorphism means "many forms," allowing one en ty (method or object) to behave differently in
different contexts. It is of two types:
Compile- me Polymorphism (Method Overloading): Same method name, but different
parameter lists.
Run me Polymorphism (Method Overriding): A child class provides a specific
implementa on of a method that is already defined in its parent class.
Method Overloading Example (Compile- me Polymorphism):
java
Copy code
class Calculator {
// Overloaded methods (different signatures)
public int add(int a, int b) {
return a + b;
}
public double add(double a, double b) {
return a + b;
public class Main {
public sta c void main(String[] args) {
Calculator calc = new Calculator();
System.out.println(calc.add(10, 20)); // Calls int version
System.out.println(calc.add(10.5, 20.5)); // Calls double version
Method Overriding Example (Run me Polymorphism):
java
Copy code
class Animal {
void sound() {
System.out.println("This animal makes a sound.");
class Dog extends Animal {
@Override
void sound() {
System.out.println("The dog barks.");
public class Main {
public sta c void main(String[] args) {
Animal myAnimal = new Dog(); // Run me polymorphism
myAnimal.sound(); // Calls Dog's overridden method
5. Abstrac on
Abstrac on is the process of hiding the implementa on details and showing only the essen al
features of the object. In Java, abstrac on can be achieved using:
Abstract Classes
Interfaces
Abstract Class Example:
java
Copy code
abstract class Shape {
// Abstract method (doesn't have a body)
abstract void draw();
// Concrete method
void info() {
System.out.println("This is a shape.");
class Circle extends Shape {
@Override
void draw() {
System.out.println("Drawing a circle.");
public class Main {
public sta c void main(String[] args) {
Shape shape = new Circle(); // Cannot instan ate Shape, only subclass
shape.draw();
shape.info();
Interface Example:
java
Copy code
interface Drawable {
void draw(); // Abstract method by default
class Square implements Drawable {
@Override
public void draw() {
System.out.println("Drawing a square.");
public class Main {
public sta c void main(String[] args) {
Drawable drawable = new Square();
drawable.draw();
6. Constructor
A constructor is a special method used to ini alize objects. It is called when an object of a class is
created.
Example:
java
Copy code
class Student {
String name;
int age;
// Constructor
Student(String name, int age) {
this.name = name;
this.age = age;
void displayInfo() {
System.out.println("Name: " + name + ", Age: " + age);
public class Main {
public sta c void main(String[] args) {
Student student = new Student("Alice", 20);
student.displayInfo();
7. Sta c Keyword
Sta c Variables: Belong to the class, not instances.
Sta c Methods: Can be called without crea ng an object of the class.
Sta c Block: Used to ini alize sta c data members.
Example:
java
Copy code
class Counter {
sta c int count = 0; // Sta c variable
// Sta c method
sta c void increment() {
count++;
public class Main {
public sta c void main(String[] args) {
Counter.increment(); // Call without object
Counter.increment();
System.out.println("Count: " + Counter.count);
8. Final Keyword
Final Variables: Cannot be changed a er ini aliza on.
Final Methods: Cannot be overridden by subclasses.
Final Classes: Cannot be subclassed.
Example:
java
Copy code
final class Vehicle {
final int maxSpeed = 120; // Final variable
final void run() {
System.out.println("Vehicle is running.");
// class Car extends Vehicle {} // Error: Cannot extend final class Vehicle
public class Main {
public sta c void main(String[] args) {
Vehicle v = new Vehicle();
v.run();
// v.maxSpeed = 130; // Error: Cannot change final variable
9. This Keyword
Used to refer to the current instance of a class.
Commonly used in constructors and se ers to resolve naming conflicts.
Example:
java
Copy code
class Person {
String name;
Person(String name) {
this.name = name; // 'this' refers to the current instance
void display() {
System.out.println("Name: " + this.name);
public class Main {
public sta c void main(String[] args) {
Person person = new Person("John");
person.display();
}
10. Super Keyword
Used to refer to the parent class (superclass) in a subclass.
Can access parent class variables, methods, and constructors.
Example:
java
Copy code
class Animal {
String type = "Animal";
void sound() {
System.out.println("This animal makes a sound.");
class Dog extends Animal {
String type = "Dog";
void sound() {
super.sound(); // Calls parent class method
System.out.println("The dog barks.");
void printType() {
System.out.println("Type in Dog: " + type);
System.out.println("Type in Animal: " + super.type); // Refers to parent class variable
public class Main {
public sta c void main(String[] args) {
Dog dog = new Dog();
dog.sound();
dog.printType();
These are the core concepts of OOP in Java that you need to understand for exam purposes. You
should prac ce wri ng code with each concept and understand how they work together.