Can I declare a class as abstract without declaring any of the class methods as abstract (i.e. all methods of the abstract class is fully defined). Kindly respond
Thanks a lot. Even I am of the view that a class can be declared as abstract without declaring any of the methods as abstract. Can anybody give me the reference from where I can verify this.
Wasimullaha an abstract class may or may not have abstract method.but it is not a good programming practice.if you put abstract key word at the class header then your class is taken as abstract.you can't call its constructor.because abstract class means incomplete class.but an abstract class have constrouctor.but you can't call it. its sole purpose & mission is to be extended.so you can't put private access specifier in the abstract method if any.better you go and try it on your machine.
Abstract classes are also used skeletan implementors. Consider the AbstractMap class in the java.util. This class provides implementations of almost all the methods of the Map interface. So in case someday there's any other method in the Map interface. You can override the method in this Class only and the descendants of the abstract class would have method. Though all classes not extending the abstract class would still be broken if they do not override the method. It would only lessen your overhead in such scenarios.
I mean whats the use of abstract classes doing this without the method implementation, and use abstract as a key word. why we should do all of this in abstract classes? Using abstract classes how much its useful?
you can use abstract classes to make generic stuff that more specific subclasses will use. For example say you have a abstract Car class , you could have concrete subclasses as Ferrari360, FordKa.
See below in the example every Car subclass needs to be able to goFaster() but how this is done is different for diff car subclasses so the subclasses would need to define that for them selves.
However every car subclass has a current gear which can be defined in the abstract class as it is generic and the same for all subclasses, so that could be done in the abstract Car class.
abstract class Car{
int currentGear;
abstract void goFaster();
public int getCurrentGear(){ return currentGear; }
"where it's useful?" - It's all about trying to write software that is maintainable, so you need some kind of flexibility in your design. You know that requirements are going to change and when the project gets into the maintaince phase someone is going to be adding extra functionality or making changes to existing code.
An abstract class is a class that is declared abstract�it may or may not include abstract methods. Abstract classes cannot be instantiated, but they can be subclassed. An abstract method is a method that is declared without an implementation (without braces, and followed by a semicolon), like this:
If a class includes abstract methods, the class itself must be declared abstract, as in: public abstract class GraphicObject { // declare fields // declare non-abstract methods abstract void draw(); }
Abstract classes are used when you want to share a common implemnetaion acorss the sub class but still sub class can have few differences. Car | ------------------------- | | Automatica Manual
Now, Car has implementation of runs() method as both types would use the same style to run. But the way it starts i.e. gear() is abstract and is very specific to the type of Car.
Post by:autobot
We don't have time for this. We've gotta save the moon! Or check this out:
a bit of art, as a gift, the permaculture playing cards