1

I'm new to Java programming and right now, I am trying to understand OOP concepts (inheritance, polymorphisms, etc.).

I know that, when a subclass extends a superclass (abstract or not), subclass constructor calls the constructor of that superclass first (super()).

My questions are:

1) Is it the same case for Interfaces? I've read some articles saying that interfaces don't have constructors, so how exactly are they being extended?

2) How come multiple inheritance is not supported in Java but an interface can "extend" multiple other interfaces?

Thanks in advance.

odexue
  • 11
  • 1
  • 2
  • 1
    you may read http://stackoverflow.com/questions/13773714/extending-an-object-vs-implementing-an-interface?rq=1 – sina72 Jul 04 '14 at 07:04
  • 4
    I always consider an interface to be a contract as it basically dictates what an implementing class must adhere to. So when one contract extends another contract, it obtains all the rules, regulations and boundaries contained within the source contract and adds its own. – Gimby Jul 04 '14 at 07:08
  • An interface is a contract without implementations (Java8 introduced default methods). By extending you extend the contract with new "names" to be implemented by concrete class. – PeterMmm Jul 04 '14 at 07:09
  • May be this question http://stackoverflow.com/q/8531292/1055241 also help you understanding concept of interfaces – gprathour Jul 04 '14 at 07:11

4 Answers4

1

Yes, you can do it. An interface can extends multiple interfaces.

interface Maininterface extends inter1, inter2, inter3{  
  // methods
}

Not only interfaces,A single class can also implements multiple interface. Then obviously a doubt raises, what if two methods have an same method name.

There is a tricky point:

interface A
{
    void test();
}

interface B 
{
    void test();
}

class C implements A, B
{

    @Override
    public void test() {

    }     

}

Then single implementation works for both :)

Shyam
  • 6,376
  • 1
  • 24
  • 38
1

1) Is it the same case for Interfaces? I've read some articles saying that interfaces don't have constructors, so how exactly are they being extended?

Yes, there is no constructor in an interface, you will have to define a concrete class(that implements that interface) to create an object of that interface type.

Example: you can check the profile of java.io.Serializable using javap java.io.Serializable which is:

public interface java.io.Serializable {
}

which says there is no constructor.

2) How come multiple inheritance is not supported in Java but an interface can "extend" multiple other interfaces?

Yes you can extend multiple interfaces, this is because, if two interface contains abstract method with same signature, this will not be an ambiguity to the compiler. But this is not with the case with class, if you will try to extend two classes that have method with same signature then it will be an ambiguity to the compiler for which method to call as method declaration might be different in different class.

Vishrant
  • 15,456
  • 11
  • 71
  • 120
1

First of all try to clear the concept of inheritance . The basic concept behind Inheritance is to inheriting (getting ) all the property(variables and methods)(considering access modifier ) of parent class with our rewriting them in the child class.

Suppose we have a Class A,

class A{
  public void doA()
  {
  }
}

now if class B extends A(mark the literal meaning of extends) it will get the method doA(), inherently. If you look at the literal part actually B extends A it seems B is an extended version of A.

Now Come to the Interface part. If I have a interface InA

interface InA{
  doInA();
 } 

and i try to inherit it in class B i need to implement (mark the literal again) it. So i will get the method doInA() it B but i need to give it a body.

In case of of Interface to Interface, Now as you have used two key word for inheritance. If i ask you that an interface InB will inherit interface InA what key work will you chose among extends and implements? doesn't the extends sounds more logical. Because InB implementing nothing its just getting bigger and it will be an extended version of InA.

Now Lets answer you two key question:

1.Is it the same case for Interfaces?

Yes and No. actually the constructor of parent only be called when the constructor of child is called. So as you never can call the constructor of child Interface the constructor of parent interface will never be called. Its doesn't call the parent constructor but still it reserve the technique of constructor calling .

2) How come multiple inheritance is not supported in Java but an interface can "extend" multiple other interfaces?

Have a look at this Why is there no multiple inheritance in Java, but implementing multiple interfaces is allowed?

Community
  • 1
  • 1
Saif
  • 6,804
  • 8
  • 40
  • 61
0

1) Is it the same case for Interfaces?

Not really. Constructors for an interface don't really make sense, as constructors define some initial state, but interfaces don't have state. So constructors aren't being called.

I've read some articles saying that interfaces don't have constructors, so how exactly are they being extended?

You should think of extending an interface as more extending a type -- that is, expanding the defined behaviors of something. It's like taking a contract and adding some clauses onto the end -- you're saying "In addition to the methods defined in the superinterface, I want classes implementing this interface to also implement______"

Extending a class is somewhat similar, as you also extend a type, but the thing is that extending a class should be considered to be adding additional state/implementation to a type. Because of this, superclass constructors should be called to make sure that all the state associated with the superclass definitions is properly initialized.

2) How come multiple inheritance is not supported in Java but an interface can "extend" multiple other interfaces?

You have to be careful here, as there isn't just one kind of multiple inheritance. Java does support multiple inheritance of types, which is why you can implement multiple interfaces, which define types. What Java doesn't support is multiple inheritance of state (and multiple inheritance of implementation wasn't allowed until Java 8. See note below). This is why you can only extend 1 class -- because classes define state.

Thus, because you're allowed to inherit multiple types, extending multiple interfaces is perfectly OK for a given interface. It's like taking one contract and stapling several other contracts to the back of it. As long as the implementing class follows the entire thing, your program should compile.

Note: As of Java 8, you can now inherit multiple implementations, through default methods in interfaces. In the case of a conflict, the programmer must explicitly implement the method in question.

awksp
  • 11,764
  • 4
  • 37
  • 44