aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Regarding abstract class and interfaces Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Regarding abstract class and interfaces" Watch "Regarding abstract class and interfaces" New topic
Author

Regarding abstract class and interfaces

Tusshar Fasate
Ranch Hand

Joined: May 21, 2008
Posts: 81
Hi,
I would like to know that when one should use abstract class and when interfaces?
I found its very confusing.I googled for the same and i got many answers on this.
But I am facing problem to understand them .
Can anybody explain on this?
Answer is expected in lucid language.Exmaples ,real life scenarios are welcomed.
Thanks in advance....
Pushkar Choudhary
Rancher

Joined: May 21, 2006
Posts: 425

Try reading this and this.
Shailesh Phatak
Ranch Hand

Joined: Aug 27, 2009
Posts: 64
Hi,
Create abstract class when

1> When you are not sure about what the abstract method of that class will do when the class will get inherited.
2> When you want to define different feature of the abstract method to different class inheriting it.

For e.g look at this real life e.g
You want to purchase a car(abstract class) under certain parameters like Color of car and engine of car(abstract methods) and for that You have created your mindset that if i purchase HondaCar(Runtime Class)then your car should have Blue Color and cctvi engine. But if i Purcahse GmCar(RunTime Class) then it should have Red color and advanced cctvi engine but at the biginning you are not sure whether you wiil buy a HondaCar or GmCar. in this case abstract class is created because you have certain common methods but about you are not sure about the content of that method.

Hope you understand


abstract class ChColorEngine
{
abstract public void carColor();
abstract public void engine();
}

class HondaCar extends ChColorEngine
{
public void carColor()
{
System.out.println("This car should have Blue color");
}
public void engine()
{
System.out.println("This car should have cctvi engine");
System.out.println("");

}

}


class GmCar extends ChColorEngine
{
public void carColor()
{
System.out.println("This car should have Red color");
}
public void engine()
{
System.out.println("This car should have advanced cctvi engine");
}

}

class MainCar
{
public static void main(String args[])
{
HondaCar H=new HondaCar();
H.carColor();
H.engine();

GmCar G=new GmCar();
G.carColor();
G.engine();
}

}





See Dreams With Open Eyes And Make It Real
Pushkar Choudhary
Rancher

Joined: May 21, 2006
Posts: 425

Aim [surl='http://www.coderanch.com/how-to/java/ScjpFaq' class='api' title='SCJP FAQ wrote:Scjp[/surl]]


Please Use Code Tags. Also, please change your display name to use your real first and last names. You can read about the Javaranch's Naming Policy here.
Shailesh Phatak
Ranch Hand

Joined: Aug 27, 2009
Posts: 64
Now lets try to understand interface

You want to purchase a car under certain parameters like Color of car and engine of car and for that You have created your mindset that if i purchase HondaCar(Runtime Class)then my car should have Blue Color and cctvi engine. But if i Purcahse GmCar(RunTime Class) then it should have Red color and advanced cctvi engine + some AdvanceFeature but at the biginning you are not sure whether you wiil buy a HondaCar or GmCar. in this case interface is created because you have certain common methods but about you are not sure about the content of that method.

Tusshar Fasate
Ranch Hand

Joined: May 21, 2008
Posts: 81
Thanks Aim for elaborative reply
Shailesh Phatak
Ranch Hand

Joined: Aug 27, 2009
Posts: 64
Most Welcome
Parag Patankar
Greenhorn

Joined: Feb 26, 2007
Posts: 13
Hi Shailesh,

Thanks for your reply on abstract vs interface with very good example. But still not very clear, in your example of a car, what requirements make the difference that makes you abstract class or inerface ? For me it was looking same,

1, Certain about colour, engine, additional facilities
2. Not sure which car going to purchase

Can you elaborate on example please ?

thanks
parag
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9303
    
  17

Choosing between abstract class and interface involves a lot of factors. An abstract class can provide some default behavior and leave the rest for sub-classes to implement, but interfaces cannot have any default behavior. But a class can extend only one other class while it can implement many interfaces. Read a tutorial like this to get more insight on this...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Shailesh Phatak
Ranch Hand

Joined: Aug 27, 2009
Posts: 64
Hi Parag,
In abstract class you can also put concrete methods but in interface you ought to put only abstract methods. Hope you understand that eg is self explanatory with this only difference. |^
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Regarding abstract class and interfaces