• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Abstract class

 
m wasimullah
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, you can't do that. Atleast one method should be abstract.
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Atleast one method should be abstract.

What makes you think so ? I think you may have abstract classes without abstract methods.
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
m wasimullah
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can anybody give me the reference from where I can verify this.


Ever written some Java code before?
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1090
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1090
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Probably reading my post above should asnwer your query.
 
Peter Rooke
Ranch Hand
Posts: 847
1
Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"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
 
Gaurav Pavan Kumar Jain
Ranch Hand
Posts: 168
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1090
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 !
 
sadasiva kumar
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
sushil bhogale
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 Reply
  • Bookmark Topic Watch Topic
  • New Topic