1

I have one interface having three abstract methods.

public interface ThreeDemoInter {
    public void a();
    public void b();
    public void c();
}

I have one parent class having the same three method:

public class ParentThree {

    public void a(){
        System.out.println("parent a");
    }
    public void b(){
        System.out.println("parent b");
    }
    public void c(){
        System.out.println("parent c");
    }
}

and lastly one child class having two methods extending parent class and implementing the interface

public class ChildThree extends ParentThree implements ThreeDemoInter {

    public void a(){
        System.out.println("child a");

    }
    public void b(){
        System.out.println("child b");
    }

}

my concern is why not getting error of method c()

i know that it extending the parent class having this method but concept behind is not getting full clear. Thanks in advance. looking for explanation behind this code.

2
  • Because ChildThree class already has method c(). Commented May 6, 2016 at 5:32
  • Mind, it would be absurd, if @Override public void c() { super.c(); } would have been needed. Commented May 6, 2016 at 5:38

7 Answers 7

1

my concern is why not getting error of method c()

Because its parent class(ParentThree) has already the implementation defined for the method c() needed by the child class(ChildThree).

Rule says that any class that implements an interface must implement all the methods declared in the interface.

When the code is compiled to check the implementation dependencies, then the request is checked first from the parent class down to the hierarchy, thus resolving the implementation issue. So, the request for implementation of method c() has already been satisfied by the parent class.

Sign up to request clarification or add additional context in comments.

Comments

1

As we know that, sub-class has access to all of its parent class methods. we override only when we have to customize the method. means it has all three methods in access.thats why there is no need to implement method c().
hope, you got the point.

Comments

1

Because in your ParentThree you already have implemented those methods. And as per the inheritance all methods are accessible in child class. So all same methods in ChildThree are not implementing methods. but they are Overriding methods. Like in your code ChildThree overriding methods are

public void a(){..}
public void b(){..}

So, in your ChildThree you are not implementing ThreeDemoInter but overriding methods of ParentThree that is why you are not getting error as you were expecting.

Comments

1

There is one core rule in java that any class implements any interface then that class should also implements its all methods unless the class is abstract.

Now,In your case you have two class ParentThree that is parent class and ChildThree.

ChildThree is implementing ThreeDemoInter interface so it should implements its all methods and it is also extending ParentThree.

ChildThree is a child class of ParentThree so all the methods of Parenthree is accessible in ChildThree class so now in childThree we have all the methods a(),b() and c() that are implemented on parentThree class and childThree class is just overriding method a() and b ()

1 Comment

nice written! bit comfort to understand
0

it's because ChildThree class has extended the class ParentThree who have all three methods defined so after inheriting this class get inherited these methods also so no need to define these methods again if not neccessary.

Comments

0

If you list down the methods of the ChildThree class, using a classLoader or through reflection, it will list down three methods a, b and c. As it extends ParentThree class, these methods are available as it's own public methods

The interface defines that the class implementing it should have three methods a, b and c.

Interface implementation checks for presence of those methods on the implementing classes. ChildThree passes that test and hence it is a valid implementation.

Comments

0

looks like many great answers , now i m also come at point to conclude something about that code.

if a sub class extended the base then method got inherited to sub class

i also read the javadoc about sub class, here some lines.

What You Can Do in a Subclass

A subclass inherits all of the public and protected members of its parent, no matter what package the subclass is in. If the subclass is in the same package as its parent, it also inherits the package-private members of the parent. You can use the inherited members as is, replace them, hide them, or supplement them with new members:

The inherited fields can be used directly, just like any other fields.
You can declare a field in the subclass with the same name as the one in the superclass, thus hiding it (not recommended).
You can declare new fields in the subclass that are not in the superclass.
The inherited methods can be used directly as they are.
You can write a new instance method in the subclass that has the same signature as the one in the superclass, thus overriding it.
You can write a new static method in the subclass that has the same signature as the one in the superclass, thus hiding it.
You can declare new methods in the subclass that are not in the superclass.
You can write a subclass constructor that invokes the constructor of the superclass, either implicitly or by using the keyword super.

so, in this case if child class ChildThree extends the parent class ParentThree & implements the interface ThreeDemoInter, then not mandatory to define any method in child class like below code

public class ChildThree extends ParentThree implements ThreeDemoInter{
//if no any method works fine
}

Ref:-java inheritance

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.