Java | Interface :
~~ CORE-JAVA ~~
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
7. STRING MANIPULATIONS
7.2 String Buffer
7.3 String Tokenizer
8.1 Predefined packages
8.2 Userdefined Packages
8.3 Access Specifiers
9. EXCEPTION HANDLING
9.2 Pre Defined Exceptions
9.4 Throws, throw
9.5 User Defined Exception
10.2 Thread Creations
10.3 Thread Life Cycle
10.4 Life Cycle Methods
10.6 Wait() notify() notify all()
11.2 Wrapper Classes
12. COLLECTION FRAME WORK
12.2 List interface
12.3 Set interface 12.4 Map interface
13. INNER CLASSES
13.2 Member inner class
13.3 Static inner class
13.4 Local inner class
13.5 Anonymous inner class
14. CoreJava Interview Questions
- 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 :
- A Class can extend only one Class at a time.
- A Class can implement any number of interfaces at a time.
- A Class can extend a Class and implement any number of Interfaces simultaneously.
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.
Naming Conflicts in Interface Methods :
- 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.
- 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.
- 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 :
- 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.