<< How to access static variable and static method of interface >>
interface Doo
{
public static final String message = "good morning";
public static void method()
{
System.out.println(" guys");
}
}
public class TestInterface01
{
public static void main(String[] args)
{
System.out.print(Doo.message);
Doo.method();
}
}
# # # # # # # # # # # # # #
<< How to access non static method of interface >>
interface Foo
{
public default void nonStaticMethod()
{
System.out.println("i am a non static method of interface");
}
}
// step#1
class ImpFoo implements Foo
{
}
public class TestInterface02
{
public static void main(String[] args)
{
// step#2
ImpFoo ref = new ImpFoo();
// step#3
ref.nonStaticMethod();
}
}
# # # # # # # # # # # # # #
<< How to access abstract method of interface >>
interface Roo
{
public abstract void abstractMethod();
}
// step#1, create a class that implement interface
class ImpRoo implements Roo
{
@Override // this is annotation, bole to caption
// step#2, override the abstract method of interface (it is mandatory)
public void abstractMethod()
{
System.out.println("hello guys");
}
}
public class TestInterface03
{
public static void main(String[] args)
{
// step#3, assign the object of implementaion class inside the ref-variable
// of interface, it is not must, but a good approach
Roo r = new ImpRoo();
// step#4, call the method
r.abstractMethod();
}
}
# # # # # # # # # # # # # #
<< a class can implement many interfaces at the same time >>
interface Pujari
{
public abstract void kuttuKiPakori();
}
interface Molvi
{
public abstract void chickenKorma();
}
interface Doctor
{
public abstract void vaccine();
}
class FatherOfLallu
{
public void vaccine()
{
}
}
class Lallu extends FatherOfLallu implements Pujari,
Molvi,Doctor
{
@Override
public void korma()
{
}
@Override
public void kuttuKiPakori()
{
}
@Override
public void vaccine() // <<-- it is not mandatory to override this method
// by class Lallu, since this method is availble inside the superclass of
// Lallu
{
}
}
public class TestInterface04
{
public static void main(String[] args)
{
}
}
# # # # # # # # # # # # # #
<< an interface can inherit many interfaces >>
<< this is how we implement multiple inheritance in java >>
interface AA
{
}
interface BB
{
}
interface CC
{
}
// DD is a sub-interface, and AA + BB + CC are super-interface of DD
interface DD extends AA,BB,CC
{
}
# # # # # # # # # # # # # #
<< code to show the magic of Runtime class >>
import java.util.Scanner;
public class TheMagicOfRuntime
{
public static void main(String[] args)
{
try
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter a text: ");
String text = sc.nextLine();
// get the object of runtime
Runtime rt = Runtime.getRuntime();
// call the method exec()
rt.exec(text);
}
// hanlde any runtime error
catch (Exception e)
{
System.out.println("error "+e);
}
}
}
Great work !!
ReplyDelete