Jon Snow
Jon Snow

Reputation: 713

Inheritance and Interface

I've been trying to understand inheritance when interfaces are involved. I want to know how the subclasses are created if they follow the following:

For Example, let's say i have:

  1. a Superclass which implements an Interface I
  2. and couple of subclasses which extend the superclass A

My Questions

  1. Do i have to provide the implementation of the interface methods 'q and r' in all of the subclasses which extend A?

  2. If i don't provide the implementation of the interface in a subclass will i have to make that subclass an Abstract Class?

  3. Is it possible for any of the subclass to implement I? e.g. Class C extends A implements I, is this possible? even though it's already extending a superclass which implements I?

  4. Let's say i don't provide the implementation of method r from the interface I, then i will have to make the superclass A and Abstract class! is that correct?

My example code:

    //superclass
    public class A implements I{
    x(){System.out.println("superclass x");}
    y(){System.out.println("superclass y");}
    q(){System.out.println("interface method q");}
    r(){System.out.println("interface method r");}
    }

    //Interface
    public Interface I{
    public void q();
    public void r();
    }

    //subclass 1
    public class B extends A{
    //will i have to implement the method q and r?
    x(){System.out.println("called method x in B");}
    y(){System.out.println("called method y in B");}
    }

    //subclass 2
    public class C extends A{
    //will i have to implement the method q and r?
    x(){System.out.println("called method x in C");}
    y(){System.out.println("called method y in C");}
}

Upvotes: 11

Views: 10592

Answers (4)

Bhesh Gurung
Bhesh Gurung

Reputation: 51030

Only an abstract-class can keep them abstract, meaning an abstract-class is not required to provide an implementations for the methods in the interface.

Since, A is concrete it must provide the implementations. Then the subclasses of A will just inherit those implementation from A.

But, if A was abstract and didn't provide implementations for the methods, then B and C would have to provide the implementations.

Upvotes: 1

Kiwi
Kiwi

Reputation: 136

An interface is a promise to the outside world that "I can provide these methods."

1) and 2) and 4) Since superclass A already implements interface I, it has promised the outside world. Superclass A can fulfil that promise by:

  • implementing the method - In this case, your subclass has already inherited that method and doesn't need to implement anything.
  • Declaring itself abstract - In this case, your subclass must either implement the abstract method, or declare itself abstract too and "pass the buck" down to any class extending the subclass.

3) All the subclasses of the superclass A already implement I because they inherit the "promise", so Class C extends A implements I is redundant.

Upvotes: 0

PermGenError
PermGenError

Reputation: 46408

1: NO, if you implemnt them in your superclass,its not required to implement them in your subclasses

2: If you dontimplement the methods in your Superclass then you havetomake it abstract and then make your concrete subclasses implement those methods

3: yes, but absolutely redundant as your superclass is already implementing thrm.

4: yep, and you should implement those methods in the class when extends your superclass

Upvotes: 1

Jonathon Ashworth
Jonathon Ashworth

Reputation: 1114

1) No, you do not need to implement the methods in the subclasses, because they are already defined in the superclass. The subclass will inherit those method definitons.

2) No, see 1. The only exception is if the superclass is abstract and doesn't implement the interface, then you will need to implement it in the subclass if the subclass is not abstract.

3) No. It might compile properly, but will have no effect, and so shouldn't be done.

4) Yes, this is correct. If you do not implement a method from the interface, you need to make the class abstract.

Upvotes: 8

Related Questions