• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

Regarding abstract class and interfaces

 
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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....
 
Rancher
Posts: 425
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try reading this and this.
 
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Aim for elaborative reply
 
Shailesh Phatak
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Most Welcome
 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 9709
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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. |^
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic