Concrete Class in Java – Definition, Syntax, and Example


2. Abstract Class in Java

In Java, an abstract class cannot be instantiated and is meant to be extended by other classes. It provides a common base and allows some methods to be abstract, so subclasses implement them.

  • Can have abstract methods (no body)
  • Can have concrete methods (with body)
  • Can have constructors, fields, and static methods
  • Can implement interfaces
  • Declared with the abstract keyword

Key Points

  • Classes with abstract methods must be declared abstract
  • Abstract methods end with a semicolon, no body
  • Subclasses must override abstract methods or be abstract
  • Abstract classes can have constructors

Syntax

abstract class ClassName {
    abstract void display();  // abstract method
    void show() {            // concrete method
        System.out.println("Concrete method");
    }
}

Example

abstract class Animal {
    abstract void sound();
    void sleep() {
        System.out.println("Sleeping...");
    }
}

class Dog extends Animal {
    @Override
    void sound() {
        System.out.println("Bark");
    }
}

public class Main {
    public static void main(String[] args) {
        Dog d = new Dog();
        d.sound();  // Bark
        d.sleep();  // Sleeping...
    }
}

Output

Bark
Sleeping...


Frequently Asked Questions (FAQ)

What is an abstract class in Java?
A class that cannot be instantiated and may contain abstract methods to be implemented by subclasses.
Can we create objects of an abstract class?
No, abstract classes cannot be instantiated directly.
Can abstract classes have constructors?
Yes, constructors in abstract classes are called when subclass objects are created.
Must subclasses override all abstract methods?
Yes, otherwise they must also be declared abstract.
Can abstract classes implement interfaces?
Yes, they can implement interfaces.

Singleton Class in Java – Definition, Syntax, and Example

Singleton Class in Java – Definition, Syntax, and Example

Definition:

A Singleton Class in Java is a design pattern that ensures only one instance of the class exists in the JVM. It provides a global point of access to that instance.

Key Points:

  • Only one instance is created during the entire lifecycle.
  • Private constructor to prevent external instantiation.
  • Static method to provide access to the single instance.

Syntax Example:

public class Singleton {
    private static Singleton instance;

    private Singleton() {
        // private constructor
    }

    public static Singleton getInstance() {
        if (instance == null) {
            instance = new Singleton();
        }
        return instance;
    }
}

public class Main {
    public static void main(String[] args) {
        Singleton obj1 = Singleton.getInstance();
        Singleton obj2 = Singleton.getInstance();
        System.out.println(obj1 == obj2); // true
    }
}

Advantages:

  • Controlled access to the single instance.
  • Reduces memory footprint.
  • Ensures consistent state across application.

Limitations:

  • Difficult to test with multiple instances.
  • Can introduce global state issues.

❓ Frequently Asked Questions

Q1: Can a Singleton class have parameters in the constructor?

No. Singleton constructors are private and parameters are not allowed for external calls.

Q2: Is Singleton thread-safe by default?

No, you need to implement synchronization for multi-threaded environments.

Q3: Can Singleton be broken by serialization?

Yes, unless you override readResolve() method.

Comments

Popular posts from this blog

Introduction to Java

Java Classes in Depth – Complete Guide with Definitions, Syntax & Examples

Top AI Tools for Social Media Marketing in 2025 – Automate & Grow Your Brand