Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Regarding Abstract Class & Interfaces  RSS feed

 
Hareendranath Babu Kotha
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Will You please let me know in which Scenarios Abstract Classes are beneficial than Interfaces and Vice-ver-sa ?

Thanks in Advance,
Hari.K.

"A true friend Will Never Hurt"
 
john cahil
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you want to implement some basic functionality that has to be used by all implementers
 
mohit bahl
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Interfaces are used when you want to set a template of function for which implementation has to be provided to access/use a particular functionality.

Abstart classes give default implementations for some functions and rest as a template for which implementations has to be provided

i hope this helps

cheers!
 
Shaan Shar
Ranch Hand
Posts: 1249
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by hareendranath babu Kotha:
Hi,

Will You please let me know in which Scenarios Abstract Classes are beneficial than Interfaces and Vice-ver-sa ?

Thanks in Advance,
Hari.K.

"A true friend Will Never Hurt"



Check out the followin resources
1. InterfaceVsAbstract Class 1

2. InterfaceVsAbstract Class 2

3.
InterfaceVsAbstract Class 3

Hope this will be usefull..


[ August 01, 2006: Message edited by: Ankur Sharma ]

[ August 01, 2006: Message edited by: Ankur Sharma ]
[ August 01, 2006: Message edited by: Ankur Sharma ]
 
miguel lisboa
Ranch Hand
Posts: 1281
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
imagine you have to deal with bosses, parents, children: people.

so you create an abstract class Person, with common methods like firstName, lastName, dateOfBirth and so on

classes like Boss, Employee, etc, all extend from Person

now you want some of your people to have a particular behaviour, like driving a car and ridding a bike - you create an interface Skills that has two methods: drive() and ride(); those people you want to have those skills must implement interface Skills

hope it helps
 
Hareendranath Babu Kotha
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ,
thanks for reply, But queston is not theoritical difference, when we can use Abstract classes in stead of Interfaces and Vice-ver-sa.

There might be some particular scenarios only one of these can be used na. i want those Scenarios.

Could you please help me.

thanks in advance,
Hari.K

"A true friend will never Hurt"
 
miguel lisboa
Ranch Hand
Posts: 1281
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There might be some particular scenarios only one of these can be used na. i want those Scenarios.

read my post?
 
Hareendranath Babu Kotha
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ,

With the example you posted , Can you able to justify why cant you use Abstract Classes or Interfaces in your example.

Even me too know that it is perrfect example. But how can you justify that we can't Either interface or abstract calsses for your example.

Thanks in advance,
Hari.K
"A true friend will never hurt"
 
miguel lisboa
Ranch Hand
Posts: 1281
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
one advantage of using inheritance is that subclasses (because they inherit) dont need to have again and again those common methods written

as to interfaces, you can have:
class Boss extends Person implements Skills {
drive() {
system.out.println("ferrari!");
}
// ...
}
class Employee extends Person implements Skills {
drive() {
system.out.println("just a ford...");
}
// ...
}
and then code
//...
Skills Louis = new Boss();
Skills Bill = new Employee();
Louis.drive(); // prints "ferrari!"
Bill.drive(); // prints "just a ford..."
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You cannot use abstract classes when you want a class to extend more than one at the same time.

You cannot use interfaces when you want to provide some implementation.

See also http://faq.javaranch.com/view?InterfaceVsAbstractClass
 
Hareendranath Babu Kotha
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi miguel lisboa and Ilja Preuss,

Many thanks for clearing my doubt. Feeling great to have such a nice explanations

Thanks,
Hari.K
"A true Friend Will Never Hurt"
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!