Java Object-Oriented Programming Concepts
1. Object-Oriented Programming (OOP)
OOP is a paradigm based on the concept of "objects," which contain data (attributes) and methods (functions). Java is an object-oriented language.
2. Class, Objects, Attributes, and Methods
- Class: Blueprint for creating objects.
public class Student {
String name;
int rollNo;
void printDetails() {
System.out.println(name + ", " + rollNo);
}
} - Object: An instance of a class.
Student s1 = new Student();
- Attributes: Variables defined within a class (e.g.,
name
,rollNo
). - Methods: Functions defined within a class (e.g.,
printDetails()
).
3. The new
Keyword
Used to create new objects:
Student s1 = new Student();
4. Encapsulation
Encapsulation bundles data (attributes) and methods inside a class, protecting internal state by using private fields and public getters/setters.
class Programmer {
private String name;
public String getName() { return name; }
public void setName(String name) { this.name = name; }
}
5. Static Method
A static method belongs to the class rather than any object instance. Called via the class name.
public class MathUtil {
public static int add(int x, int y) { return x + y; }
}
int sum = MathUtil.add(2, 3);
6. Java Constructor
A constructor initializes objects. It has no return type and shares the class name.
class Student {
String name;
Student(String n) {
this.name = n;
}
}
Student s1 = new Student("Ram");
7. this
Keyword
Refers to the current object, helping differentiate instance variables from parameters:
public class Point {
int x;
Point(int x) {
this.x = x;
}
}
8. Java Inheritance (extends
)
Inheritance allows a subclass to inherit fields and methods from a superclass.
class Animal { void eat() {} }
class Dog extends Animal { void bark() {} }
Superclass: The class being inherited from.
Subclass: The class that inherits.
9. final
Keyword
- final variable: Value cannot change (constant).
- final method: Cannot be overridden.
- final class: Cannot be subclassed.
final int MAX_SIZE = 100;
final void display() {}
final class Base {}
10. Polymorphism
The ability for different classes to provide different implementations of methods with the same name.
Compile-time polymorphism: Method overloading.
Run-time polymorphism: Method overriding.
class Animal { void sound() { System.out.println("Generic Animal"); } }
class Dog extends Animal { void sound() { System.out.println("Bark"); } }
Animal a = new Dog();
a.sound(); // Output: Bark
11. Inner Class
A class declared inside another class for better encapsulation.
class Outer {
class Inner { void display() {} }
}
Outer.Inner in = new Outer().new Inner();
12. Abstract Class
Cannot be instantiated. Can have both abstract (no implementation) and concrete methods.
abstract class Animal {
abstract void makeSound();
void sleep() { System.out.println("Sleeping"); }
}
class Dog extends Animal {
void makeSound() { System.out.println("Bark"); }
}
13. Interface
Defines abstract methods for full abstraction (Java 8+: default and static methods allowed).
interface Drawable {
void draw();
}
class Circle implements Drawable {
public void draw() { System.out.println("Drawing Circle"); }
}
14. super
Keyword
Refers to the superclass’s constructor or methods/fields.
class Animal { void eat() { System.out.println("eat"); } }
class Dog extends Animal {
void eat() { super.eat(); }
}
15. Constant
Use the final
keyword for constants:
final double PI = 3.14159;
16. Enum
Special classes representing a group of constants.
enum Day { MONDAY, TUESDAY, WEDNESDAY }
Day d = Day.MONDAY;