Friday, 1 February 2019

Java : Interfaces in Java

package udemy;

public interface InterfaceForClass {

int min_bal=100;

public abstract void Credit();
public abstract void Debit();

}


package udemy;

public class ClassForInterface implements InterfaceForClass{

public void Credit() {
System.out.println("From Credit Method");
}

public void Debit() {
System.out.println("From Debit Method");
}
public void MethodNotinInterface() {
System.out.println("this method does not belong to interface");
}

public static void main(String []args) {
ClassForInterface c1=new ClassForInterface();
c1.Credit();
c1.Debit();
c1.MethodNotinInterface();
System.out.println("@@@@@@@@@@@@Separator@@@@@@@@@@@@@@@@@");
InterfaceForClass i=new ClassForInterface();
i.Credit();
i.Debit();
System.out.println(min_bal); //=>100
min_bal=min_bal+1; // THIS IS NOT POSSIBLE. VARIABLES IN INTERFACE ARE FINAL
i.MethodNotinInterface() //THIS METHOS WILL NOT POP UP
}
}



//Output:

From Credit Method
From Debit Method
this method does not belong to interface
@@@@@@@@@@@@Separator@@@@@@@@@@@@@@@@@@@@@@@

//Below Accessed from InterFace reference
From Credit Method
From Debit Method
100


No comments:

Post a Comment

Spring Boot : Exception Handler 14