Forums Register Login

Abstract class

+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
No, you can't do that. Atleast one method should be abstract.
+Pie Number of slices to send: Send
 

Atleast one method should be abstract.


What makes you think so ? I think you may have abstract classes without abstract methods.
+Pie Number of slices to send: Send
 

Originally posted by Sasi Kanth:
No, you can't do that. Atleast one method should be abstract.



No, that is not true.
All methods of an abstract class can be fully implemented.
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
 

Can anybody give me the reference from where I can verify this.



Ever written some Java code before?
+Pie Number of slices to send: Send
 

Can anybody give me the reference from where I can verify this.


Just make one !

If you change your name according to the naming policy, I'll make one for you

http://www.javaranch.com/name.jsp
[ March 01, 2007: Message edited by: Satou kurinosuke ]
+Pie Number of slices to send: Send
Thanks for your bashing.
I am quite new to Java and especially abstract class.
It compelled me to write the code myself.
Its valid and is working fine
+Pie Number of slices to send: Send
hello ranchers,
I have a small doubt what actually the use of abstract classes in JAVA? can anybody give me breif example? where it's useful?
+Pie Number of slices to send: Send
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.

shahid
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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?
+Pie Number of slices to send: Send
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;
}

}
+Pie Number of slices to send: Send
Probably reading my post above should asnwer your query.
+Pie Number of slices to send: Send
"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.

Have a look at a few design ideas:Open Closed Principal
Design By Contract
+Pie Number of slices to send: Send
hi

Abstract class must have atleast one abstract method.

You can verify it by Book Complete Refrence Java2.0 or From SCJP Khalid Mugal.
+Pie Number of slices to send: Send
 

Originally posted by Gaurav Pavan Kumar Jain:
hi

Abstract class must have atleast one abstract method.

You can verify it by Book Complete Refrence Java2.0 or From SCJP Khalid Mugal.



A better way would be to write an abstract class without any abstract methods. Try to complie it and see if it runs.
+Pie Number of slices to send: Send
Abstract classes can have 0 methods, 0 abstract methods...
An interesting example:


import static java.lang.System.out;
public abstract class TestAbstract {

public static void main(String[] args) {
out.println("test");


}

}

The code compiles and runs !
+Pie Number of slices to send: Send
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:

abstract void moveTo(double deltaX, double deltaY);

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();
}


This is from abstract classes and methods tutorial http://java.sun.com/docs/books/tutorial/java/IandI/abstract.html

regards
SADASIVAKUMAR UTTI
SCJP1.4
+Pie Number of slices to send: Send
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.
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
https://gardener-gift.com


reply
reply
This thread has been viewed 931 times.
Similar Threads
difference between interface and abstract classes
Difference between abstract and interfaces
Abstract class
Another abstract and method question.....
Difference between abstract and interface,not Syntactical diff
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 29, 2024 08:11:21.