0
abstract classes
Have abstract classes a constructor ??
16 Respuestas
+ 10
suppose let it be interface have constructor but if the constructor is public then it is possible to create object of interface and if it is private then how implementation class call that constructor using super .
+ 9
i think the interface is just a specification and the variable present inside interface is always public static final that means no way related to object . and final static variable always in initialized form .
+ 8
Need of constructor in an abstract class :-
Assume that.. in an abstract class there are 1000 property is there and suppose for every implementation class 990 property are common to use
and let be there are 10000 implementation class is there so assign that 990 property for each implementation class is a big work so we can assign that common property in that abstract class only by taking a constructor inside that abstract class and in all implementation class we can call and pass value by using
super(value1,value2,value3......value990)
so that code readability is done and less code we have to required
+ 3
I think the same way. Its happen because of there is no instance variables available in interface declaration and class variables are final.
+ 2
The next interesting question why does interface cant have constructor? 😉
+ 1
Yes abstract classes have constructor because after all they are classes, isn't it.
+ 1
Abstract class: is a restricted class that cannot be used to create objects (to access it, it must be inherited from another class).
Abstract method : can only be used in an abstract class, and it does not have a body. The body is provided by the subclass (inherited from).
for example
// Abstract class
abstract class Animal {
// Abstract method (does not have a body)
public abstract void animalSound();
// Regular method
public void sleep( ) {
System.out.println("Zzz");
}
}
// Subclass (inherit fromAnimal)
class Pig extends Animal {
public void animalSound() { // The body of animalSound() is provided here
System.out.println("The pig says: wee wee");
}
}
class MyMainClas {
public static void main(String[] args) {
Pig myPig = new Pig(); // Create a Pig object
myPig.animalSound();
myPig.sleep();
}
}
+ 1
I agree with Lev Tolstoy SPB - public static FINAL - which means that this class haven't got access to object(s). No way. BTW, final is a keyword for that - you can create class and put wall around class by adding FINAL.
+ 1
And, of course, abstract class borrow constructor from classes who call abstract class. For example,I use abstract class for some calculations (normally, they represent module or function with pointer).
+ 1
After all, you can create local constructor (like human virus for instance). But, if you do that, what was purpose of that abstract class?
+ 1
Maybe some hidden code or backdoor kode? 😁
0
any examples ?
0
You cannot create an object of the abstract class so you have to make a call using the super() inside the child class constructor.
It is similar to write like non-abstract classes, just add the abstract keyword.
0
okay. thank you very much 👌🏻
0
yes. i can’t get it so well
0
thanks i got it.