0

abstract classes

Have abstract classes a constructor ??

5th Jan 2020, 12:23 PM
Muqaddas
Muqaddas - avatar
16 Answers
+ 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 .
7th Jan 2020, 3:45 PM
Saroj Patel
Saroj Patel - avatar
+ 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 .
7th Jan 2020, 3:34 PM
Saroj Patel
Saroj Patel - avatar
+ 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
6th Jan 2020, 12:54 PM
Saroj Patel
Saroj Patel - avatar
+ 3
I think the same way. Its happen because of there is no instance variables available in interface declaration and class variables are final.
7th Jan 2020, 5:43 PM
Lev Tolstoy SPB
Lev Tolstoy SPB - avatar
+ 2
The next interesting question why does interface cant have constructor? 😉
7th Jan 2020, 3:30 PM
Lev Tolstoy SPB
Lev Tolstoy SPB - avatar
+ 1
Yes abstract classes have constructor because after all they are classes, isn't it.
5th Jan 2020, 12:40 PM
Avinesh
Avinesh - avatar
+ 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(); } }
5th Jan 2020, 1:04 PM
Vikram Singh
Vikram Singh - avatar
+ 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.
7th Jan 2020, 7:48 PM
Aleksandar Nedeljkovic
Aleksandar Nedeljkovic - avatar
+ 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).
7th Jan 2020, 7:50 PM
Aleksandar Nedeljkovic
Aleksandar Nedeljkovic - avatar
+ 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?
7th Jan 2020, 7:52 PM
Aleksandar Nedeljkovic
Aleksandar Nedeljkovic - avatar
+ 1
Maybe some hidden code or backdoor kode? 😁
7th Jan 2020, 7:53 PM
Aleksandar Nedeljkovic
Aleksandar Nedeljkovic - avatar
0
any examples ?
5th Jan 2020, 12:42 PM
Muqaddas
Muqaddas - avatar
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.
5th Jan 2020, 12:48 PM
Avinesh
Avinesh - avatar
0
okay. thank you very much đŸ‘ŒđŸ»
5th Jan 2020, 12:49 PM
Muqaddas
Muqaddas - avatar
0
yes. i can’t get it so well
5th Jan 2020, 12:53 PM
Muqaddas
Muqaddas - avatar
0
thanks i got it.
5th Jan 2020, 1:17 PM
Muqaddas
Muqaddas - avatar