I am confused. I got excited when I got to know that abstract basically means the class is hidden and no one can instantiate it. Cool. But when I got to know that someone can come and extend to my class and just instantiate it, use all the methods, what is the point of making a class abstract when someone can just steal all your qualities, act like you and sell himself in the market.
If anyone could help me with explaining this, I would be grateful.
CodePudding user response:
If you want to prevent others from inheriting from your class make it final
. But than its the last of its hierarchy, so it can no longer be inherited and so to be declared abstract
(because its obsolete).
Abstract classes are templates. They inherit properties and functions without implementing them concretely (you can of course give a default behavior for all, but the idea behind abstract
in a declared method is to be abstract
-> not implemented) and only serve to build up a hierarchy. Example:
abstract class Vehicle {...}
class Car extends Vehicle {...}
class Bike extends Vehicle {...}
Vehicle
descibes common properties but a concret instantiation is too vague. We call all types of vehicles 'Vehicle', even if we never have seen one of them purely in nature. It's a thought construct that makes it easier to classify concrete forms like a car or a bike.
CodePudding user response:
Abstract class basically means hiding the implementation details and showing only functionality to the user.
Abstract class can have following method.
Abstract Method
Non Abstract Method After Java 8 they can also have
static method
final method
Abstract class A{
abstract void run(); //Abstract Method
}
class B extends A{
void run(){ System.out.println("running safely"); }
}
Above you can see run() method doesn't have any implementation in class A.
It cannot be instantiated because of this only as there is no implementation of run() in class A
so to make it work it need to be extended and implemented (that's what done in class B).
In outer world people can only see run() they will not able to get how run works or actual implementation of run() method.
refer this link for more information: abstract class in java
CodePudding user response:
Abstract classes are not hidden. Abstract classes are "incomplete" classes which can be "completed" in many different ways. If you have classes that contain a lot of similar code, it may be a good idea to move all the similar code into the abstract class and make your classes extend this abstract class.
Abstract classes are otherwise quite full-featured. They can have constructors even, despite the fact that you can't instantiate them. These constructors can be called via the super
keyword from child class constructors:
abstract class Parent {
private String name;
public Parent(String name) {
this.name = name;
}
public String getName() {
return name;
}
}
class Child extends Parent {
private boolean hasMilkTeeth;
public Child(String name, boolean hasMilkTeeth) {
super(name); // calls the parent constructor!
this.hasMilkTeeth = hasMilkTeeth;
}
public boolean hasMilkTeeth() {
return hasMilkTeeth;
}
}