954,541 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

need of abstract class

i know the concept of abstract class in java and this concept is used in interface, i.e all the methods declared in interface are abstract. i want to know how the abtract class is different from the normal class in java

Thanks a million

tinamary
Newbie Poster
16 posts since May 2007
Reputation Points: 37
Solved Threads: 0
 

I'm sorry, but if you "know the concept of abstract class", then you know how it "is different from the normal class".

Maybe we should begin with you stating what you believe to be "the concept of abstract class", and what you believe to be "the normal class". Then we can continue on.

The most basic difference, of course, is that you cannot instantiate the abstract class, directly. That does not mean that you cannot get an instatiated object declared as an abstract type, but you cannot directly instantiate the abstract class (except anonymously, in which case it is still not really a direct instantiation of the abstract class as it will be "extended" inline, i.e. the methods are defined on instantiation).

masijade
Industrious Poster
Moderator
4,253 posts since Feb 2006
Reputation Points: 1,471
Solved Threads: 494
 

his "concept of the abstract class" is flawed if he believes it's what lies at the heart of interfaces...

jwenting
duckman
Team Colleague
8,392 posts since Nov 2004
Reputation Points: 1,662
Solved Threads: 337
 

pls explain the concept of abstract classes and method in java............

mansi4u
Newbie Poster
1 post since Aug 2010
Reputation Points: 10
Solved Threads: 0
 

Normal class:
Normal class has definition of methods and variables and their declaration as well.
Abstract class:
Abstract class has methods and variables with definition and declaration as well like the normal classes. Hoewever it must have at least one method with no implementation, which hence is called abstract method.
We can not make instance of Abstract Class.
Interface:
Interface is an "extreme" abstract class where all the methods are abstract, that is, all methods with no implementation.

Reference:
Normal class vs Abstract class vs Interface class 65

tong1
Posting Whiz
358 posts since Jul 2010
Reputation Points: 34
Solved Threads: 72
 

abstraction is the act of representing essential features withi=out including the background details or explaination. so, abstraction is used for hiding the unnecessary or the unwanted data from being displayed. for example if you know about functions in java, the process of calling the sub function,etc is hidden from the user. in a normal class, all these processes are shown.

Megha SR
Newbie Poster
24 posts since Jul 2010
Reputation Points: 10
Solved Threads: 3
 

In terms of actual functionality, an interface usually represents an ability, and an abstract class typically has a 'is-a' relationship with subclasses.

For example:
You might have an interface Drivable, and classes Car and Bus that implement it, because both Cars and Buses are Drivable.

You might also have a Machine abstract class, and a class Computer that extends it, because a Computer is-a Machine.

coil
Posting Whiz in Training
273 posts since Aug 2010
Reputation Points: 27
Solved Threads: 56
 

There is actually a significant difference between the abstract class and the interface. The abstract class depends on inheritance, while there is no inheritance in implementing an interface.

An interface simply says "I can do these things". It is a promise to the compiler that a method that implements this interface will have a certain set of methods, which take certain parameters and return certain types. It says nothing about how those methods should be implemented. You might have a "sortable" interface which requires that something sort itself. When you go to implement this method, you get to and you have to implement it each time - this is good because it can be anything at all, and there's no guarantee that sorting your Car class will be anything like sorting your Fencepost class. So this doesn't require anything but "the stuff in this class will be in order when we're done".
This is a drag, if you have a lot of things that are the same, and you want them to sort themselves the same way. Suppose you have a VWBug class and a Miata class and an Edsel class. All of these will be sorted in the same way, presumably - perhaps by VIN or license plate number, or I don't know what, but they'll all sort in the same fashion. So here's where you use an abstract class. You make a class Car, which is abstract - it exists only to be extended - but it extends "Sortable" and it has a fully implemented sort() method, which is suitable for any class extending Car. Now, when you extend Car, you get the sorting for free, and you're automatically registered as implementing Sortable.

Understanding inheritance and interfaces is very important to making Java work for you. I sugest you do some reading on this, and then write little programs to test what you read, until you get the hang of it. Just reading what we all say about here will not be enough to understand it.

jon.kiparsky
Posting Virtuoso
1,849 posts since Jun 2010
Reputation Points: 383
Solved Threads: 187
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You