• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

Doubt on Abstract classes

 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 195
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?

 
Nalini Reddy
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 195
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 195
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Ranch Hand
Posts: 5093
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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...).
 
Vijayendra V Rao
Ranch Hand
Posts: 195
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank You All.
 
Jeroen Wenting
Ranch Hand
Posts: 5093
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.

 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
By the way, this is probably simpler to try than to muse about.

The following should compile without any problems:

 
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 33
Netbeans IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.)
 
Ilja Preuss
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1272
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 33
Netbeans IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Yeast devil! Back to the oven that baked you! And take this tiny ad too:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic