What is the use of abstract classes? How will we decide in a scenario, whether we have to use abstract class or interface?

Showing Answers 1 - 47 of 47 Answers

slp2683

  • Dec 2nd, 2005
 

Abstact classses are those classes which can have abstact as well as Method with body . we use Abstract classes becouse we wont ot implement the Polymorphism.yes we should prefer the abstarct class becouse it will give us the addtional method declaration in the class.

  Was this answer useful?  Yes

imiten

  • Dec 3rd, 2005
 

Use case will lead you to entities. These might be leading to is-A relationship. Such relationship is usually rooted with Abstract classes which provides polymorphism and also lead to application of many design patterns such as Abstract Factory, template, strategy and many others.

  Was this answer useful?  Yes

Prasanna Balaraman

  • Dec 16th, 2005
 

We use Abstract class and interface to enforce some rules to the classes which extends/implements. For example we can define a class say "Bird" and we can declare methods say "Fly()", "Walk()". This means whatever the class that is derived from Bird has to override or give definition for Fly() and Walk() and therefore we are making sure that all the derived classes follows or has the common functionalities. In other way the classes derived from superclass should have common properties. In addition to this the derive class can have its own methods like "Swim()"...

In case of Abstract class we can define COMMON functionalities in super class and those can be used in the derived class where as in Interface we cant do that. ( this i would say as advantage of abstract class)

In case of Interface the derived class can implement any number of interface but restricted to extend only one abstract class (this i would say as advantage of Interface)

Thanks

Prasanna

chandra reddy

  • Jan 21st, 2006
 

Thats exactly very good example.

Thank you very much prasanna

thanks

Chandra_mca2000@yahoo.com

  Was this answer useful?  Yes

japa2001in

  • Mar 13th, 2006
 

The abstract class is a base class for a variety of classes that offers common members n methods. If we want to hold common properties for different classes then we use abstract class. If we need common functionalities among different classes then we use interfaces.

  Was this answer useful?  Yes

Gopi

  • Mar 15th, 2006
 

Both the Abstract Class and the Interface Class canot be Instansitiated

  Was this answer useful?  Yes

Hi,

At the same time abstract class cannot be final. I think it will be compilation error. Please correct me if I am wrong.

Never tried final interface and compile it. Did somebody? I think again compilation error.

With Best Regards

-ashruf

  Was this answer useful?  Yes

keshav

  • Apr 10th, 2006
 

hi in the real time environment , when do we go for abstract class and when do we go for interfaces , can any one please revert back in this regard?

chennakeshava_r@gmail.com

  Was this answer useful?  Yes

Giridhar

  • Apr 11th, 2006
 

that's good example

Abstact class mainly used for default behaviour purpose.

Ravi Kumar

  • Apr 15th, 2006
 

What is the Exact difference bt Load Testing and Stress Testing and Performance Testing.

  Was this answer useful?  Yes

sumanrao

  • Apr 17th, 2006
 

You given good example for abstract class

thank u

          with regards

            suman .ch

              

  Was this answer useful?  Yes

ajay

  • Apr 27th, 2006
 

Hi anil,

  Can u describe what is the mean of properties amd functionality

  Was this answer useful?  Yes

jayaramreddy

  • May 26th, 2006
 

can u tell me in real time where we can use interface and where we can use abstraction.

thanks

jayaram

siva kumar reddy

  • Aug 9th, 2006
 

hi,

abstract class is a class that contains 0 or more abstract methods.interface is specification of method prototypes(all abstract methods only).

whenever the developer wants to use some features was shared by all objects that time they go for abstarct class.

the developer wants different implemaentation for every then they go for interface.

we cannot create the object for abstract and interface.we can create the reference of absract class and interface.

cheers

siva

  Was this answer useful?  Yes

Sai

  • May 14th, 2007
 

Actually LoadTesting and Performance Testing are nothing but the same. Hence we call as P&L testing sometimes..
StressTesting comes under this as one of the branches.Where the thinktime between the transactions is reduced inorder to make the system to load on Stress and observe the perfomance.
Similarly we have Endurance test,Scalability Test,Bandwidth test , Volume test ect under this activity P&L Testing.

  Was this answer useful?  Yes

Since we can define abstract method and non-abstract method in Absract class,  abstract class has more designing flexibility comparatively than interface.

Abstarct class can be used for the common functionalities, which is common for different(or all) users. For example,  to get social security number, getSSN() can be defined as non abstract method in an abstarct class.

If the functionalities is user specific means then the method can be defined in the interface and it can be implemented from the sub class(es). For example,

public interface ISalary
{
     public double calculateSalary(double amt);
}

public class PermanentEmp implements ISalary
{

public double calculateSalary(double amt){
//salary calculation varies for permanent employee
}


}

public class ContractEmp implements ISalary
{

public double calculateSalary(double amt){
//salary calculation varies for contract employee
}


}

  Was this answer useful?  Yes

akash

  • Sep 12th, 2007
 

We can't instantiate an abstract class.

  Was this answer useful?  Yes

Ashwani kumar

  • Oct 15th, 2007
 

Abstract is a process of defining the essential concepts while ignoring the non essential concept.
It is the process of picking out common feature of objects and procedure ..

Abstract class contain 0-99% of implementation.
while interface contain 0% of implementaion.
We can not instantite both.;
we can inherit only one abstract class .
while we can implement as many no of interface.

Also
interface can  not come in inherting chain

  Was this answer useful?  Yes

An abstract class can be used when your base class is having some default behaviour and all the classes that extend the base class can use that functionality and on the above they can implement their own business. The disadvantage is the given class can sub class only one class and the scope is narrowed.

On the other side, classes can implement multiple interfaces.  That means you are giving more implementation. But the interfaces can not have the default behaviour attached. Who ever implements interface, has to implement the whole thing. We can use interface when you have your design changes.

For complex functionality, it's common to use both - first, an interface that defines a component's functionality, and then a base class that implements this interface in a default manner.

  Was this answer useful?  Yes

dineshsemal

  • Jun 18th, 2009
 

When you defined a class as an abstract class, it has to be inherited if you want these function to be in use because implementation part is not there in the base class hence you have a control over your team that designs functions with the irrelevant prototypes and many other function which are marked as non-abstract functions in the base class can not be used usinig the base class hence those are now secure and can not be directly without inheriting.

  Was this answer useful?  Yes

Narsi Reddy

  • Apr 29th, 2011
 

Abstract classes are used when a third person want to call overloaded method of the sub class which is extending the abstract class.

Yes for the above use even interfaces can be used but prefer abstract classes only when you want to give its subclass some implementation.

  Was this answer useful?  Yes

abstract classes are those classes from which we cannot create object.
When we use abstract classes?: If programmer want no one will be able to change class structure or miss use class then he simply make that class as abstract.

  Was this answer useful?  Yes

Hareesh Reddy

  • Aug 3rd, 2011
 

Good Prasanna.

We got better answer from you.But I want Example code for that.

Please...........

  Was this answer useful?  Yes

shri

  • Sep 6th, 2011
 

for polymorphism we need implements not extend co abstract class is use

  Was this answer useful?  Yes

raghu

  • Nov 3rd, 2011
 

Can any one give a real time example program for abstract class and virtual functions plzzzzzzzz

  Was this answer useful?  Yes

ram

  • Feb 17th, 2012
 

First of all abstract is a key word. Abstract classes are having both defined methods and undefined methods. Concrete classes having only defined methods in the case of concrete classes the execution of the Java application takes more time compare to abstract classes. In the case of abstract classes performance is increased and execution time less.

In interfaces all methods by default public abstract, these are having only undefined methods, the execution time is less and moreover performance will increase compare to abstract classes.

So interfaces are better to use reusability of our java applications.

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions