#Abstraction class in Java

1 messages · Page 1 of 1 (latest)

limber oceanBOT
#

@queen egret has a question:

rank1espada

what is the use of abstraction class in java?

#

<@&987246399047479336> please have a look, thanks.

#

Here is an AI assisted attempt to answer your question 🤖. Maybe it helps! In any case, a human is on the way 👍. To continue talking to the AI, you can use </chatgpt:1108714622413963314>.

#
TJ-Bot
What is the use of abstraction class in java?

An abstraction class in Java is a class that is designed to be a blueprint for other classes to inherit from. It contains abstract methods, which are declared but not implemented in the abstraction class itself. These methods must be implemented by any subclass that inherits from the abstraction class.

The main use of an abstraction class in Java is to enforce a specific structure or behavior in its subclasses. It allows for a common interface to be defined that all subclasses must adhere to, while still allowing each subclass to have its own unique implementation of the abstract methods.

Abstraction classes are often used in conjunction with interfaces to create a hierarchy of classes that share common characteristics but have different implementations. This can help make code more modular, easier to maintain, and more easily extensible.

Overall, abstraction classes help promote code reusability, encapsulation, and polymorphism in Java programs. They provide a way to define a set of rules or requirements that subclasses must follow, while still allowing for flexibility and customization in how those requirements are met.

#

@queen egret

An abstract class denotes a yet unfinished class. For example, it might be only done to 70% but there are still 30% missing. As such, it is used to create a template for other classes that extend it. The extending class can then concentrate on only doing the remaining 30% since 70% are already done. This can greatly reduce code duplication.

In general, a class is abstract if it has an abstract method. An abstract method is a method that is missing any implementation:

abstract class Animal {
  String name;

  Animal(String name) {
    this.name = name;
  }

  String getName() { // regular method
    return name;
  }

  abstract void makeNoise(); // No method body
}

Since the class is not finished yet and makeNoise is lacking any implementation, it is impossible to create instances of it in that state:

Animal animal = new Animal("Buddy"); // Does not compile! Can not instantiate abstract class

However, we can use it as template for extending it and get the existing name-functionality for free (getName()). But then we are required to implement everything that is still abstract, i.e. incomplete like makeNoise:

class Dog extends Animal {
  Dog() {
    super("Buddy"); // all dogs are called Buddy
  }

  @Override
  void makeNoise() {
    System.out.println("Wuff Wuff");
  }
}

And now we can create instances of it and use the methods:

Dog dog = new Dog();
System.out.println(dog.getName()); // Prints Buddy
dog.makeNoise(); // Prints Wuff Wuff
blissful trout
#

But also you can say Animal a = new Dog(); and the rest will still produce the same results.