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

Java OOP Concepts Notes

Uploaded by

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

Java OOP Concepts Notes

Uploaded by

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

Java OOP Concepts - Detailed Notes

Introduction to OOP
Object-Oriented Programming (OOP) is a programming paradigm that organizes software design
around data, or objects, rather than functions and logic. In Java, everything is treated as
an object, making it a pure object-oriented language (except primitive types). OOP provides
modularity, reusability, and scalability for building complex applications.

Class and Object


- Class: A blueprint or template for creating objects. It defines attributes (variables) and
behaviors (methods).
Example:
class Car {
String color;
void drive() { System.out.println("Car is driving"); }
}
- Object: An instance of a class.
Car myCar = new Car();

Encapsulation
Encapsulation is the practice of hiding internal details of a class and exposing only
essential features. It is achieved using access modifiers (private, public, protected).

Benefits:
- Data hiding
- Increased security
- Code flexibility and maintainability

Example:
class Student {
private String name;
public void setName(String n) { name = n; }
public String getName() { return name; }
}

Inheritance
Inheritance allows one class to acquire properties and methods of another class. It
promotes code reusability.

- Superclass: The parent class


- Subclass: The child class

Example:
class Animal {
void eat() { System.out.println("Eating..."); }
}
class Dog extends Animal {
void bark() { System.out.println("Barking..."); }
}

Types of Inheritance in Java:


- Single Inheritance
- Multilevel Inheritance
- Hierarchical Inheritance
(Note: Java does not support Multiple Inheritance directly; it is achieved using interfaces.)

Polymorphism
Polymorphism means "many forms." In Java, polymorphism allows the same method to perform
different actions depending on the context.

- Compile-time Polymorphism (Method Overloading)


- Runtime Polymorphism (Method Overriding)

Example (Overloading):
class MathOp {
int add(int a, int b) { return a + b; }
double add(double a, double b) { return a + b; }
}

Example (Overriding):
class Animal { void sound() { System.out.println("Animal sound"); } }
class Dog extends Animal { void sound() { System.out.println("Bark"); } }

Abstraction
Abstraction is the process of hiding implementation details and showing only essential
features of an object. It can be achieved using abstract classes and interfaces.

Example (Abstract Class):


abstract class Shape {
abstract void draw();
}
class Circle extends Shape {
void draw() { System.out.println("Drawing Circle"); }
}

Example (Interface):
interface Animal {
void makeSound();
}
class Cat implements Animal {
public void makeSound() { System.out.println("Meow"); }
}

Advantages of OOP
- Modularity: Divide complex programs into smaller units.
- Code Reusability: Inheritance allows reuse of existing code.
- Maintainability: Easier to modify and maintain.
- Security: Encapsulation ensures data security.
- Extensibility: New features can be added without affecting existing code.

Conclusion
OOP concepts are the backbone of Java programming. By mastering Classes, Objects,
Encapsulation, Inheritance, Polymorphism, and Abstraction, developers can build
scalable, secure, and efficient applications.

You might also like