DEV Community

P Mukila
P Mukila

Posted on

Java Core Concepts...

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");
    }
}
Enter fullscreen mode Exit fullscreen mode

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);
    }
}

Enter fullscreen mode Exit fullscreen mode

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;

Enter fullscreen mode Exit fullscreen mode

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;
Enter fullscreen mode Exit fullscreen mode

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();
Enter fullscreen mode Exit fullscreen mode

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");
    }
}

Enter fullscreen mode Exit fullscreen mode

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);
    }
}
Enter fullscreen mode Exit fullscreen mode

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...");
    }
}
Enter fullscreen mode Exit fullscreen mode

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();
}
Enter fullscreen mode Exit fullscreen mode

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");
    }
}
Enter fullscreen mode Exit fullscreen mode

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; }
}
Enter fullscreen mode Exit fullscreen mode

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");
    }
}
Enter fullscreen mode Exit fullscreen mode

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
    }
}
Enter fullscreen mode Exit fullscreen mode

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");
    }
}
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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++;
    }
}
Enter fullscreen mode Exit fullscreen mode

Top comments (0)