Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

what is the need to declare a class abstract if it doesn't contain abstract methods?

 
preethi kumar
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can anyone tell me?
 
Hunter McMillen
Ranch Hand
Posts: 492
Firefox Browser Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Abstract classes can't be instantiated(you can't create objects of their type), but they can be subclassed. Which means that another class could inherit all of its methods and attributes; whether those methods are abstract or not is up to the designer.

Example: Assume Java doesn't actually have a Math class


The PI constant in the shape class can be used by the Circle class. This is a poor example because Java provides a Math class that holds constants like PI.

Sun examples:
http://java.sun.com/docs/books/tutorial/java/IandI/abstract.html

Hunter
 
preethi kumar
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
we could have done the same thing without declaring the shape class abstract right?

whats is the difference it made?
 
Hunter McMillen
Ranch Hand
Posts: 492
Firefox Browser Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could have yes. But in this case Shape was abstract because you wouldn't just want to create a generic Shape object. You wouldnt know how to find the volume of a "Shape", or its circumference, etc..


Hunter
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64827
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Remember, just because you can do something doesn't mean that you should.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:Remember, just because you can do something doesn't mean that you should.

I can reply to this.
 
preethi kumar
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you so much
 
preethi kumar
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ya marc you can reply
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic