Professional Documents
Culture Documents
02 StaticMethod
02 StaticMethod
===================
1) Upto Java 7v, in an interface we can only create abstract methods
2) From Java 8v onwards in addition to default methods we can also create
static implemented methods common to all implementation classes of this
interface
3) Syntax:
public static RT MN(PL) throws EL{ //logic }
For example:
interface Example{
void m1();
default void m2(){ }
static void m3(){ }
}
- For example:
interface Example{
void m1();
default void m2(){System.out.println("Example DM m2 from Example
interface"); }
static void m3(){System.out.println("Example SM m3 from Example
interface"); }
}
class Test02{
public static void main(String[] args){
Sample s1 = new Sample();
s1.m1();
s1.m2();
//s1.m3(); //CE: c f s
System.out.println();
System.out.println();
Example.m3();
//Sample.m3();
System.out.println();
s1.m4();
}
};
4) What is the difference between interface static method and final method, are
they same?
A) Nooooooooo, both are different
Diff #1:
- final method is inherited to sub class, but we can to override
means a final can be called by using sub class refence, but we
can not redefined it in sub clss
5) What is the difference between interface static method and private method, are
they same?
A) Nooooooooooo, both are different
- interface static method we can call either in implementation class and in
user class
- but private method we can not call from other classes
- common point is
if override either private method or interface static method.
it is not considered as overriding method, overriding 4 rules are
not applied
we can add any RT, ELMs and AMs
6) What is the differences between inteface static method and class static method,
are they same?
A) Noooooooooooo, both are different
- class static method is inherited to sub class, can override in sub class,
all 4 overriding rules are applied
we can call it either by using super class name, super class referenced
variable,
or by using sub class name, sub class
referenced variable
- inteface static method is not inherited to sub class, can not override in
sub class,
if we override, overriding rules are not applied, it is consided as sub
class own method
we can add any RT, ELMs and AMs
7) What are the the differences between default method and static methods in an
interface?
- When you
1) do not want inherit a method to implementation clases and
2) do no want to allow implmentation classes to override the
interface given default implementations,
3) do want to force to invoke this method
only by using interface name, no by using implementation
class reference
then you must choose static method
//Example.m2(); //CE:
//Sample.m2(); //CE:
8) Identify are there any compile time errors in the below program?
interface Example{
void m1();
default void m2(){System.out.println("Example DM m2 from Example
interface"); }
static void m3(){System.out.println("Example SM m3 from Example
interface"); }
}
void m4(){
this.m1();
this.m2();
this.m3();
Efg.m3();
Example.m3();
}
};
class Test02{
public static void main(String[] args){
Sample s1 = new Sample();
s1.m1();
s1.m2();
//s1.m3(); //CE: c f s
System.out.println();
System.out.println();
Example.m3();
//Sample.m3();
System.out.println();
s1.m4();
System.out.println();
Efg e = new Efg();
e.m4();
}
};