day-29: Java Interface Explained: Rules, Real-World Examples, and Multiple Inheritance

What is interface in java? Interface like contract between two of them. Interface methods always in implement in different class. You and company: 1). 2 days leave can taken 2). upskill 1). Interface is not class - True public interface Company_rules { int leaves = 10; boolean variable_pay = true; public void work(); public void rotate_shift(); public void relocate(); public void upskill(); public void apply_leave(); // method define and can't implement, no body } public interface Player_rules { public int player_rules(); public int practice(); } public class Amma { public void eat() { System.out.println("have you had your dinner..."); } } public class Employee extends Amma implements Company_rules, Player_rules { public static void main(String[] args) { // TODO Auto-generated method stub Employee empl1 = new Employee(); empl1.work(); System.out.println(Company_rules.leave); // Static variable from the interface can be access via interface name with variable name System.out.println(Employee.variable_pay); // Static variable from the interface can be access via implement class name } @Override // definition by someone and implements public void work() { // TODO Auto-generated method stub } @Override public void rotate_shift() { // TODO Auto-generated method stub } @Override public void relocate() { // TODO Auto-generated method stub System.out.println("move to blr..."); } @Override public void upskill() { // TODO Auto-generated method stub } @Override public void apply_leave() { // TODO Auto-generated method stub } @Override public int player_rules() { // TODO Auto-generated method stub return 0; } @Override public int practice() { // TODO Auto-generated method stub return 0; } } // @override means - method can be create by some and implement by someone 2). If method is interface mode if we again abstract that will be consider as redundant method. - True 3). methods in interface return datatypes - public void apply_leave(); arguments - public void apply_leave(int leave_days); method signature - access modifier - public, private, default, protect, final and static. Static is common for all. and it will be called by class name. final methods cannot be overridden. final fields cannot be changed. So, Interface won't support Final Key word. Private also won't support. interface also supports local variables: public void apply_leave(int number); It will support global variables: int leave = 10; boolean variable_pay = true; if variable name is italy format that is static, in interface all the variables are static. Is java supports multiple inheritance? yes, not directly public class Employee extends Amma implements Company_rules { // multiple inheritance public static void main(String[] args) { // TODO Auto-generated method stub Employee empl1 = new Employee(); empl1.work(); empl1.play(); } } Static variable from the interface can be access via interface name with variable name Static variable from the interface can be access via implement class name { System.out.println(Company_rules.leave); System.out.println(Employee.variable_pay); } Class can be more than one interfaces To create New Object: Classname refer_name = new classname(); Calculator cal = new Calculator(); For validation purpose interface can verify the employee class. This is also called as dynamic Biding. interface_name from_interface_reference = new Class_name(); parent_class partent_Object = new parent_class(); Company_rules manager = new Employee(); manager.apply_leave(); //dynamic biding Amma object_amma = new Employee(); object_amma.eat(); you are allocating the new place for your manager and coach to validate the their given method in your class. Dynamic Binding example chart with teacher role: ---------------------------------- end of the blog ---------------------------------------

Apr 20, 2025 - 13:56
 0
day-29: Java Interface Explained: Rules, Real-World Examples, and Multiple Inheritance

What is interface in java?
Interface like contract between two of them.

Interface methods always in implement in different class.

You and company:
1). 2 days leave can taken
2). upskill

1). Interface is not class - True


public interface Company_rules {

   int leaves = 10;
   boolean variable_pay = true;

    public void work();
    public void rotate_shift();
    public void relocate();
    public void upskill();
    public void apply_leave();
        // method define and can't implement, no body

}

public interface Player_rules {

    public int player_rules();
    public int practice();
}

public class Amma {

    public void eat() {
        System.out.println("have you had your dinner...");  
    }
}


public class Employee extends Amma implements Company_rules, Player_rules {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Employee empl1 = new Employee();
         empl1.work();

         System.out.println(Company_rules.leave); 
// Static variable from the interface can be access via interface name with variable name

         System.out.println(Employee.variable_pay); 
// Static variable from the interface can be access via implement 
class name        

    }

    @Override // definition by someone and implements
    public void work() {
        // TODO Auto-generated method stub

    }

    @Override
    public void rotate_shift() {
        // TODO Auto-generated method stub

    }

    @Override 
    public void relocate() {
        // TODO Auto-generated method stub
           System.out.println("move to blr...");

    }

    @Override
    public void upskill() {
        // TODO Auto-generated method stub

    }

    @Override
    public void apply_leave() {
        // TODO Auto-generated method stub

    }
    @Override
    public int player_rules() {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public int practice() {
        // TODO Auto-generated method stub
        return 0;
    }
}

// @override means - method can be create by some and implement by someone

2). If method is interface mode if we again abstract that will be consider as redundant method. - True

3). methods in interface
return datatypes - public void apply_leave();
arguments - public void apply_leave(int leave_days);
method signature - access modifier - public, private, default, protect, final and static.

Static is common for all. and it will be called by class name.

final methods cannot be overridden.
final fields cannot be changed.

So, Interface won't support Final Key word.
Private also won't support.

interface also supports local variables:
public void apply_leave(int number);

It will support global variables:
int leave = 10;
boolean variable_pay = true;

if variable name is italy format that is static, in interface all the variables are static.

Is java supports multiple inheritance? yes, not directly

public class Employee extends Amma implements Company_rules { 
// multiple inheritance

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Employee empl1 = new Employee();
         empl1.work();
         empl1.play();

    }
}

Static variable from the interface can be access via interface name with variable name

Static variable from the interface can be access via implement class name

    {
         System.out.println(Company_rules.leave); 
         System.out.println(Employee.variable_pay); 

   }

Class can be more than one interfaces

To create New Object:
Classname refer_name = new classname();
Calculator cal = new Calculator();

For validation purpose interface can verify the employee class.
This is also called as dynamic Biding.

interface_name from_interface_reference = new Class_name();
parent_class partent_Object = new parent_class();

     Company_rules manager = new Employee();
     manager.apply_leave(); //dynamic biding

     Amma object_amma = new Employee(); 
     object_amma.eat();

you are allocating the new place for your manager and coach to validate the their given method in your class.

Dynamic Binding example chart with teacher role:

Image description

---------------------------------- end of the blog ---------------------------------------