Interfaces just like classes in Java, can be extended. A class implementing the outer interface must implement all the methods of the super class. A class implementing the sub interface must implement all the methods of sub interface as well as methods of super interface. If it fails to do so, compiler reports an error, that it should either implement the missing method or declare itself as Abstract.
I have written the program that clearly describes all the concepts associated with it. Interface Print.java is same as my previous article.
I have written the program that clearly describes all the concepts associated with it. Interface Print.java is same as my previous article.
package InterfaceIllustraion; interface Print { void Printme(); interface Printme { void Printmetoo(); } }Interface PrintIt in PrintIt.java extends the interface Print.
package InterfaceIllustraion; interface PrintIt extends Print { void PrintThis(); }Class Prints in Prints.java implements PrintIt interface and must implement all the methods of interface Print and PrintIt.
package InterfaceIllustraion; class Prints implements PrintIt { public void Printme() { System.out.println("Inside PrintMe() Method"); } public void PrintThis() { System.out.println("Inside PrintThis() Method of Class Prints"); } public static void main(String...args) { Prints p = new Prints(); p.Printme(); p.PrintThis(); } }To compile the program
javac -d . Print.java javac -d . PrintIt.java javac -d . Prints.javaAnd to run
java InterfaceIllustraion.PrintsHere is the sample output that I received in my machine.
Inside PrintMe() Method Inside PrintThis() Method of Class Prints
No comments:
Post a Comment