Java | Interface :


             ~~ CORE-JAVA ~~


     1.1 Java Intorduction
     1.2 Java history
     1.3 Java features
     1.4 Diff b/w Java & C++
     1.5 Java Variables
     1.6 Java Data Types
     1.7 Typecasting
     1.8 Arrays

 2. OOPS

     2.1 Introduction
     2.2 Class
     2.3 Object
     2.4 Static Keywords
     2.5 Constructors
     2.6 This KeyWord


     3.1 Inheritance (IS-A) 
     3.2 Aggregation (HAS-A)


     4.1 Polymorphism & Its Type
     4.2 Method Overloading
     4.3 Method Overriding
     4.4 Super Keyword
     4.5 Final Keyword
     4.6 Dynamic Binding


     5.1 Abstract Classes & Methods
     5.2 Interfaces
     5.3 Abstract vs Interface


     6.1 Encapsulation


     7.1 String
     7.2 String Buffer
     7.3 String Tokenizer


     8.1 Predefined packages
     8.2 Userdefined Packages
     8.3 Access Specifiers


     9.1 Introduction
     9.2 Pre Defined Exceptions
     9.3 Try-Catch-Finally
     9.4 Throws, throw
     9.5 User Defined Exception


     10.1 Introduction
     10.2 Thread Creations
     10.3 Thread Life Cycle
     10.4 Life Cycle Methods
     10.5 Synchronization
     10.6 Wait() notify() notify all()


     11.1 Introduction
     11.2 Wrapper Classes


     12.1 Introduction
     12.2 List interface
     12.3 Set interface                     12.4 Map interface                           


     13.1 Introduction
     13.2 Member inner class
     13.3 Static inner class
     13.4 Local inner class
     13.5 Anonymous inner class

 14. CoreJava Interview Questions

Introduction :

  • In Java, we know Interface is an another way to achieve Abstraction.
  • Interface is just like a Java Class but it contains only abstract method and static constants. We can say Interface is 100% pure abstraction.
  • We can’t create Object of Interfaces but we can create References for it.
  • Here also same as Abstract Class, like internal implementation will be hide and it will expose whatever services we are providing.
    • For example – By using Bank ATM GUI Screen, Bank people will highlight the set of services only what they are offering. They are not exposing there internal details or implementation to customer.
  • The main advantage of Interfaces are :
    • We can achieve Security, because we are not highlighting our internal implementations.
    • Enhancement will be very easy, because without effecting outside person we can change our internal implementation.
    • Two different system can communicate easily via Interface.


Declaration & Implementation of Interface :

We can declare an Interface using interface keyword, and we can implement Interface using implement keyword as below example.

  • If a Class implements as Interface then compulsory, we should provide implementation of every method of that Interface in our class. Otherwise we have to declare our class as Abstract Class only.
  • Whenever we are implementing an interface methods, compulsory it should be declared as Public only.


Extends vs Implements :

  1. A Class can extend only one Class at a time.
  2. A Class can implement any number of interfaces at a time.
  3. A Class can extend a Class and implement any number of Interfaces simultaneously.

Example :


Interface Methods :

In Interface whether we are declaring or not, every Interface Methods will be public and abstract by-default.

    • Public – To make this method available for every implementation Class.
    • Abstract – Because in interface every method will be declared only, not implementation.

Example :


Naming Conflicts in Interface Methods :

  1. If two Interfaces Contains a method with same singnature and same return type in the implementation Class we can provide implementation for only one method.
  2. If two Interfaces Contains a method with same name but different args then in the implementation class we have to provide implementation for both methods & these methods are considered as Overloaded Methods.
  3. If two interfaces contains a method with same signature but different return type, then it is impossible to implement both interfaces at a time.


Interface Variables :

In Interface whether we are declaring or not, every Interface Variables will be public, static and final by-default.

    • Public – To make this variable available for every implementation Class.
    • Static – Without existing Object also implementation class can access.
    • Final – Implementation Class can access this variable, but can’t modify.
  • For the Interface Variable Compulsory we should perform initialization at the time of declaration only otherwise will get Compile Time Error.
  • Inside Implementation Classes we can access interface variables but we can’t modify there values.


Naming Conflicts in Interface Variables :

  1. There may be a chance of 2 interface contains some variable with same name, But we can resolve this naming conflicts by using Interface name.


Multiple Inheritance using Interface :



Marker Interface :

Help Others, Please Share