0

I am trying to use extends keyword in place of implement to use interface is it possible in java.

Interface myinterface
{
     //methods
}

public class myclass extends myinterface
{
    //methods
}

Tell me the purpose of these two words extends and implements. why class is not use implement keyword to inherits the class from other class

3
  • Why? What is the motive to use this approach? Commented Oct 9, 2013 at 14:29
  • 6
    Short answer: No. Long answer: Noooooo. Commented Oct 9, 2013 at 14:29
  • 1
    NO! you can extends an interface with another interface, extending an interface in a class doesn't make sense! do you have problem with implements keyword?! Commented Oct 9, 2013 at 14:30

7 Answers 7

2

Think about the two words and what they are telling you.

Implements - means to put something into effect. An interface is regularly defined as a contract of what methods a class must have, or implement. Essentially you are putting that contract into effect.

Extends - means to make longer. By extending the class you are basically making it longer by also including all the methods of the extended class.

Two different words that are giving you, by definition, two different abilities within your code.

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

Comments

2

Interface cannot be extended but rather implemented. Interfaces can contain only constants, method signatures, and nested types. That is they only represent an abstraction of your model or can simply contain a list of constants.

Interfaces support inheritance. You can have for instance :

public interface InterfaceA extends InterfaceB

If you really want to extend from a class and have some abstract methods you can use an abstract class as :

public abstract class AbstractA {
  public abstract void myAbstractMethod;
}

public class A extends AbstractA {
  @Override 
  public abstract void myAbstractMethod {
       // your code
  }
}

Comments

1

No, you have to use implements with interfaces.

You can however make an abstract class if you absolutely need to use extend.

1 Comment

Interface can extend (inherit from) another interface.
1

Classes cannot extend an Interface. They can only implement them. Only an Interface can extend another Interface just like only a Class can extend another Class.

Tell me the purpose of these two words extends and implements.

When a class extends it inherits attributes and behaviour i.e. methods from the class it extends from. A class can only extend from one class since multiple inheritance isn't supported in Java.

When a class implements it provides behaviour i.e. implementation for the methods defined as stubs (just the signature without code) in the Interface it implements. A class can however implement multiple interfaces.

When an Interface extends another Interface its simply adding more methods to the list of methods that a Class implementing it needs to provide implementation for.

2 Comments

Tell me the purpose of these two words extends and implements. Why call is not use implement keyword to inherits the class
@user2842567 The main difference is that a class can only have one parent class (that is, one class it extends), but it can implement multiple interfaces.
0

As others, most succinctly @Stefan Beike, have said: no, you can't use extends when you mean implements. What you can do, if desired, is to add an in-between abstract class which implements your interface, and then extend that class. Sometimes this is done with empty implementations of the interface's methods, and then you only need to override the methods of interest in your child class. But it can be a purely abstract class if all you want is to use extends where implements would otherwise be called for.

Comments

0

Extends - is used by a class for extending some features of another class, so that same method or fields can be reused. Basic example can be :

 class Animal
{
  private String name;
   public void setName(String name)
   {
      this.name = name;
   }
   public int getLegs()
   {
      return 2;
   }
}

class Elephant extends Animal
{
   public int getLegs()
   {  
      return 4;
   }
}

Now, setter is reused and extends doesn't mandate it to be overriden, but as per requirement any method can be overriden also, as getter in our case.

Implements - A class can implement an interface. This helps in achieving abstraction. any method in interface needs to be implemented by any class that is implementing the interface. It is mandatory, until or unless class is abstract, in which case any other concrete class should implement the unimplemented methods.

So, a class can extends other class for reusing functionality, and a class can implement an interface to enforce some functionality that a class must provide by itself.

Now, why interface extends interface, I am also not sure, may be its because sub interface will extend the methods of super interface and it will enforce implementation of methods in super interface on class that is implementing the sub interface. As super interface does not enforce implementation on sub interface, so implements can not be used.

I hope I am clear.

1 Comment

Further to achieve something, where you can enforce implementation for some methods and provide some methods implementation, abstract class can be used.
0

class extends class (Correct)

class extends interface (Incorrect) => class implements interface (Correct)

interface extends interface (Correct)

interface extends class (Incorrect) (Never possible)

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.