• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Regarding abstract class and interfaces

 
Tusshar Fasate
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 425
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try reading this and this.
 
Shailesh Phatak
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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();
}

}



 
Pushkar Choudhary
Rancher
Posts: 425
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Aim Scjp wrote:


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
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Aim for elaborative reply
 
Shailesh Phatak
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Most Welcome
 
Parag Patankar
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9519
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
Shailesh Phatak
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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. |^
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic