Copyright © 2017, edureka and/or its affiliates. All rights reserved.
HOW TO DECLARE JAVA INTERFACE? DIFFERENCE BETWEEN INTERFACE AND CLASS www.edureka.co WHAT IS A JAVA INTERFACE? WHAT IS AN INTERFACE? ADVANTAGES AND DISADVANTAGES OF JAVA INTERFACE WHY DO WE NEED A JAVA INTERFACE?
WHAT IS AN INTERFACE? www.edureka.co
• An interface is a shared boundary across which two or more separate components of a computer system to exchange information. • The exchange can be between software, computer hardware, peripheral devices, humans, and combinations of these. www.edureka.co WHAT IS AN INTERFACE?
WHAT IS A JAVA INTERFACE? www.edureka.co
Interface is an abstract type that is used to specify a behavior that all the classes must implement. WHAT IS A JAVA INTERFACE? www.edureka.co
WHY DO WE NEED A JAVA INTERFACE? www.edureka.co
We need Java Interface to overcome the problem of Multiple inheritance. WHY DO WE NEED A JAVA INTERFACE? Super Class Class C Class A Class B www.edureka.co
WHY DO WE NEED A JAVA INTERFACE? Airplane Passenger Airplane Cargo Airplane www.edureka.co
WHY DO WE NEED A JAVA INTERFACE? Airplane Passenger Airplane Cargo Airplane Commercial Airplane www.edureka.co
WHY DO WE NEED A JAVA INTERFACE? Passenger Airplane Cargo Airplane Commercial Airplane www.edureka.co
WHY DO WE NEED A JAVA INTERFACE? Passenger Airplane Cargo Airplane Commercial Airplane IMPLEMENTING INTERFACES FOR CARGO PLANE www.edureka.co
HOW TO DECLARE A JAVA INTERFACE? www.edureka.co
HOW TO DECLARE A JAVA INTERFACE? interface <interface_name> { // declare constant fields // declare methods that abstract // by default. } SYNTAX TO DECLARE AN INTERFACE? www.edureka.co
CLASS V/S INTERFACE www.edureka.co
Interface supports Multiple Inheritance Class does not support to Multiple inheritance Multiple Inheritance www.edureka.co
Interface does not have Data Members Class includes Data Members Data Members www.edureka.co
Interface does not have Constructors Class includes Constructors Constructors www.edureka.co
Interfaces Includes Incomplete Member(Signature Member) Class Includes both Complete(Abstract) and Incomplete Members. Complete and Incomplete Members www.edureka.co
www.edureka.co Interface does not have Access Modifiers Class includes Access Modifiers Access Modifiers
www.edureka.co No member of an Interface is Static Class has complete Members of the class as Static. Static Members
ADVANTAGES AND DISADVANTAGES www.edureka.co
ADVANTAGES AND DISADVANTAGES • Through interfaces we can implement multiple inheritance in java. • Interfaces function to break up the complex designs and clear the dependencies between objects. • Interfaces makes your application loosely coupled. ADVANTAGES www.edureka.co
ADVANTAGES AND DISADVANTAGES DISADVANTAGES • Java interfaces are slower and more limited than other ones. • Interface should be used multiple number of times else there is hardly any use of having them. • Through interfaces we can implement multiple inheritance in java. • Interfaces function to break up the complex designs and clear the dependencies between objects. • Interfaces makes your application loosely coupled. ADVANTAGES www.edureka.co
KEY POINTS www.edureka.co
KEY POINTS CAN WE INSTANTIATE AN INTERFACE? We cannot create the object of an interface. Hence, We can’t instantiate an interface in Java. www.edureka.co
KEY POINTS ABSTRACTION Interface provides full abstraction as none of its methods have body. www.edureka.co
KEY POINTS IMPLEMENTING AN INTERFACE? • The Keyword Implement is used by a class to implement an Interface. • The method in an Interface must be provided as Public before implementing it in any Class. • Class must implement all the methods declared in an Interface, or else it must be declared as an Abstract Class. www.edureka.co
KEY POINTS ACCESS MODIFIERS IN AN INTERFACE • Interface cannot be declared as private, protected or transient. • All the interface methods are by default abstract and public. • Variables declared in interface are public, static and final by default. • Interface variables must be initialized at the time of declaration otherwise compiler will throw an error. • Inside any implementation class, you cannot change the variables declared in interface. www.edureka.co
KEY POINTS EXTENDING AN INTERFACE • An interface can extend any interface but cannot implement it. • A class can implement any number of interfaces. • If there are two or more same methods in two interfaces and a class implements both interfaces, implementation of the method once is enough. • A class cannot implement two interfaces that have methods with same name but different return type. • Variable names conflicts can be resolved by interface name. www.edureka.co
Copyright © 2019, edureka and/or its affiliates. All rights reserved.
www.edureka.co

What is Interface in Java | How to implement Multiple Inheritance Using Interface | Edureka

  • 1.
    Copyright © 2017,edureka and/or its affiliates. All rights reserved.
  • 2.
    HOW TO DECLAREJAVA INTERFACE? DIFFERENCE BETWEEN INTERFACE AND CLASS www.edureka.co WHAT IS A JAVA INTERFACE? WHAT IS AN INTERFACE? ADVANTAGES AND DISADVANTAGES OF JAVA INTERFACE WHY DO WE NEED A JAVA INTERFACE?
  • 3.
    WHAT IS ANINTERFACE? www.edureka.co
  • 4.
    • An interfaceis a shared boundary across which two or more separate components of a computer system to exchange information. • The exchange can be between software, computer hardware, peripheral devices, humans, and combinations of these. www.edureka.co WHAT IS AN INTERFACE?
  • 5.
    WHAT IS AJAVA INTERFACE? www.edureka.co
  • 6.
    Interface is anabstract type that is used to specify a behavior that all the classes must implement. WHAT IS A JAVA INTERFACE? www.edureka.co
  • 7.
    WHY DO WENEED A JAVA INTERFACE? www.edureka.co
  • 8.
    We need JavaInterface to overcome the problem of Multiple inheritance. WHY DO WE NEED A JAVA INTERFACE? Super Class Class C Class A Class B www.edureka.co
  • 9.
    WHY DO WENEED A JAVA INTERFACE? Airplane Passenger Airplane Cargo Airplane www.edureka.co
  • 10.
    WHY DO WENEED A JAVA INTERFACE? Airplane Passenger Airplane Cargo Airplane Commercial Airplane www.edureka.co
  • 11.
    WHY DO WENEED A JAVA INTERFACE? Passenger Airplane Cargo Airplane Commercial Airplane www.edureka.co
  • 12.
    WHY DO WENEED A JAVA INTERFACE? Passenger Airplane Cargo Airplane Commercial Airplane IMPLEMENTING INTERFACES FOR CARGO PLANE www.edureka.co
  • 13.
    HOW TO DECLAREA JAVA INTERFACE? www.edureka.co
  • 14.
    HOW TO DECLAREA JAVA INTERFACE? interface <interface_name> { // declare constant fields // declare methods that abstract // by default. } SYNTAX TO DECLARE AN INTERFACE? www.edureka.co
  • 15.
  • 16.
    Interface supports MultipleInheritance Class does not support to Multiple inheritance Multiple Inheritance www.edureka.co
  • 17.
    Interface does nothave Data Members Class includes Data Members Data Members www.edureka.co
  • 18.
    Interface does nothave Constructors Class includes Constructors Constructors www.edureka.co
  • 19.
    Interfaces Includes IncompleteMember(Signature Member) Class Includes both Complete(Abstract) and Incomplete Members. Complete and Incomplete Members www.edureka.co
  • 20.
    www.edureka.co Interface does nothave Access Modifiers Class includes Access Modifiers Access Modifiers
  • 21.
    www.edureka.co No member ofan Interface is Static Class has complete Members of the class as Static. Static Members
  • 22.
  • 23.
    ADVANTAGES AND DISADVANTAGES •Through interfaces we can implement multiple inheritance in java. • Interfaces function to break up the complex designs and clear the dependencies between objects. • Interfaces makes your application loosely coupled. ADVANTAGES www.edureka.co
  • 24.
    ADVANTAGES AND DISADVANTAGES DISADVANTAGES •Java interfaces are slower and more limited than other ones. • Interface should be used multiple number of times else there is hardly any use of having them. • Through interfaces we can implement multiple inheritance in java. • Interfaces function to break up the complex designs and clear the dependencies between objects. • Interfaces makes your application loosely coupled. ADVANTAGES www.edureka.co
  • 25.
  • 26.
    KEY POINTS CAN WEINSTANTIATE AN INTERFACE? We cannot create the object of an interface. Hence, We can’t instantiate an interface in Java. www.edureka.co
  • 27.
    KEY POINTS ABSTRACTION Interface providesfull abstraction as none of its methods have body. www.edureka.co
  • 28.
    KEY POINTS IMPLEMENTING ANINTERFACE? • The Keyword Implement is used by a class to implement an Interface. • The method in an Interface must be provided as Public before implementing it in any Class. • Class must implement all the methods declared in an Interface, or else it must be declared as an Abstract Class. www.edureka.co
  • 29.
    KEY POINTS ACCESS MODIFIERSIN AN INTERFACE • Interface cannot be declared as private, protected or transient. • All the interface methods are by default abstract and public. • Variables declared in interface are public, static and final by default. • Interface variables must be initialized at the time of declaration otherwise compiler will throw an error. • Inside any implementation class, you cannot change the variables declared in interface. www.edureka.co
  • 30.
    KEY POINTS EXTENDING ANINTERFACE • An interface can extend any interface but cannot implement it. • A class can implement any number of interfaces. • If there are two or more same methods in two interfaces and a class implements both interfaces, implementation of the method once is enough. • A class cannot implement two interfaces that have methods with same name but different return type. • Variable names conflicts can be resolved by interface name. www.edureka.co
  • 31.
    Copyright © 2019,edureka and/or its affiliates. All rights reserved.
  • 32.