wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Why Abstract classes? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Why Abstract classes?" Watch "Why Abstract classes?" New topic
Author

Why Abstract classes?

karthik manick
Ranch Hand

Joined: Aug 25, 2009
Posts: 52
i dont understand the practical usage of abstract classes... why do we need a class that doesn't have any implementation? As far as my perception, without abstract classes also we can use the concept of polymorphism...then why to create a abstract class and extending it?
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18532
    
  40

karthik manick wrote:i dont understand the practical usage of abstract classes... why do we need a class that doesn't have any implementation?


An abstract class can have an implementation. It just doesn't have to have the whole contract implemented.

karthik manick wrote:As far as my perception, without abstract classes also we can use the concept of polymorphism...then why to create a abstract class and extending it?


Not sure what you mean here. Can you elaborate?

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Edwin Keeton
Ranch Hand

Joined: Jul 10, 2002
Posts: 214

Abstract classes do not have to be purely abstract, in other words an abstract class can have implemented methods, as well as fields. These methods and fields of course will be inherited by all concrete child classes. This allows you to guarantee that specific implementations and properties (data) are available in the class hierarchy.

If you don't need this, use an interface instead.


SCJP, SCWCD
swapnl patil
Ranch Hand

Joined: Aug 13, 2007
Posts: 80
Abstract class are used to provide the common implementation & also provide abstract method so that other users are free to provide the implementation as per their need.

lets take bank example in which withdraw & deposit method contains common logic which all need to use & other method like get balance , other methods need to abstract so that other user can provide their own logic for that.

Its a contract.
karthik manick
Ranch Hand

Joined: Aug 25, 2009
Posts: 52
Thanks for your reply...I agree with you that abstract class can also be implemented ... my question is whats the significance of having an unimplemented abstract class...is is only for better code readability ? or is it having any performance improvement?
James Dixon
Ranch Hand

Joined: Jun 20, 2009
Posts: 32
Hi Karthik

I think you misunderstand what an abstract class is. Abstract classes cannot directly be implemented (i.e. you cannot call new AbstractClass()). They must be extended to be used.

The idea behind having one is that you may have a concept of, say a Cat, and you have different species of cats.
What you could do is create an interface called Cat and then implement it a number of times to create your different species. This is perfectly acceptable, however as the number of cat species increases this starts becoming more and more unwieldy as you have to implement all methods each time a new species it added so you may end up repeating a lot of code.

One solution would be to create a GenericCat class to hold all the the common code and methods, and then extend this for each of your species, overriding the methods for each species as needed. This poses two problems though. Firstly it allows for the GenericCat to be implemented directly, which we don't want any one to do, and secondly we have to implement all the methods defined in the interface, meaning that when we create our new species we may forget to override one of the methods that we need to, thus introducing a possible error.

A better solution would be to use an AbstractCat class that implements Cat and then only implements the methods that will be common to all Cats.
That way we don't have to worry about developers trying to directly implement AbstractCat(i) as there will be a compile error, or if they forget to implement a method as again this will cause a compile time error. It also means that the code will be cleaner as the abstract class will not be cluttered with empty methods or ones that return arbitrary values (which should also help reduce the risk of error).

(i) Note: I am telling a bit of a white lie at this point as developers can sort of implement AbstractCat, but as it confuses matters I thought I'd leave out the fringe cases. For more info though have a look into Anonymous classes and possibly the use of Reflection.

I hope I haven't confused matters too much :-)
Ulrika Tingle
Ranch Hand

Joined: Nov 24, 2009
Posts: 92
karthik manick wrote:As far as my perception, without abstract classes also we can use the concept of polymorphism...then why to create a abstract class and extending it?


It's for design flexibility and control.

Say you have a fully concrete class. Now you don't want people to make objects of it, you want to force people to extend it. What do you do? That's right, you declare the class abstract.

Or say you have a class which is designed to be extended and you want to make absolutely sure every subclass provides an implementation for specific methods. What do you do? That's right, you declare those methods abstract (and because you do that the class must be abstract).

So abstract classes really aren't necessary but they're helping you decide how your class is to be used.
Ryan Webb
Greenhorn

Joined: Dec 09, 2009
Posts: 28
I want to add to previous great reply about polymorphism and flexibility.
Before I have trouble in understanding the purpose of this abstract classes and interface (what the heck they're for)

I will try to explain using Java codes.

Suppose you have an abstract class like this:


then we will provide the first implementation: A Dog which is of type Animal


we can use this in a program...lets call this our MainClass (hope its ok )


Now, after some time our Dog will get old after years of using this program and will no longer catch up to the requirements (as the saying, "can't teach old dogs new tricks") We will need to be able to change the behavior of our MainClass without too much change in the code. Lets say for example, the company no longer needs a Dog...as new rules of doing business this company needs a Cat to do the Job (and the change in the code should be minimal as possible.)

All we need to do is to create Cat class!


Then the only change needed in our MainClass is this:


As you can see, minimal code is changed! Any class that extends Animal should have the method makeSound() and our MainClass will not care what type of animal it is, it just knew that it has a method makeSound().

What happened to the Dog? Well after years of service, there are jobs that only Dog Class can do like BARK when there's a burglar (which the cat can't do) So we can't just kill it. (I mean there's still other class that needs Dog class)

I hope, I have demonstrated in code the flexibility of using Abstract classes. Maybe in this example it is trivial but in real world applications, where thousands of lines of codes are used...this is really helpful.

Thanks for reading!
Ryan Webb


frustrated to learn c/c++/objective-c
karthik manick
Ranch Hand

Joined: Aug 25, 2009
Posts: 52
Thanks to all for your reply... Now i am a bit clear with abstract classes..
Soumil Shah
Ranch Hand

Joined: Jul 13, 2009
Posts: 54
Ryan Webb wrote:I want to add to previous great reply about polymorphism and flexibility.
Before I have trouble in understanding the purpose of this abstract classes and interface (what the heck they're for)

I will try to explain using Java codes.

Suppose you have an abstract class like this:


then we will provide the first implementation: A Dog which is of type Animal


we can use this in a program...lets call this our MainClass (hope its ok )


Now, after some time our Dog will get old after years of using this program and will no longer catch up to the requirements (as the saying, "can't teach old dogs new tricks") We will need to be able to change the behavior of our MainClass without too much change in the code. Lets say for example, the company no longer needs a Dog...as new rules of doing business this company needs a Cat to do the Job (and the change in the code should be minimal as possible.)

All we need to do is to create Cat class!


Then the only change needed in our MainClass is this:


As you can see, minimal code is changed! Any class that extends Animal should have the method makeSound() and our MainClass will not care what type of animal it is, it just knew that it has a method makeSound().

What happened to the Dog? Well after years of service, there are jobs that only Dog Class can do like BARK when there's a burglar (which the cat can't do) So we can't just kill it. (I mean there's still other class that needs Dog class)

I hope, I have demonstrated in code the flexibility of using Abstract classes. Maybe in this example it is trivial but in real world applications, where thousands of lines of codes are used...this is really helpful.

Thanks for reading!
Ryan Webb

we can do same thing with interface , right...?
Ryan Webb
Greenhorn

Joined: Dec 09, 2009
Posts: 28
Soumil Shah,

Yes that is correct!
The only difference is Interfaces cannot have any implementations unlike in abstract classes which
may have their own implementations.

Another difference is you may extend an abstract class only once unlike interfaces.
 
 
subject: Why Abstract classes?
 
Similar Threads
Abstract Classes
Abstract classes
Can we mark an overriding method as abstract
Abstract Method in enum
strictfp