-2

I am new to java and am having trouble understanding these basic concepts. I have googled this but can't find a great explanation on this so hopefully one of you can help me. Thanks. My questions are...

What is an abstract class?

When would you use an abstract class and when would you use an interface?

  • http://www.javaworld.com/article/2077421/learn-java/abstract-classes-vs-interfaces.html – Jeroen Peeters Nov 01 '15 at 22:35
  • 1
    This question will probably get marked as duplicate in a minute. Most of the duplicates are very outdated. When you read the linked answers, make sure you check if they apply to Java 8, as the rules have been fundamentally changed. – Paul Boddington Nov 01 '15 at 22:38
  • Also relevant: [Use of Java Interfaces / Abstract classes](http://stackoverflow.com/questions/2869222/use-of-java-interfaces-abstract-classes?rq=1) – resueman Nov 01 '15 at 22:38
  • 1
    is in't duplicate question? – RamPrakash Nov 01 '15 at 23:04

2 Answers2

0

Abstract method is used for inheritance, when Object B IS Object A. Interface is used when Object B HAS Object A. For example, BMW is a CAR, but CAR is not ENGINE, CAR has ENGINE. But other than that, there are only few technical nuances between them. The logic here is most important.

EDIT: Due to popular demand, here is my example:

interface Engine {
horsePower(int a);
torque(int a);
volume(int a);
//...
}

Car

abstract class Car implements Engine {
//implement interface methods
public abstract accelerate(int howMuch, int horsePower);
public abstract brake(int howMuch);
public abstract turn(int degreesOfRotation);
//..
}

And the BMW

public class BMW extends Car {    
private final String carMake = "BMW";
private String carModel;
// implement abstract methods of Car
public void setModel(String s){
carModel = s;
//and so on
}
The Law
  • 344
  • 3
  • 20
  • PLEASE provide us an example of `Car` and `Engine`, I would love to see that. And don't show me/us a field of type `Engine` stored in a `Car` instance, because this something different and has nothing to do with a difference between abstract classes and interfaces. – Tom Nov 01 '15 at 22:45
  • @Tom Edited and done – The Law Nov 01 '15 at 22:55
  • Not bad, this example follows your explanation. But how does marker interface follow it? Or how about the "default" case of `MyInterface` and `MyInterfaceImpl` ... how do they match? And don't you think that following your explanation "Class 'has an' Interface" the code would result in a bad structure? For example your `Car`. By directly implementing the interface you have no change to implement different engine types (gas, diesel, electric) without implementing different `Car` classes, which then would result in more and more unlovely inheritance. – Tom Nov 01 '15 at 23:03
  • @Tom Solution to that would be to introduce Factory/Builder/Decorator(not sure which would be best option, without doing the actual testing) pattern inside the abstract Car and each Car maker could use it to pre-build the car engine to its desired form, before branding it and implementing the abstract methods of the Car. But then the Car would not be 100% abstract, true but that is going too deep for the sake of this question and it was not even point of this question. – The Law Nov 01 '15 at 23:12
  • How would a Factory help you here? It can create instance and initialize fields, but you still need to write an abstract class for a "blue car with four wheel, which uses gas" or for a "red car with three wheels, which uses diesel". Well or you think about the "has" and if it really correct or if an interface also describes an "is" relation. – Tom Nov 01 '15 at 23:16
  • @Tom And why not have interface for the car chassis and wheels too, and let the BMW decide how many wheels or what color the chassis should be? My main point I guess is that the final class that is exposed (to some degree) to the user should be as flexible and customizable, without introducing too large chain of inheritance. And interfaces are generally safer than overzealous inheritance. And also, lets assume every car has same engine and same number of wheels. We are going from abstraction into too much concretisation. Maybe I should have used human body and organs in my example. – The Law Nov 01 '15 at 23:22
  • So you want to tell the BMW company that they can now build only one type of a car with a certain amount of wheels and a specific color? I wonder what they would tell you :P. A human body might be better for your "has" explanation, but it doesn't help you that much, because it is still inaccurate ;). [Is there any relation between the class that implements interface and that interface?](http://stackoverflow.com/q/1050146) .. you should use "has" for fields or inner classes (for example). – Tom Nov 01 '15 at 23:28
0

There are mainly two types of classes 1.concrete classes 2.abstract classes Concrete classes are the classes in which all the methods are declared and defined i.e in simple words each method knows what it has to do and how it how's to do it When it comes to abstract class in simple words these are incomplete classes i.e they have methods which know what to do but they don't hnow how to get it done.such method is called abstract method and a class with one or more than one abstract method is called abstract class It is important to note that one cannot instantiate an object of abstract class and a class which inherits Ab abstract class must define all the abstract methods so that by itself becomes a concrete class and it can be instantiated On other hand interface is a collection of abstract methods and static final variables one of the main use of interface is to overcome the problem of multiple inheritance(a class in java can extend only one class but can implement any number of interfaces)