1.Class
A class is a user-defined blueprint or prototype from which objects are created. It contains fields (variables) and methods to define the behavior of objects.
Why:
Classes provide a structured way to group related data and behavior. It promotes reusability and modularity in code.
class Car {
String color;
void drive() {
System.out.println("Car is driving");
}
}
2.Global and Local Variables
Global Variable (Instance Variable): Declared inside the class but outside any method. Accessible to all methods of the class.
Local Variable: Declared inside a method or block. Limited scope to that block.
Why:
Global variables maintain object state.
Local variables support temporary operations.
class Test {
int globalVar = 10;
void show() {
int localVar = 5;
System.out.println(globalVar + localVar);
}
}
3.Data Types
Specify the type of data a variable can hold.
Primitive: byte, short, int, long, float, double, char, boolean.
Non-Primitive: String, Array, Class, Interface.
Why:
To define memory size and the type of operation allowed on the data.
int age = 25;
float weight = 68.5f;
char grade = 'A';
boolean isValid = true;
4.Package
A package is a namespace that organizes a set of related classes and interfaces.
Why:
Avoids name conflicts and helps manage classes efficiently.
package mypackage;
import java.util.Scanner;
5.Object
An object is an instance of a class.
Why: To access the fields and methods defined in the class.
Car myCar = new Car();
myCar.drive();
6.Constructor
A special method that initializes objects. It has the same name as the class and no return type.
Why: To set initial values for object fields.
class Bike {
Bike() {
System.out.println("Bike created");
}
}
7.Constructor Chaining
Calling one constructor from another in the same class using this() or from superclass using super().
Why: Reuse code and ensure consistent initialization.
class Car {
Car() {
this(100);
System.out.println("Default constructor");
}
Car(int speed) {
System.out.println("Speed: " + speed);
}
}
8.Inheritance
Mechanism where a new class acquires the properties and behaviors of a parent class using extends.
Why: Code reuse and hierarchical classification.
class Animal {
void eat() {
System.out.println("Eating...");
}
}
class Dog extends Animal {
void bark() {
System.out.println("Barking...");
}
}
9.Abstraction
Hiding implementation details and showing only the functionality.
Why: Increases code security and reduces complexity.
Using abstract class or interface.
abstract class Shape {
abstract void draw();
}
10.Polymorphism
Ability of an object to take many forms. Two types: compile-time (method overloading) and run-time (method overriding).
Why: Flexibility and dynamic behavior.
class Animal {
void sound() {
System.out.println("Animal sound");
}
}
class Dog extends Animal {
void sound() {
System.out.println("Dog barks");
}
}
11.Encapsulation
Binding data (variables) and methods into a single unit (class).
Why: Protects data from outside interference and supports data hiding.
class Person {
private int age;
public void setAge(int a) { age = a; }
public int getAge() { return age; }
}
12.Interface
A reference type in Java, similar to a class, that can contain only abstract methods (Java 8+ allows default/static methods).
Why: To achieve full abstraction and multiple inheritance.
interface Animal {
void eat();
}
class Dog implements Animal {
public void eat() {
System.out.println("Dog eats");
}
}
13.this vs super
this: Refers to the current class object.
super: Refers to the immediate parent class object.
Why: Resolve naming conflicts, call parent class methods or constructors.
class A {
int x = 10;
}
class B extends A {
int x = 20;
void display() {
System.out.println(this.x); // 20
System.out.println(super.x); // 10
}
}
14.this() vs super()
this(): Calls a constructor in the same class.
super(): Calls a constructor in the parent class.
Why: Constructor reuse and initialization.
class Parent {
Parent() {
System.out.println("Parent constructor");
}
}
class Child extends Parent {
Child() {
super();
System.out.println("Child constructor");
}
}
15.final Keyword
Used to make a variable constant, prevent method overriding, or inheritance.
Why: Enforce immutability and restriction.
final int x = 100; // can't be changed
final void show() {} // can't be overridden
final class A {} // can't be extended
16.static vs non-static
static: Belongs to class, not object. Shared across all instances.
non-static: Belongs to individual objects.
Why: To share common data or methods.
class Demo {
static int count = 0;
int id;
Demo(int id) {
this.id = id;
count++;
}
}
Top comments (0)