Reputation: 13
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
Upvotes: 0
Views: 2721
Reputation: 483
class extends class (Correct)
class extends interface (Incorrect) => class implements interface (Correct)
interface extends interface (Correct)
interface extends class (Incorrect) (Never possible)
Upvotes: 0
Reputation: 8653
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.
Upvotes: 0
Reputation: 40336
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.
Upvotes: 0
Reputation: 51711
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.
Upvotes: 1
Reputation: 222
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.
Upvotes: 2
Reputation: 2220
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
}
}
Upvotes: 2
Reputation: 451
No, you have to use implements
with interfaces.
You can however make an abstract class
if you absolutely need to use extend.
Upvotes: 1