aspose file tools*
The moose likes Beginning Java and the fly likes why we need abstract class? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "why we need abstract class?" Watch "why we need abstract class?" New topic
Author

why we need abstract class?

Lucy Rai
Greenhorn

Joined: Dec 19, 2012
Posts: 8
Please tell me answer. I am java fresher.

meeta gaur
Ranch Hand

Joined: Dec 05, 2012
Posts: 305

To provide special classes.


My one regret in life is that I am not someone else.
- Woody Allen
harshvardhan ojha
Ranch Hand

Joined: Jul 26, 2007
Posts: 157
    
    1

Hi Lucy,

Lets say i want to create a contract for other class, but don't want itself to be created.(i will make it interface).
When i know concrete implementation for a class i would like it to be a normal class.

But at times, there will be situation when i know some of the implementation and want to impose some contract over it, so that it could only be instantiated after fulfilling those requirements(overriding). Then i need abstract class.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38865
    
  23
You do not “need” abstract classes; you could work quite happily with interfaces and concrete classes. Indeed, some languages eschew interfaces, too.
As harshvardhan ojha said, they are intermediate between interfaces and concrete classes, intended for situations where some but not all of the implementation is already known. But remember you can have abstract classes which are fully implemented (no abstract methods).
vinay chaturvedi
Greenhorn

Joined: Jan 16, 2012
Posts: 14
Yes in some cases the abstract class may or may not contain any abstract methods. For Ex. In the case of "HttpServlet" class. This class contains all implemented methods but still it is an abstract class.
There are the cases in which you don't want to implement an Interface because you have to define all the methods of the implemented interface. In such cases these abstract classes becomes useful as they already contain method implementations. So when you extend the abstract class you just have to override the method you need.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38865
    
  23
vinay chaturvedi wrote:Yes in some cases the abstract class. . .
Good answer
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7801
    
  21

Lucy Rai wrote:Please tell me answer. I am java fresher.

Another thing that abstract classes are useful for is as "skeleton implementations" for an interface; a good example of which is AbstractList.

For example, the following class (filched from Effective Java) wraps an Array as a read-only List:which is pretty powerful stuff, when you consider all the things you can do with a List (for example: iterating items in reverse order).

HIH

Winston

Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Andy Jack
Ranch Hand

Joined: Nov 22, 2012
Posts: 257
meeta gaur wrote:To provide special classes.

???


Java Newbie with 72% in OCJP/SCJP - Super Confused Jobless Programmer.
I am a "newbie" too. Please verify my answers before you accept them.
Andy Jack
Ranch Hand

Joined: Nov 22, 2012
Posts: 257
Lucy Rai wrote:Please tell me answer. I am java fresher.



Say we are making a computer game that depicts the earth and all the living and non-living things in it.

When two (or more) things are closely related you can combine all their common functions and properties inside an Abstract class and then derive from it. Example. Bird and Mammal are living beings, they can move(), eat(), breathe(), reproduce(), exist as dimorphs(male/female). We know all this from our knowledge of biology. So, we create an Abstract class Animal with abstract methods like move(), eat() etc and variables like sex = male or female (or transgender???). Then Bird and Mammal define methods in their unique way - bird eats with beak, mammal with mouth etc.
Tomorrow, if we discover a new animal in some jungle, its quite likely that it will be similar to bird and mammal, at least in some ways. To define that new animal, we will simply extend from Abstract animal class and add the add the extra features or modify animal methods accordingly.

Interfaces are used when two things can perform similar functions, but are not very closely related. Example bird and bat can fly. Even airplanes can fly. So, is a bat a bird or vice versa ? No, bats don't lay eggs and they breastfeed. So we make an interface canFly or flyable or something like that. Bat and
bird implement that interface. When a third person reads the documentation for bat and bird, he sees "implements canFly" and extends Animal. So, he knows immediately that they are animals, but not the same kind. They can fly (and have two feet etc) but a bat is not a bird.

(Hey, potato has skin. I have skin. So am i a potato ?...Monkey has hair, monkey has skin. It can walk, eat and reproduce like me. It looks like and shares a huge percentage of its DNA with me. So, am i a monkey..no i am like a monkey... )

I feel that these may not be perfect examples. But, i thought they could guide you and maybe you can think of a better example.

Andy Jack
Ranch Hand

Joined: Nov 22, 2012
Posts: 257
Assume that scientists have not discovered Ostrich, Penguin, Chicken which cannot fly Chicken can fly for about 20 seconds When they discover that, they will have to change bird class. Flying bird, non flying bird. Flying implements canFLy and other one does not.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: why we need abstract class?