Java OOP Concepts Explained Simply: Class, Object, Inheritance, Polymorphism
Java OOP Concepts Explained Simply
Author: Gursehbaj Singh | Blog: DevMode
Object-Oriented Programming (OOP) is the heart of Java. It helps us create real-world programs using objects and classes. In this post, you will learn all main OOP concepts in a simple and clear way.
What is OOP?
OOP is a programming style where everything is represented as objects. Each object has data and behavior.
1. Class
A class is a blueprint of an object.
class Student {
String name;
int age;
void display() {
System.out.println(name + " " + age);
}
}
2. Object
An object is created from a class.
Student s1 = new Student();
s1.name = "Gursehbaj";
s1.age = 18;
s1.display();
3. Inheritance
Inheritance allows one class to use properties of another class.
class Animal {
void sound() {
System.out.println("Animal makes sound");
}
}
class Dog extends Animal {
void bark() {
System.out.println("Dog barks");
}
}
4. Polymorphism
Polymorphism means many forms.
class Calculator {
int add(int a, int b) {
return a + b;
}
int add(int a, int b, int c) {
return a + b + c;
}
}
5. Encapsulation
Encapsulation means wrapping data with methods.
class Account {
private int balance;
public void setBalance(int b) {
balance = b;
}
public int getBalance() {
return balance;
}
}
6. Abstraction
Abstraction means hiding internal details.
abstract class Shape {
abstract void draw();
}
class Circle extends Shape {
void draw() {
System.out.println("Drawing Circle");
}
}
Why OOP is Important?
- Code reusability
- Easy maintenance
- Better security
- Real-world modeling
Conclusion
OOP makes Java powerful and flexible. By learning class, object, inheritance, polymorphism, encapsulation and abstraction, you can build large and professional applications easily.
Comments
Post a Comment