What is actually abstract class used for? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

What is actually abstract class used for?

19th Jul 2017, 3:54 PM
Niko Prianto
Niko Prianto - avatar
9 Answers
+ 3
so beside an abstract class, there is an 'abstract answer' too? :D just kidding guys, I appreciate all your answers. but i still confused about the double work. is it just to make the code more structured?
19th Jul 2017, 6:36 PM
Niko Prianto
Niko Prianto - avatar
+ 1
Methods of Interfaces cannot have implementations. In other words, they can have only abstract or non-concrete methods. Abstract classes can have both abstract methods and concrete methods. This is why abstract classes are used to work as base class for other classes
19th Jul 2017, 4:05 PM
Nirmal Kumar Bhakat
Nirmal Kumar Bhakat - avatar
+ 1
thx for the answer, but i still dont get the idea of declaring an abstract method that absolutely do nothing, and then have to initiate the same method with the same name just to make it functional in the subclass? why do we bother to create a 'blueprint'?
19th Jul 2017, 6:07 PM
Niko Prianto
Niko Prianto - avatar
+ 1
yup David is correct, in every programming language there's a lot of ways to achieve the same purpose .
19th Jul 2017, 6:17 PM
Nirmal Kumar Bhakat
Nirmal Kumar Bhakat - avatar
0
niko im with you :D maby i dont know enough yet as im still learning java but there seems to be multiple ways to do the same thing. maby it has somthing to do with security of your code or to prevent errors?
19th Jul 2017, 6:14 PM
D_Stark
D_Stark - avatar
0
yup bro
19th Jul 2017, 6:37 PM
Nirmal Kumar Bhakat
Nirmal Kumar Bhakat - avatar
0
thanks kumar. i have one question again. If an abstract class described as a blueprint, then what's the difference with the polymorphism concept?
19th Jul 2017, 6:40 PM
Niko Prianto
Niko Prianto - avatar
0
@Nirmal as of java 8, interfaces *can* contain static and default methods... @Niko consider an abstract class to be a generalisation of properties and behaviours that all of it's subclasses must have. For instance, you could have an abstract class Animal which defines a method eat. You cannot instantiate an Animal, it must be a particular type of animal like Cat, Dog, etc. All animals must eat though (or they die) so you define the eat() method in Animal to force all it's subclasses to implement this method. Each subclass can then implement the eat() method in it's own way (like eat different foods): polymorphism!
22nd Jul 2017, 7:21 PM
marit vandijk
0
@marit ok lets make a simple example lets say i have a superclass called mammal. then i have dogs, lion, cats as the subclass. and inside the mammal superclass, it has 2 method : breed() and makesound(). the method breed() will print "giving birth". it wont be overided in the subclass because all mammals do giving birth. but the method makesound() can be overided in each subclass, because dog barks, lion roars and cat meows. my question is do we really need to create the abstract method : makesound() method in the superclass? or is it just a useless thing because it will never be called since it was overided?
22nd Jul 2017, 7:41 PM
Niko Prianto
Niko Prianto - avatar