extends class and implements interface in java
I have an example to show why extends precedes implements in class declaration,
inerface :
public interface IParent {
void getName();
void getAddress();
void getMobileNumber();
}
abstract class :
public abstract class Parent {
public abstract void getAge();
public void getName(){
System.out.print("the parent name");
}
}
Child class :
public class Child extends Parent implements IParent {
@Override
public void getAddress() {
System.out.print(" Child class overrides the Parent class getAddress()");
}
@Override
public void getMobileNumber() {
System.out.print(" Child class overrides the Parent class getMobileNumber()");
}
@Override
public void getAge() {
//To change body of implemented methods use File | Settings | File Templates.
}
}
If you observe there is same method getName() in both interface and in abstract class, where in abstract class the method has the implementation.
When you try to implement, then it's mandatory for a class to override all the abstract methods of an interface and then we are trying to extend the abstract class which has already has an implementation for the method getName().
when you create an instance of a Child class and called the method getName() as
Child child = new Child();
child.getName();
There will a conflict for a child to call which method implementation as there will be two implementation for the same method getName().
To avoid this conflict they made it mandatory to extend first and implement an interface later.
so if an abstract class has the same method as in an interface and if abstract class has implemented the method already then for a child class its not necessary to override that method
extends
should go before implements
:
class Sub extends Super implements Colorable {}
This is because of a specification in JLS. And there is a certain order of elements when you attempt to declare a class in Java:
- Modifiers such as
public
,private
, etc. - The class name, with the initial letter capitalized by convention.
- The name of the class's parent (superclass), if any, preceded by
the keyword
extends
. A class can only extend (subclass) one parent. - A comma-separated list of interfaces implemented by the class, if
any, preceded by the keyword
implements
. A class can implement more than one interface. - The class body, surrounded by braces,
{ }
.
Reference:
http://docs.oracle.com/javase/tutorial/java/javaOO/classdecl.html
There is a rule in java if want to implement an interface and extend a class we must extend a class first then we implement an interface
interface A{}
class super{}
class sub extends super implements A {}
When the Java compiler turns a class into bytecode, it must first look to a parent class. That is because the underlying implementation of classes is to point to the bytecode of the parent class - which holds the relevant methods and fields. Then it adds in pointers to the code of the child class functions - some of which are mandated by the 'implements' keyword.
Because the parent class must be compilable, it is easier if the compiler knows up front what that class is. Further, you can extend only one class but implement any number of interfaces. The compilation time climbs if the extends keyword can be intermingled amongst any number of implements instructions. Compilers want to fail as fast as possible to decrease dev time, so this choice is logical. Further, it helps you think clearly about the class for the same reason.