Java OOPs (Object-Oriented Programming) Concepts
![]() |
Java OOPs Concepts
Java OOPs (Object-Oriented Programming) Concepts
- Class
- Object
- Inheritance
- Polymorphism
- Abstraction
- Encapsulation
1. Class in Java
A class in Java is a blueprint or template for creating objects. It defines fields (variables) and methods that represent the properties and behaviors of the object. Think of a class like a house blueprint — it defines the design (rooms, doors, windows), but the actual house is the object built from that blueprint.
Syntax of a Class
class ClassName {
// Fields (variables)
dataType variableName;
// Methods (functions)
returnType methodName(parameters) {
// method body
}
}
Example – Creating and Using a Class
// Defining a class
class Car {
// Fields (Properties)
String brand;
int speed;
// Method (Behavior)
void displayDetails() {
System.out.println("Brand: " + brand);
System.out.println("Speed: " + speed + " km/h");
}
}
// Main class to test
public class Main {
public static void main(String[] args) {
// Creating an object of Car class
Car myCar = new Car();
// Assigning values
myCar.brand = "Toyota";
myCar.speed = 120;
// Calling method
myCar.displayDetails();
}
}
Output:
Brand: Toyota
Speed: 120 km/h
Key Points about Classes in Java
- Keyword: A class is created using the
class
keyword. - Naming Convention: Class names should start with an uppercase letter (Car, Employee).
- Members: Can have variables (fields) and methods.
- Objects: Created using the
new
keyword from a class. - Access Modifiers: Can be public, private, protected, or default.
- Constructor: A special method with the same name as the class used for initialization.
Example with Constructor
class Student {
String name;
int age;
// Constructor
Student(String n, int a) {
name = n;
age = a;
}
void display() {
System.out.println("Name: " + name);
System.out.println("Age: " + age);
}
}
public class Main {
public static void main(String[] args) {
// Creating objects with constructor
Student s1 = new Student("Amit", 21);
Student s2 = new Student("Priya", 22);
s1.display();
s2.display();
}
}
Output:
Name: Amit
Age: 21
Name: Priya
Age: 22
Advantages of Using Classes
- Code Reusability – Same class can be used to create multiple objects.
- Data Organization – Combines related variables and methods.
- Object-Oriented Design – Forms the basis of OOP concepts like Inheritance and Polymorphism.
- Encapsulation – Data and methods are bundled together.
❓ FAQs with Answers
Q1: What is encapsulation in Java?
A: It is the process of wrapping variables and methods into a single unit. It helps in data hiding and securing code.
Q2: What is inheritance in Java?
A: Inheritance allows one class to acquire properties and methods of another class, enhancing code reusability.
Q3: What is the difference between method overloading and overriding?
- Overloading: Same method name, different parameters in the same class.
- Overriding: Redefining the parent method in a subclass.
Q4: What is abstraction and how is it implemented?
A: Abstraction hides complexity and exposes only relevant parts. It is implemented via abstract classes and interfaces.
Q5: Interface vs Abstract Class?
Feature | Abstract Class | Interface |
---|---|---|
Methods | Abstract and Concrete | Only Abstract (till Java 7), default/static (from Java 8) |
Multiple Inheritance | Not Supported | Supported |
Constructor | Yes | No |
Access Modifiers | Any | Public Only |
Q6: Why is OOP important in Java?
A: It promotes modularity, scalability, and maintainability. It models real-world problems more naturally.
Java OOPs