OOP JAVA DETAILED THEORY NOTES
1. Java Basics
Java is an object-oriented programming language used to create applications. It follows the concept
of classes and objects.
2. Class
A class is a blueprint used to create objects. It contains variables (data) and methods (functions).
3. Object
An object is an instance of a class. It is created using the 'new' keyword.
Example:
Student s1 = new Student();
4. Variables
Variables are used to store data.
Types: int, double, String, boolean.
5. Methods
Methods are functions inside a class used to perform actions.
Example: public void display() {}
6. Main Method
public static void main(String[] args) is the entry point of a Java program.
7. System.out.println()
Used to print output on the screen.
8. Constructor
A constructor is a special method used to initialize objects.
Rules:
- Same name as class
- No return type
- Called automatically when object is created
Example:
public Student(String name, int age) {
this.name = name;
this.age = age;
}
9. 'this' keyword
'this' refers to the current object.
10. Encapsulation
Encapsulation means hiding data using private variables and accessing them using public methods
(getters and setters).
Example:
private String name;
public String getName() { return name; }
11. Inheritance
Inheritance allows one class to inherit properties from another using 'extends'.
Example:
class Dog extends Animal {}
12. Polymorphism
Polymorphism means many forms.
Types:
- Method Overloading
- Method Overriding
Overloading Example:
void add(int a, int b)
void add(double a, double b)
Overriding Example:
Same method in parent and child class with different implementation.
13. Abstraction
Hiding implementation details and showing only functionality.
Achieved using abstract classes and interfaces.
14. Abstract Class
Cannot be instantiated and may contain abstract methods.
15. Interface
Contains abstract methods and used to achieve full abstraction.