0% found this document useful (0 votes)
5 views3 pages

Features

Java is a fully object-oriented programming language that implements key OOP principles including Encapsulation, Inheritance, Polymorphism, and Abstraction. The document provides examples of each concept through code snippets, illustrating how they work in practice. Additionally, it outlines the advantages of OOP in Java, such as code reusability, modular structure, flexibility, and security.

Uploaded by

Adarsh Pandey
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views3 pages

Features

Java is a fully object-oriented programming language that implements key OOP principles including Encapsulation, Inheritance, Polymorphism, and Abstraction. The document provides examples of each concept through code snippets, illustrating how they work in practice. Additionally, it outlines the advantages of OOP in Java, such as code reusability, modular structure, flexibility, and security.

Uploaded by

Adarsh Pandey
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Object-Oriented Programming (OOP) in Java

Java is a fully object-oriented programming language that follows OOP principles such
as Encapsulation, Inheritance, Polymorphism, and Abstraction.

Key OOP Concepts in Java

1. Encapsulation – Wrapping data and methods into a single unit using private
variables and public getters/setters.

2. Inheritance – Allowing a class to inherit properties and methods from


another class using extends.

3. Polymorphism – Enabling a single function to perform different tasks using


method overloading and method overriding.

4. Abstraction – Hiding implementation details using abstract classes and


interfaces.

Example Code for Each Concept

1. Encapsulation

class Car {

private String model; // Private variable (Encapsulation)

// Setter Method

public void setModel(String model) {

this.model = model;

// Getter Method

public String getModel() {

return model;

public class Main {

public static void main(String[] args) {

Car car = new Car();

car.setModel("Tesla Model S");

System.out.println("Car Model: " + car.getModel());

2. Inheritance

// Parent Class

class Animal {

void makeSound() {

System.out.println("Animal makes a sound");

// Child Class inheriting from Animal

class Dog extends Animal {

void bark() {

System.out.println("Dog barks");

public class Main {

public static void main(String[] args) {

Dog d = new Dog();

d.makeSound(); // Inherited Method

d.bark(); // Child Class Method

3. Polymorphism

Method Overloading (Compile-time Polymorphism)

class MathOperations {

// Method Overloading: same method name, different parameters

int add(int a, int b) {

return a + b;

}
int add(int a, int b, int c) {

return a + b + c;

public class Main {

public static void main(String[] args) {

MathOperations obj = new MathOperations();

System.out.println("Sum: " + obj.add(10, 20)); // Calls 2-arg method

System.out.println("Sum: " + obj.add(10, 20, 30)); // Calls 3-arg method

Method Overriding (Runtime Polymorphism)

class Animal {

void makeSound() {

System.out.println("Animal makes a sound");

// Overriding the method in the child class

class Dog extends Animal {

@Override

void makeSound() {

System.out.println("Dog barks");

public class Main {

public static void main(String[] args) {

Animal myAnimal = new Dog(); // Upcasting

myAnimal.makeSound(); // Calls Dog's overridden method

4. Abstraction

Using Abstract Class

abstract class Vehicle {

abstract void start(); // Abstract method (no implementation)

void stop() {

System.out.println("Vehicle is stopping...");

class Car extends Vehicle {

@Override

void start() {

System.out.println("Car is starting...");

public class Main {

public static void main(String[] args) {

Vehicle myCar = new Car();

myCar.start(); // Calls overridden method

myCar.stop(); // Calls non-abstract method from abstract class

Using Interface

interface Animal {

void makeSound(); // Abstract method (by default public and abstract)

class Dog implements Animal {

@Override
public void makeSound() {

System.out.println("Dog barks");

public class Main {

public static void main(String[] args) {

Animal myDog = new Dog();

myDog.makeSound();

Advantages of OOP in Java

✅ Code Reusability – Inheritance promotes reuse of existing code.


✅ Modular Structure – Makes maintenance and updates easier.
✅ Flexibility – Polymorphism allows flexibility in code.
✅ Security – Encapsulation prevents unauthorized data access.

You might also like