+ 1
What is actually abstract class used for?
9 Réponses
+ 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?
+ 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
+ 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'?
+ 1
yup David is correct, in every programming language there's a lot of ways to achieve the same purpose .
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?
0
yup bro
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?
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!
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?