Difference between abstract class and non abstract class in Java

Vita 140 Published: 11/05/2024

Difference between abstract class and non abstract class in Java

I'm happy to help!

In Java, a class can either be abstract or concrete (non-abstract). An abstract class is a blueprint for other classes that can inherit its properties and behavior, whereas a non-abstract class represents an individual object.

A key difference between the two is that an abstract class cannot be instantiated on its own, whereas a non-abstract class can be used to create objects.

An abstract class in Java must have at least one abstract method (a method declared without implementation). This means that if you declare a class as abstract, it must contain at least one method without implementation. Abstract classes are intended to be inherited by other classes, which then provide the necessary implementation for those methods.

Here are some key characteristics of an abstract class in Java:

An abstract class cannot be instantiated on its own. You can't create objects from an abstract class. An abstract class must contain at least one abstract method (a method declared without implementation). Abstract classes are intended to be inherited by other classes, which then provide the necessary implementation for those methods. An abstract class can have both abstract and non-abstract methods.

On the other hand, a non-abstract class is also known as a concrete class. It represents an individual object with specific properties and behavior. Non-abstract classes are intended to be instantiated on their own or used to create objects of that class.

Here are some key characteristics of a non-abstract (concrete) class in Java:

A non-abstract class can be instantiated on its own, which means you can create objects from that class. A non-abstract class does not need to contain abstract methods. Non-abstract classes represent individual objects with specific properties and behavior. A non-abstract class can inherit properties and behavior from an abstract class or another non-abstract class.

To illustrate the difference, consider a hierarchy of classes representing different types of vehicles. An abstract class Vehicle might contain abstract methods for things like startEngine() and stopEngine(), which would need to be implemented differently by subclasses such as Car, Truck, and Motorcycle. The Vehicle class itself cannot be instantiated on its own, but it provides a blueprint for other classes that can inherit its properties and behavior.

In contrast, a non-abstract class Car might represent an individual object with specific properties like color, make, and model, as well as methods like startEngine() and stopEngine() implemented specifically for that type of vehicle. A Car object could be instantiated on its own and used to create objects.

In summary, abstract classes in Java are blueprints for other classes that can inherit their properties and behavior, whereas non-abstract (concrete) classes represent individual objects with specific properties and behavior.

How do you create an abstract class in Java Eclipse?

To create an abstract class in Java using Eclipse, follow these steps:

Step 1: Create a new Java Class

Open Eclipse and navigate to the package where you want to create your abstract class. Right-click on the package and select New > Class from the context menu.

In the "New Java Class" dialog box, enter the name of your abstract class (e.g., "AbstractClass") in the "Name" field. Make sure to check the "Public" option if you want the class to be accessible from other parts of your program.

Click "Finish" to create the new class file.

Step 2: Declare the Class as Abstract

In the Java editor, add the abstract keyword before the class keyword in your class definition. For example:

public abstract class AbstractClass {

// ...

}

The abstract keyword indicates that this class cannot be instantiated on its own and is intended to serve as a base class for other classes.

Step 3: Define Abstract Methods (Optional)

If you want your abstract class to contain abstract methods, declare them using the abstract keyword followed by the return type and method name. For example:

public abstract class AbstractClass {

public abstract void someMethod();

}

Abstract methods are methods that are declared but not implemented in the abstract class. Subclasses must provide their own implementation of these methods.

Step 4: Add Concrete Methods (Optional)

If your abstract class contains concrete methods (methods with a body), you can add them as usual:

public abstract class AbstractClass {

public void someMethod() {

// Method body...

}

}

Concrete methods in an abstract class are inherited by subclasses and can be used without modification.

Step 5: Create Subclasses

To use your abstract class, create subclasses that extend the abstract class:

public class ConcreteClass extends AbstractClass {

@Override

public void someMethod() {

// Subclass implementation...

}

}

Subclasses must provide their own implementation of any abstract methods declared in the abstract class.

Step 6: Use Your Abstract Class

You can use your abstract class as a base for other classes, and its concrete methods and abstract methods can be inherited or overridden by subclasses:

public class MainClass {

public static void main(String[] args) {

ConcreteClass concrete = new ConcreteClass();

concrete.someMethod(); // Calls the subclass implementation...

}

}

That's it! By following these steps, you've created an abstract class in Java using Eclipse.