GeekInterview.com
Series: Subject: Topic:
Question: 32 of 38

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

Asked by: Interview Candidate | Asked on: Nov 29th, 2005
Showing Answers 1 - 27 of 27 Answers
slp2683

Answered On : Dec 2nd, 2005

View all answers by slp2683

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.

  
Login to rate this answer.
imiten

Answered On : Dec 3rd, 2005

View all answers by imiten

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.

  
Login to rate this answer.
Prasanna Balaraman

Answered On : 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

Yes  4 Users have rated as useful.
  
Login to rate this answer.
chandra reddy

Answered On : Jan 21st, 2006

Thats exactly very good example.

Thank you very much prasanna

thanks

Chandra_mca2000@yahoo.com

  
Login to rate this answer.
japa2001in

Answered On : Mar 13th, 2006

View all answers by japa2001in

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.

  
Login to rate this answer.
Gopi

Answered On : Mar 15th, 2006

Both the Abstract Class and the Interface Class canot be Instansitiated

  
Login to rate this answer.

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

  
Login to rate this answer.
keshav

Answered On : 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

  
Login to rate this answer.
Giridhar

Answered On : Apr 11th, 2006

that's good example

Abstact class mainly used for default behaviour purpose.

Yes  1 User has rated as useful.
  
Login to rate this answer.
Ravi Kumar

Answered On : Apr 15th, 2006

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

  
Login to rate this answer.
sumanrao

Answered On : Apr 17th, 2006

You given good example for abstract class

thank u

          with regards

            suman .ch

              

  
Login to rate this answer.
ajay

Answered On : Apr 27th, 2006

Hi anil,

  Can u describe what is the mean of properties amd functionality

  
Login to rate this answer.
jayaramreddy

Answered On : May 26th, 2006

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

thanks

jayaram

Yes  1 User has rated as useful.
  
Login to rate this answer.
siva kumar reddy

Answered On : 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

  
Login to rate this answer.
Sai

Answered On : 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.

  
Login to rate this answer.

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
}


}

  
Login to rate this answer.
akash

Answered On : Sep 12th, 2007

We can't instantiate an abstract class.

  
Login to rate this answer.
Ashwani kumar

Answered On : 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

  
Login to rate this answer.
sampra

Answered On : Feb 13th, 2008

View all answers by sampra

Ansrwe is given above  

  
Login to rate this answer.
ravikirankethe

Answered On : Feb 19th, 2008

View all answers by ravikirankethe

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.

  
Login to rate this answer.
dineshsemal

Answered On : Jun 18th, 2009

View all answers by dineshsemal

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.

  
Login to rate this answer.
Narsi Reddy

Answered On : Apr 29th, 2011

View all answers by Narsi Reddy

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.

  
Login to rate this answer.

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.

  
Login to rate this answer.
Hareesh Reddy

Answered On : Aug 3rd, 2011

Good Prasanna.

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

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

  
Login to rate this answer.
shri

Answered On : Sep 6th, 2011

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

  
Login to rate this answer.
raghu

Answered On : Nov 3rd, 2011

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

  
Login to rate this answer.
ram

Answered On : 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.

  
Login to rate this answer.

Give your answer:

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

Related Open Questions

Ads

Connect

twitter fb Linkedin GPlus RSS

Ads

Interview Question

 Ask Interview Question?

 

Latest Questions

Ads

Interview & Career Tips

Get invaluable Interview and Career Tips delivered directly to your inbox. Get your news alert set up today, Once you confirm your Email subscription, you will be able to download Job Inteview Questions Ebook . Please contact me if you there is any issue with the download.