Understanding Importance of Interface, Inheritance...... OOP concepts in a different way !

Lets talk about some fundamentals of Object Oriented Design concepts in a different way...
1.IS A - Inheritance
A super class for Animal

class Animal {
int legs;
String name;
public Animal(int legs, String name) {
  this.legs = legs;
  this.name = name;
public void walk() {}

Creating Cow class, a type of Animal

class Cow extends Animal {
public Cow(int legs, String name) {
  super(legs, name);

In the example above,
Cow is a subclass of Animal because Cow inherits from Animal. So inheritance is ISA relationship. You see the walk() methods is already defined for Animal and we don't need to defined them again and again.
2. Has A - Member Field
Lets create Brain...

class Memory {
 int size;
 public void loadIntoMemory(Object anything) {}
 public boolean isInMemory(Object suspect) {
  return true;

Adding brain to Dogs' head.
class Dog extends Animal{
  Memory dogMemory;

Dog is obvioulsy an Animal and a Dog has some Memory called dogMemory. Hence, HAS-A relation is defined by a member field.
3. Performs - Interface
Lets introduce a interface IHelp.

interface IHelp {
  void doHelp();

Creating A Dog
class Dog extends Animal implements IHelp {
  private Memory dogMemory;
  public Dog(int legs, String name) {
  super(legs, name);
public void doHelp() {
  if (dogMemory.isInMemory(new Object())) {
private void findSuspect() {}

Here Dog is an Animal, it has Memory and it can Help. We can ensure a Dog can help by implementing IHelp interface.

Something More
The benefit of IHelp interface is that we can get help from all Animals by using same interface methods.
For example , lets create a Horse Class

class Horse extends Animal implements IHelp{
public Horse(int legs, String name){
public void doHelp() {
private void carryHuman();

Getting help from Horse
Horse aHorse= new Horse(4,"Acorn");

and for getting help from Dog
Dog aDog= new Dog(4,"Puppy");

You see we can get help from these Animals from same method doHelp();.
Its the fun of Object Oriented Design.......
Enjoy !!!!!

1 comment :

  1. Good point,even though it is java,I am able to relate in C#.Good


Your Comment and Question will help to make this blog better...