• Post Reply Bookmark Topic Watch Topic
  • New Topic

Why we need abstract classes and interfaces in Java? What is the difference between them?  RSS feed

 
kiran madhan
Ranch Hand
Posts: 34
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Abstract classes will have partial implementation, whereas, interfaces have no implementation.
How they help programmers and when to use them?
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please do not use the bold font unnecessarily.
Search the forums This question has been discussed numerous times before
 
kiran madhan
Ranch Hand
Posts: 34
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i am very sorry...!
I will try to ask more better (in better format).
 
Campbell Ritchie
Marshal
Posts: 56527
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try searching our Wiki Forum; you will find this. Does that help? Remember that Java8 interfaces are a bit different.
 
Emil Jennings
Ranch Hand
Posts: 75
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My overly simplistic way of thinking is this:

An interface says the if you implement it, you must do certain things but how you do them is up to you.  For example, if I create a calculator interface I will specify that in order for you to use my interface you must implement methods add, subtract, multiply, and divide.  You may implement the add method to actually subtract (which will create a lot of discontent) but it's your implementation.

An abstract class defines an idea.  An Animal can be abstract because an "animal" can be considered as not having a physical existence.  An animal does have attributes associated with the idea - an animal has a specific number of legs, an animal has an age, an animal has weight, an animal has a specific diet.  So you create an Animal" with those attributes.  Then you create animals by extending Animal, such as a Giraffe.  Then the Giraffe has all of the attributes of an Animal plus those specific to a Giraffe, such as hasSpots().  Similarly, a Building is an abstract thing while houses and malls are concrete buildings.  The abstract class holds what is common among buildings while houses and malls have the attributes of Building plus the attributes that make them houses and malls.
 
Emil Jennings
Ranch Hand
Posts: 75
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Part 2 - Why you need them?  Technically you don't "need" abstract classes.  But for Buildings, which is more ideal - you have to code every House, Mall, Jail, Restaurant with numberOfFloors, numberOfDoors, numberOfWindows or you simply have every House, Mall, Jail, Restaurant extend Building which will ensure they all have numberOfFloors, numberOfDoors, numberOfWindows simply by inheritance.

Interfaces aren't "needed" either but they do provide something of a guarantee.  With the calculator example, if you implement my interface which says you must implement an add method that returns an integer result, I am at least guaranteed that the result given is an integer.  Again, your implementation of add may actually subtract, but at least the returned answer will be of the data type I am expecting, which reduces errors.
 
Campbell Ritchie
Marshal
Posts: 56527
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Emil Jennings wrote:. . . .  You may implement the add method to actually subtract . . .
That is because you think an interface looks like this:-But it doesn't. This is what the interface actually looks like:-Notice that the documentation comments constitute the biggest and bestest part of the interface; most pre‑Java8 interfaces contain something like 90% documentation comment. They constitute a guarantee. If you claim to implement that interface, you must ensure that all your methods obey the general contract for that interface, as explained in those documentation comments. If you implement addition to return the difference between the two numbers, you are not implementing the interface. You might be implementing an interface which exists in your head only, or you might be implementing what you think the interface shou‍ld have said, but you are not implementing the interface before you nor fulfilling all the guarantees in its source code..
An abstract class defines an idea.  An Animal can be abstract because an "animal" can be considered as not having a physical existence. . . . .
That sounds incorrect to me. How many animals have you ever seen that you cannot see? You have said that there are animals which have no existence. I have seen lots of animals, and even if I confine myself to those which are not microscopic in size, they all have a physical existence. I have seen worms, ants, butterflies, snakes, lions, cattle, etc, and they all seemed to have physical existence and I was able to touch them (except the lion ‍). Similarly buildings have physical existence; you can touch them, unless they are hidden behind guards and fences.

Abstract classes represent things which are so general that you think you need to specialise them by inheritance before instantiating them. You are confusing attributes and behaviours. A hasSpots() method is not an attribute but a behaviour. I do not like adding methods to subclasses; it can make the subclasses more difficult to use. The methods constitute the behaviour of the object and its fields its attributes. The following is perfectly acceptable:-As I wrote it, that method means that no Lion or subtype of Lion ever has spots.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!