• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Abstracf Vs Interfaces

 
Raj Joe
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When should I go for a abstract class or interface.
Can you please give a real world example
 
Nigel Browne
Ranch Hand
Posts: 703
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This has been discussed many times before. Use the search tool and you will find previous threads. Or read this thread
[ October 19, 2004: Message edited by: Nigel Browne ]
 
Srinivasa Raghavan
Ranch Hand
Posts: 1228
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Raj Joe:
When should I go for a abstract class or interface.
Can you please give a real world example



In an abstract class can define the methods which are common to all child classes but some functions like StartUp & Shutdown may differ from class to class such function's can be writtern inside a abstract class without a definition and the child class is forced to write that function. But in the case of interface none of the methods can have that implementation.

An abstract class can have all the flavours of class members like private public protected, but interface needs to be public in general.

When two unrelated classes need to implement a common properties we can use interface.
Ex: Human , Lorry : both these classes have some common properties like color, Weight.These Color & Weight cannot be in a base class for Class "Human" & Class "Lorry". B'coz if u'r using a base class the classes that extends from this class s'd be logically related to each other.In this case Human & lorry cannot be related logically.

Hope you understood !!!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic