Interface

 Like a normal class, an Interface can have methods and variables, but the methods declared in an interface 

are by default abstract (only method signature(head), no body).

*Interfaces specify what a class must do and not how. It is a blueprint of the class. So it specifies a set of methods that the class has to implement.

*If a class implements an interface and does not provide method bodies for all functions specified in the interface, then the class must be declared abstract. 

*Interface abstract methods can not have body.

*By default methods in Interface class have (public abstract) keyword, but there is no need to type them.

For example: public abstact void start(); it is the same as void start();  By default there is always public abstact keyword!

package com.company;

public interface Startable {
public abstract void start(); // public abstract is by default, and there is no need to write them
}
package com.company;

public class BMW {
}
package com.company;

public class Mercedes extends BMW implements Startable {
@Override
public void start() {
System.out.println("Mercedes start");
}
}
package com.company;

public class Main {

public static void main(String[] args) {
BMW b1 = new Mercedes();
Startable s = (Startable) b1;
s.start();

BMW b2 = new BMW();
Startable s2 = (Startable) b2;
}
}
sfsdf

Output:

com.company.Mercedes

Mercedes start

ClassCastException

*Why do we get ClassCastException? 

The reason is BMW does not implements Startable of does not extends class which implements Startable interface.

But why it is possible to cast b1 object to Startable? As we notice Mercedes extends BMW and implements Startable interface. That means Mercedes is a BMW and has a characteristic of Startable.

However when we casting (Upcastin) Mercedes to BMW b1 object acquire characteristics of Mercedes and act like Mercedes, then we can cast(Downcast) b1 to Startable.


***Another crucial moment: We can create concrete method inside Interface.

package com.company;

public interface Startable {
public abstract void start(); // public abstract is by defaul, and there is no need to write them

default void foo(){
System.out.println("interface");
}
}

***Varialbes in Interface. We must initialize variables in Interface, because variables by 
default are public static final. 
package com.company;

public interface Startable {
int a = 10; // by default public static final
}
package com.company;

public class Main {

public static void main(String[] args) {
System.out.println(Startable.a);
}
}
Output:
10 

***We can not override static methods from Interface. The reason is simple, because static
methods belong to class. We can not override static methods at all! It does not matter either 
it is Interface, Abstract class or concrete classes.
***Interface can extend from another Interface.

package main;

public class Main {
public static void main(String[] args) {

class Main$1 extends Car {

@Override
public void start() {

}

@Override
public void stop() {

}

@Override
public void drive() {

}
}
Car car = new Main$1();
System.out.println(car.getClass().getName());
}

}
This is anonymous class. It shows how actually anonymous classes are created. In reality Java
creates a Main$1 class which extends from abstract class and overrides its method(s).

Комментарии

Популярные сообщения из этого блога

Lesson1: JDK, JVM, JRE

SE_21_Lesson_11: Inheritance, Polymorphism

SE_21_Lesson_9: Initialization Blocks, Wrapper types, String class