Home > Java, OOP > Difference between interface and abstract in OOP

Difference between interface and abstract in OOP

Now what is interface really?Interface is kind of like template.In that you tell people what standard they should follow but not telling them how to.For example.. assume you create box when people order it to you.In that case every  time they order a box with telling you ”i want some box like that you made for my neighbour”  then you define properties and  behaviour of box yourself.After some days your  workload has been so overflowed that you have to open your own company and need to recruit some worker in that company.And later you’ve to open some branches for it.Now in that case again when order comes to you ,you are not going to go to the every branch to do the job.You need to give some kind of standard to the branch manager’s or ceo whatever you call about how they should  make a box.Give them some template of box where you define what a box should behave when they create it.Tell them something like this:

public interface boxTemplate{

double setWidth(double w);

void getWidth();

double setHeight(double h);

void getHeight();

double setLength(double l);

void getLength();

double getArea();

}

And then one of the branch,design a box class something like this:

public class box1 implements boxTemplate{

public double width;

public double height;

public double length;

public box1(double wd,double hg,double ln){

this.setWidth(wd);

this.setHeight(hg);

this.setLength(ln);

}

public void setWidth(double w){

this.width=w;

}

public    double getWidth(){

return width;

}

public    void setHeight(double h){

this.height=h;

}

public    double getHeight(){

return height;

}

public    void setLength(double l){

this.length=l;

}

public    double getLength(){

return length;

}

public    double getArea(){

return (width*height*length);

}

}

No matter how all branches want to make a box, should always follow your template .Box class design of different branches can differ from one another but since they all are implementing your boxTemplate you know that what behavior a box can behave when it’s ready no matter what it’s origin is.

Abstract:

One day somehow you get a huge order of making box but with some condition.By imagining that you can be next millioniare you accepted those condition.Now the condition was that they want box with same width and length but with red,orange,yellow,green,black,white  in  color and different type of height.Since it’s a big order  you call your three branch manager.And give all of them a new kind of template like this:

abstract class boxNewTemplate {

public double width=22.0;

public double length=22.0;

boxNewTemplate()

{

this.setWidth(this.width);

this.setLength(this.length);

}

public void setWidth(double w){

this.width=w;

}

public    double getWidth(){

return width;

}

public    void setLength(double l){

this.length=l;

}

public    double getLength(){

return length;

}

abstract void setHeight(double h);

abstract double getHeight();

abstract void setColor(int colorCode);

abstract double getArea();

}

And then tell one of them to make boxes where half of them should be red and half orange and you also told the same to others for making other colors.After that one of them create a box class like this:

public class box2 extends boxNewTemplate{

static final int RED=1;

static final int ORANGE=2;

public double height;

public box2(double hg,int colorCode) {

super();

this.setHeight(hg);

this.setColor(colorCode);

}

public  void setHeight(double h){

this.height=h;

}

public    double getHeight(){

return height;

}

public    double getArea(){

return (width*height*length);

}

public void setColor(int colorCode){

if(colorCode==RED){

//give the color

}else if(colorCode==ORANGE){

//give the color

}else{

//do nothing

}

}

}

So the meaning of abstract class is that you do some implementation of your own to a class but some of them you leave for others.So if someone wants to include your class in their system they just have to implement those things that you did not in that class and you do that by telling them it’s abstract.Now if a method is not implemented yet which is abstract but most of the method of that class are how they are going to know that some of this class’s mehod is not implemented yet if you do not put abstract  before that class name.Think about it.

I think that explains a lot about interface and abstract.

Again what you are reading in here is the concept that i figure out myself.It can contain any or many conceptual error.If you find some,feel free to tell me about it.And also if you have any question feel free to ask.

HAVE A GOOD PROGRAMMING

Advertisements
Categories: Java, OOP Tags: ,
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: