Professional Documents
Culture Documents
Intern 2
Intern 2
LECTURE 2
Today’s Agenda
RULES:
1. Every method present inside interface is always
public and abstract (whether we use these
keywords or not) .
a static method
What Is A Default Method ?
• Syntax:
• Example:
interface Greet
{
default public void message()
{
System.out.println(“Good Morning”);
}
}
What Is A Default Method ?
• Code:
package defaultmethodexample;
public interface SampleInterface
{
public void method1(); // this is abstract method
default public void method2() // this is default method
{
System.out.println(“SampleInterface.method2”);
}
}
Example
@Override
public void method1() {
System.out.println("In SampleImpl.method1");
}
}
Example
• Code
package defaultmethodexample;
public class UseSample {
public static void main(String[] args) {
SampleInterface obj=new SampleImpl();
obj.method1();
obj.method2();
}
}
In SampleImpl.method1
• Output:
In SampleInterface.method2
Overriding Default Method
package defaultmethodexample;
public class SampleImpl implements SampleInterface{
@Override
public void method1() {
System.out.println("In SampleImpl.method1");
}
@Override
public void method2() {
System.out.println("In SampleImpl.method2");
}
}
Overriding Default Method
In SampleImpl.method1
• Output:
In SampleImpl.method2
Extending Interfaces That
Contain Default Method
• Code:
package defaultmethodexample;
public interface SampleInterface1 {
default public void method1(){
System.out.println("In SampleInterface1.method1");
}
}
Example(Case 1)
• Code:
package defaultmethodexample;
public interface SampleInterface2 extends SampleInterface1{
}
Example(Case 1)
• Code:
package defaultmethodexample;
public class SampleImpl implements SampleInterface2{
}
Example(Case 1)
In SampleInterface1.method1
• Output:
Example (Case 2)
• Code:
package defaultmethodexample;
public interface SampleInterface1 {
default public void method1(){
System.out.println("In SampleInterface1.method1");
}
}
Example(Case 2)
• Code:
package defaultmethodexample;
public interface SampleInterface2 extends SampleInterface1{
public void method1(); // this method now becomes abstract
}
Example(Case 2)
• Code:
package defaultmethodexample;
public interface SampleInterface1 {
default public void method1(){
System.out.println("In SampleInterface1.method1");
}
}
Example(Case 3)
• Code:
package defaultmethodexample;
public interface SampleInterface2 extends SampleInterface1{
default public void method1(){
System.out.println("In SampleInterface2.method1");
}
}
Example(Case 3)
• Code:
package defaultmethodexample;
public class SampleImpl implements SampleInterface2{
}
Example(Case 3)
In SampleInterface2.method1
• Output:
Default Methods And
Multiple Inheritance
}
Error: class Greetings inherits unrelated defaults for
Output: greet() from types Greet1 and Greet2
Solution
<interface_name>.super.<method_name>();
Example
Output:
Good Evening
Calling Interface’s Method
Good Morning
Output: Good Afternoon
Overriding Object Class
Methods
• Syntax:
• Example:
interface Greet
{
static public void message()
{
System.out.println(“Good Morning”);
}
}
Important Points
• Code:
package staticmethodexample;
public interface SampleInterface
{
public void method1(); // this is abstract method
public static void method2() // this is static method
{
System.out.println(“In SampleInterface.method2”);
}
}
Example
• Code:
package staticmethodexample;
public class SampleImpl implements SampleInterface{
@Override
public void method1() {
System.out.println("In SampleImpl.method1");
}
}
Example
• Code
package staticmethodexample;
public class UseSample {
public static void main(String[] args) {
SampleInterface obj=new SampleImpl();
obj.method1();
SampleInterface.method2();
//if code below is uncommented , it will show
error
//obj.method2();
}
}
In SampleImpl.method1
In SampleInterface.method2
• Output:
Important Points
Output:
Good Morning
Good Evening
Benefits Of Static Method
• Code
Interface never talks about state of Abstract class can talk about state of
Object. Object.