Unit III Inheritance, Interface and Package Mr. R. M. Patil SY CO, JPR 2023-2024
3.3. Interface in Java An interface in Java is a blueprint of a class . It has static constants and abstract methods . The interface in Java is a mechanism to achieve abstraction . There can be only abstract methods in the Java interface, not method body. It is used to achieve abstraction and multiple inheritance in Java. In other words, you can say that interfaces can have abstract methods and variables. It cannot have a method body.
3.3. Interface in Java Why use Java interface?
3.3. Interface in Java How to declare an interface? An interface is declared by using the interface keyword. It provides total abstraction; means all the methods in an interface are declared with the empty body, and all the fields are public, static and final by default. A class that implements an interface must implement all the methods declared in the interface. interface < interface_name > { // declare constant fields // declare methods that abstract // by default. }
3.3. Interface in Java
3.3. Interface in Java As shown in the figure given below, a class extends another class, an interface extends another interface, but a class implements an interface .
3.3. Interface in Java Java Interface Example: interface print { void show(); } class demo implements print { public void show() { System.out.println ("Hello"); } public static void main(String args []) { demo d = new demo(); d.show (); } }
3.3. Interface in Java Java Interface Example: (Multiple Inheritance) interface first { public void dispaly (); } interface second { public void show(); } class Demo implements first, second { public void display() { System.out.println ("Some text.."); } public void show() { System.out.println ("Some other text..."); } } class test { public static void main(String[] args ) { Demo d = new Demo(); d.display (); d.show (); } }
3.3. Interface in Java – Extending Interface-Hybrid Inheritance While inheriting an interface through another interface “ extends ” keyword is used. Hybrid inheritance is the combination of more than one type of inheritances. Since java doesn’t support multiple inheritance with only classes, the hybrid inheritance is also not possible with only classes. In java, we can achieve hybrid inheritance through Interfaces .
3.3. Interface in Java – Extending Interface-Hybrid Inheritance interface A { ….. } interface B extends A { ….. } interface C extends A { ….. } class D implements B, C { ….. ….. }
3.3. Interface in Java – Extending Interface-Hybrid Inheritance interface A { void display1(); } interface B extends A { void display2(); } interface C extends A { void display3(); } class D implements B, C { void display1(); { System.out.println (“Class A”); } void display2(); { System.out.println (“Class B”); } void display3(); { System.out.println (“Class C”); } }
3.3. Interface in Java – Extending Interface-Hybrid Inheritance class demo { public static void main(String args []) { D d1 = new D(); d1.display1(); d1.display2(); d1.display3(); } }
3.3. Interface in Java – Interface Reference Runtime polymorphism can be achieved using interface. We can create reference for parent interface . This reference can point to an object of the child classes which implements the corresponding interfaces.
3.3. Interface in Java – Interface Reference Example: class demo { public static void main(String args []) { area a; a = new circle(); a.calc (); //Call to circle class method a = new triangle(); a.calc (); //Call to triangle class method } }
3.3. Interface in Java – Nested Interface An interface i.e. declared within another interface or class is known as nested interface. The nested interfaces are used to group related interfaces so that they can be easy to maintain. The nested interface must be referred by the outer interface or class. It can't be accessed directly.
Syntax of nested interface which is declared within the interface: interface interface_name { ... interface nested_interface_name { ... } } Syntax of nested interface which is declared within the class: class class_name { ... interface nested_interface_name { ... } } 3.3. Interface in Java – Nested Interface
3.3. Interface in Java – Nested Interface - Rules
interface Showable { void show(); interface Message { void msg (); } } class demo implements Showable.Message { public void msg () { System.out.println ("Hello nested interface"); } public static void main(String args []) { Showable.Message m=new demo(); m.msg(); } } 3.3. Interface in Java – Nested Interface - Example
Homework 1. What is interface? With suitable program explain the use of interface. W14, W15, S16, S17 – 8M 2. Explain with example how to achieve multiple inheritance with interface. S15, S16, W16, W17, S18 – 8M 3. Write a program to implement following inheritance. W14, S17 – 4M interface : salary basic_salary basic_sal () class : employee name, age display() class : gross_salary TA, DA, HRA total_sal ()
Homework 4. Write a java program to extend interface assuming suitable data. S18 - 6M 5. Difference between Abstract Class and Interface. W14, S16 – 4M