Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

What is an abstract class? (was: scjp)

 
saisreereddy surabhi
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi.
can anyone explan me about abstract class.......and i have no idea about abstract classes before
[ February 19, 2007: Message edited by: Henry Wong ]
 
Henry Wong
author
Marshal
Pie
Posts: 21405
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by saisreereddy surabhi:
hi.
can anyone explan me about abstract class.......and i have no idea about abstract classes before


An abstract class is a class that can't be instantiated. In general, you would want to make a class abstract, when some of the methods of the class are not implemented (abstract methods). This will allow your class to compile, but not directly used.

To use your abstract class, you would have to subclass from the abstract class, and implement the abstract methods.


For design reasons, there may also be other reasons why you don't want a class to be instantiated -- maybe certain methods has been implemented but incomplete.

Henry
 
saisreereddy surabhi
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
tanks henry.....
 
Jay Dilla
Ranch Hand
Posts: 201
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
also if you don't know about abstract classes you may want to research interfaces
and the relationship between abstract classes, interfaces, and the concrete classes that implement them

I heard there are questions on the test about abstract methods passed down the line thru interfaces and into concrete classes,etc
 
Faisal Ahmad
Ranch Hand
Posts: 355
Chrome Java Redhat
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Let us go by an example..
Imagine, you are asked to create horse, dog etc. Obviously, you would design classes like Horse, Dog etc. right? But, how many such individual classes would you design to match the entire animal kingdom? Wouldn't it be silly to repeat all the common(like eat, sleep, run etc) animal specific code for each class that you design? Smart you are.. you would simply pick out the common behavior and make it one class called "animal"! Yup, that's right.. you just want to put everything an animal is supposed to have in "animal" class. Now, imagine, someone is trying to create an instance of animal! Uh oh!! What would be that animal look like? Dog? Horse? Else what? There you are.. an animal class is not supposed to allow its instances. You should make be precise about your animal thing before you actually make an instance. Well, the animal class is now called as abstract class. It is just a concept or idea not associated with any specific instance. Other examples for abstract classes include humanbeing, employee, shape, etc.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic