OCJP

Interface in Java (Basics)

Java does not support multiple inheritance, means there must be only single parent class in Java. But we can implement Interfaces to work like multiple inheritance. Below is the basic example for interface. In our later posts we will discuss in depth.

interface Interface1{
	void method1();
	void method2(int x);
}

class Demo1 implements Interface1{
	@Override
	public void method1(){
	System.out.println("Method 1");
	}
	
	@Override
	public void method2(int x){
	System.out.println("Value is "+ x);
	}
}
class InterfaceDemo1{
	public static void main(String []a){
	
	Demo1 demoObj= new Demo1();
	demoObj.method1();
	demoObj.method2(20);	
	}
}

Leave a Reply

Your email address will not be published. Required fields are marked *


× How can I help you?