+ 4

Can you implement an abstact method without extending it class ?

23rd Nov 2018, 8:26 PM
Mind To Machine 💻🕆
Mind To Machine 💻🕆 - avatar
9 Antworten
+ 10
Not really. But you can use an anonymous class that implements the abstract method to avoid a boilerplate class. Something like AbstractClass clazz = new AbstractClass(){ void method(){ // your implementation } }; clazz.method(); // works
23rd Nov 2018, 8:51 PM
Tashi N
Tashi N - avatar
+ 8
Mind To Machine 🤖😎 Nice. So writing this reply was a waste of time 😑
23rd Nov 2018, 8:57 PM
Tashi N
Tashi N - avatar
+ 7
If a class have abstract methods, then the class should also be abstract using abstract keyword, else it will not compile.
23rd Nov 2018, 8:58 PM
Danijel Ivanović
Danijel Ivanović - avatar
+ 7
No is the correct answer 🙄 The anonymous class extends the abstract class by implementing its methods. You just spare the extends keyword.
23rd Nov 2018, 9:02 PM
Tashi N
Tashi N - avatar
+ 1
i knew that😉
23rd Nov 2018, 8:54 PM
Mind To Machine 💻🕆
Mind To Machine 💻🕆 - avatar
+ 1
Tashi N i was merely trying to see if someone would say no, so i can say what you said😊
23rd Nov 2018, 8:59 PM
Mind To Machine 💻🕆
Mind To Machine 💻🕆 - avatar
+ 1
Tashi N oh...i see
23rd Nov 2018, 9:09 PM
Mind To Machine 💻🕆
Mind To Machine 💻🕆 - avatar
0
honestly the idea of instintiating an abstract class corrupts the purpose. can you hack it? sure. however, if you find yourself needing to implement an abstract class, then you either need to rethink making it abstract and maybe changing your design. or maybe you need to rethink how you are using it. typically abstract classes are designed that way for a reason. if they contain functionality that uou need then maybe you should go more of a composition route than an inheritance. just food for thought.
8th Dec 2018, 3:24 AM
John Anderson
John Anderson  - avatar
0
Why we mast to ensign the class, if we out of print only one string or without out of printing?
8th Dec 2018, 11:35 AM
Viktor Polishchuk
Viktor Polishchuk - avatar