Unit 3
Unit 3
• Syntax
interface <interface_name>{
package com.Interface;
interface TestInterface1 public static void main(String[] args) {
{ // TODO Auto-generated method stub
// abstract method defaultmethod d = new
public void square(int a); defaultmethod();
d.square(4);
// default method
default void show()
{
// default method executed
System.out.println("Default Method Executed"); d.show();
} }
}
public class defaultmethod implements }
TestInterface1 {
public void square(int a)
{
System.out.println(a*a);
}
Static methods in interfaces
interface TestInterface class TestClass implements
TestInterface
{ {
// abstract method // Implementation of square
abstract method
public void square (int a); public void square (int a)
{
// static method System.out.println(a*a);
static void show(){ }
System.out.println("Static public static void main(String args[])
Method Executed"); {
TestClass d = new TestClass();
} d.square(4); // Static method
} executed
TestInterface.show();
}
The interfaces can have static methods as well
} which is similar to static method
of classes.
Packages
package p1;
public class B
{
//body of B
} //Adding class B in p1 // B.java file
Hiding classes
package p1;
public class x
{
//body of x
}
class Y
{
//body of y
}
import p1.*;
X objectX;
Y objectY;
Access Protection
Top Level class can be
public,default,abstract,final