1. Class and Object
A class is a template. An object is a real instance created from the class.
class Car {
String color;
void drive() {
System.out.println("Driving...");
}
}
public class Main {
public static void main(String[] args) {
Car myCar = new Car(); // Object creation
myCar.color = "Red";
myCar.drive();
}
}
2. Encapsulation
Encapsulation = hiding data using private
+ access via public
methods.
class Employee {
private int salary;
public void setSalary(int s) {
salary = s;
}
public int getSalary() {
return salary;
}
}
✅ Benefits: Data protection, controlled access, easy maintenance.
3. Inheritance
Allows a class (child) to inherit from another (parent) using extends
.
class Animal {
void eat() {
System.out.println("Eating...");
}
}
class Dog extends Animal {
void bark() {
System.out.println("Barking...");
}
}
✅ Benefits: Code reuse, method overriding.
4. Polymorphism
➤ A. Method Overloading (Compile-time)
Same method name with different parameters.
class Math {
int add(int a, int b) {
return a + b;
}
double add(double a, double b) {
return a + b;
}
}
➤ B. Method Overriding (Runtime)
Child class redefines parent method using @Override
.
class Animal {
void sound() {
System.out.println("Animal sound");
}
}
class Cat extends Animal {
@Override
void sound() {
System.out.println("Meow");
}
}
✅ Benefits: Flexibility (same interface, different behavior).
5. Abstraction
Hiding implementation and showing only essentials.
➤ A. Abstract Class
Can have abstract (no body) and concrete methods.
abstract class Shape {
abstract void draw(); // abstract
void display() {
System.out.println("Shape display");
}
}
class Circle extends Shape {
void draw() {
System.out.println("Drawing Circle");
}
}
➤ B. Interface
Only method declarations (Java 8+ allows default and static methods too).
interface Printable {
void print();
}
class Document implements Printable {
public void print() {
System.out.println("Printing...");
}
}
✅ Use: Interface → 100% abstraction; Abstract Class → partial abstraction.
6. this
Keyword
Refers to current object. Resolves variable ambiguity.
class Student {
String name;
Student(String name) {
this.name = name; // this differentiates field from parameter
}
}
7. static
Keyword
Belongs to the class, not objects.
class Counter {
static int count = 0;
Counter() {
count++;
System.out.println("Count: " + count);
}
}
✅ Shared by all objects.
8. final
Keyword
- Final variable → constant
- Final method → can't override
- Final class → can't be inherited
final class Constants {
final int VALUE = 10;
}
// final method
class A {
final void show() {}
}
class B extends A {
// void show() {} ❌ Error
}
9. super
Keyword
Refers to parent class methods/constructor.
class Parent {
void greet() {
System.out.println("Hello from Parent");
}
}
class Child extends Parent {
void greet() {
super.greet(); // calls Parent's greet
System.out.println("Hello from Child");
}
}
✅ Useful in overriding or constructor chaining.
10. Constructor Overloading
Multiple constructors with different parameters.
class Box {
int length;
Box() {
length = 0;
}
Box(int l) {
length = l;
}
}
11. Access Modifiers
Control visibility.
Modifier | Class | Package | Subclass | World |
---|---|---|---|---|
private |
✅ | ❌ | ❌ | ❌ |
(default) | ✅ | ✅ | ❌ | ❌ |
protected |
✅ | ✅ | ✅ | ❌ |
public |
✅ | ✅ | ✅ | ✅ |
Comments
Post a Comment