It's not a secret anymore!*
The moose likes Beginning Java and the fly likes Doubt on Abstract classes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Doubt on Abstract classes" Watch "Doubt on Abstract classes" New topic
Author

Doubt on Abstract classes

Nalini Reddy
Greenhorn

Joined: Jul 01, 2004
Posts: 18
Hi,

I read ,an abstract class need not have any abstract methods .Can it have concrete methods and then be declared abstract? Can it also have variables?

Help me out soon.

Thanks in advance,
NaliniRedy.
Vijayendra V Rao
Ranch Hand

Joined: Jul 04, 2004
Posts: 195
Hi Nalini,

Abstract classes can have concrete methods and also variables. Suppose a class has 10 methods. Out of these, lets assume 9 are concrete methods and one is an abstract method. Even then, the class itself will have to be declared abstract.

Does this help or do you need some more explanation?



Vijayendra <br /> <br />"The harder you train in peace, the lesser you bleed in war"
Nalini Reddy
Greenhorn

Joined: Jul 01, 2004
Posts: 18
Hi Vijay,
I know we can have both concrete and abstract methods in an abstract class.actually when i was going through Whizlabs tutorial for SCJP,I read an abstract class even without having any abstract method can be called abstract.Is is possible that way ? If yes, can we have concrete methods in it (in which case it'll be a concrete class).I think my doubt is clear this time.
Vijayendra V Rao
Ranch Hand

Joined: Jul 04, 2004
Posts: 195
Originally posted by Nalini Reddy:
Hi Vijay,
I read an abstract class even without having any abstract method can be called abstract.


Was that class empty or did it have any methods? If that class had no methods, be it whether concrete methods or abstract methods, then it can be declared to be abstract. But when you add a concrete method, you will have to declare it to be a normal class...you can no longer have it as an abstract class.

Good that your doubt is cleared now
Nalini Reddy
Greenhorn

Joined: Jul 01, 2004
Posts: 18
Hi Vijay

If the class is empty, is it that it has no subclass or the subclass can have any methods with their own implementation.
Vijayendra V Rao
Ranch Hand

Joined: Jul 04, 2004
Posts: 195
Originally posted by Nalini Reddy:

If the class is empty, is it that it has no subclass or the subclass can have any methods with their own implementation.


If there is an abstract class and its empty, then even if a class extends this empty abstract class, it can have its own implementations. Thats the basic concept of inheritance itsn't it? The subclasses are free to add their own implementations that the super class has no knowledge about.
Jeroen Wenting
Ranch Hand

Joined: Oct 12, 2000
Posts: 5093
Originally posted by Vijayendra V Rao:

But when you add a concrete method, you will have to declare it to be a normal class...you can no longer have it as an abstract class.


incorrect.
When you have ANY abstract methods a class MUST be declared abstract.
When you have none, the class still MAY be declared abstract (this is why you can declare a class with no methods abstract...).


42
Vijayendra V Rao
Ranch Hand

Joined: Jul 04, 2004
Posts: 195
Originally posted by Jeroen Wenting:


incorrect.
When you have ANY abstract methods a class MUST be declared abstract.
When you have none, the class still MAY be declared abstract (this is why you can declare a class with no methods abstract...).


Did I mean something else? Could you please refer to my earlier posts? You have just put my words in another way...thats all
Nalini Reddy
Greenhorn

Joined: Jul 01, 2004
Posts: 18
Thank You All.
Jeroen Wenting
Ranch Hand

Joined: Oct 12, 2000
Posts: 5093
Originally posted by Vijayendra V Rao:


Did I mean something else? Could you please refer to my earlier posts? You have just put my words in another way...thats all


Not quite. You stated that any class that has non-abstract methods can't be abstract.
That's incorrect as I pointed out.
Maybe you meant to say something else, but you didn't.


But when you add a concrete method, you will have to declare it to be a normal class...you can no longer have it as an abstract class.
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
By the way, this is probably simpler to try than to muse about.

The following should compile without any problems:



The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Mike Gershman
Ranch Hand

Joined: Mar 13, 2004
Posts: 1272
Summarizing:

A class with one or more abstract methods MUST be abstract.

A class with no abstract methods CAN be abstract.

A final or anonymous class CAN NEVER be abstract.

Abstract classes can contain anything that concrete classes can contain.


The only consequence of declaring a class abstract is that that class cannot be instantiated. If the abstract class is extended by a concrete class then each time the concrete class is instantiated, the non-static, concrete members of the abstract class are instantiated in the same object with the non-static members of the concrete class.


Mike Gershman
SCJP 1.4, SCWCD in process
Harish Vembu
Ranch Hand

Joined: Jul 19, 2004
Posts: 33

Good Summarization by Mike.

But could we extend this thread by asking one more question as what exactly would be the difference between an abstract class and interface in Java?. Can One be used in place of the other? if they are similar. Can a interface be compiled into bytecodes?. can Abstract class be compiled?

Expecting your thoughts on this query?

Have a wonderful day

Thanks and Regards,

Harish.V


Harish
SCJP ,SCDJWS
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

Originally posted by Harish Vembu:

But could we extend this thread by asking one more question as what exactly would be the difference between an abstract class and interface in Java?.


An interface's methods must all be abstract; an abstract class can contain both abstract and non-abstract methods. Both are compiled to class files; the interface will contain only the small amount of bytecode used to initialize any class constants of reference type, whereas an abstract class file will contain the bytecodes that implement its non-abstract methods.

You should generally prefer interfaces for the flexibility they offer; the best use of an abstract class is as a default implementation of an interface (like the XXXAdapter classes in the java.awt.event package.)


[Jess in Action][AskingGoodQuestions]
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
The only (but big) difference between a fully abstract class and an interface is that you can implement multiple interfaces, but only extend one abstract class. That's actually why interfaces got introduced in Java: to allow a restricted form of multiple inheritance.
Mike Gershman
Ranch Hand

Joined: Mar 13, 2004
Posts: 1272
One other difference between fully abstract classes and interfaces that Tony Morris pointed out is that some library classes such as java.lang.reflect.Proxy expect an interface and won't accept an abstract class. A class specifying an interface can rely on the restrictions on interfaces - methods are public and fields are public static final.
Harish Vembu
Ranch Hand

Joined: Jul 19, 2004
Posts: 33

Thanks all for the explanation. Now its very clear. But there was a mention of Adapter classes as an example of Abstract classes implementing interfaces. Do they implement any? if so what?

Harish.V
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Doubt on Abstract classes