aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Abstract class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Abstract class" Watch "Abstract class" New topic
Author

Abstract class

m wasimullah
Greenhorn

Joined: Mar 01, 2007
Posts: 4
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
AbhiRam Kalyan
Greenhorn

Joined: Oct 31, 2005
Posts: 25
No, you can't do that. Atleast one method should be abstract.


Sasikanth
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

Atleast one method should be abstract.

What makes you think so ? I think you may have abstract classes without abstract methods.


[My Blog]
All roads lead to JavaRanch
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
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.


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
m wasimullah
Greenhorn

Joined: Mar 01, 2007
Posts: 4
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.
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Can anybody give me the reference from where I can verify this.


Ever written some Java code before?
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

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 ]
m wasimullah
Greenhorn

Joined: Mar 01, 2007
Posts: 4
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
Rajesh Goud kumar
Greenhorn

Joined: Nov 29, 2006
Posts: 19
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?
shahid iqbal
Greenhorn

Joined: Mar 04, 2007
Posts: 3
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
Anupam Sinha
Ranch Hand

Joined: Apr 13, 2003
Posts: 1088
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.
Rajesh Goud kumar
Greenhorn

Joined: Nov 29, 2006
Posts: 19
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?
Louis Moloney
Ranch Hand

Joined: Feb 06, 2007
Posts: 59
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;
}

}


Anupam Sinha
Ranch Hand

Joined: Apr 13, 2003
Posts: 1088
Probably reading my post above should asnwer your query.
Peter Rooke
Ranch Hand

Joined: Oct 21, 2004
Posts: 794

"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


Regards Pete
Gaurav Pavan Kumar Jain
Ranch Hand

Joined: Mar 19, 2007
Posts: 168
hi

Abstract class must have atleast one abstract method.

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

Joined: Apr 13, 2003
Posts: 1088
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.
Amit Wadhwaa
Ranch Hand

Joined: Feb 15, 2007
Posts: 74
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 !


SCJP 5 94%<br /><a href="http://amit-wadhwa.blogspot.com/" target="_blank" rel="nofollow">My Blog</a>
sadasiva kumar
Ranch Hand

Joined: Oct 30, 2003
Posts: 91
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


SADASIVAKUMAR UTTI, SCJP1.4
A bend in the road is not the end of the road ... unless you fail to make the turn.
sushil bhogale
Greenhorn

Joined: Mar 13, 2007
Posts: 8
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Abstract class
 
Similar Threads
Another abstract and method question.....
difference between interface and abstract classes
Difference between abstract and interface,not Syntactical diff
Abstract class
Difference between abstract and interfaces