✔ JAVA CHAPTERS:-
1. Introduction to Java
2. Features of Java
3. Java Virtual Machine
4. D/F B/W JVM, JRE, JDK
5. Java Data Types
6. Java Programs
7. Setting Java Path
8. OOPs Concepts
9. Object & Class In Java
10. OOPs Continued
11. Encapsulation & Polymorphism
12. Method Overloading
13. Method Overriding
14. Dynamic Method Dispatch
15. Association In Java
16. Inheritance in Java
17. Aggregation in java
18. Command Line Argument
19. Execute Command Line
20. Read Input From Command Line
21. Abstract Class In Java
22. Interface in Java
23. Cutom Marker Interface
24. Constructor In Java
25. Package in Java
26. Acess Modifier In Java
27. Static Import In Java
28. Package Class In Java
29. This Keyword
30. Instance Intializer Block
31. Super Keyword
32. Static Keyword
33. Final Keyword
1. Introduction to Java
2. Features of Java
3. Java Virtual Machine
4. D/F B/W JVM, JRE, JDK
5. Java Data Types
6. Java Programs
7. Setting Java Path
8. OOPs Concepts
9. Object & Class In Java
10. OOPs Continued
11. Encapsulation & Polymorphism
12. Method Overloading
13. Method Overriding
14. Dynamic Method Dispatch
15. Association In Java
16. Inheritance in Java
17. Aggregation in java
18. Command Line Argument
19. Execute Command Line
20. Read Input From Command Line
21. Abstract Class In Java
22. Interface in Java
23. Cutom Marker Interface
24. Constructor In Java
25. Package in Java
26. Acess Modifier In Java
27. Static Import In Java
28. Package Class In Java
29. This Keyword
30. Instance Intializer Block
31. Super Keyword
32. Static Keyword
33. Final Keyword
Abstract Class in Java: Previous Next: Custom Marker Interface
Interface In Java
Interface in real world:
You can see number of interface examples. Let us take example of a TV. You press change channel button of TV remote and channel is changed. Here remote is act as an interface between you and TV.
Dictionary meaning of interface:
A point where two systems, subjects, organizations, etc., meet and interact.
Interface in java:
Interface is a way of implementing 100% abstraction. An interface is declared with interface keyword. It can contain only abstract methods and static final data members Or Interface is a group of related abstract methods.
Syntax:
Interface Interface_Name {
//abstract methods
//static final data members
}
|
An interface can’t be instantiated, it can be implemented by classes. To implement an interface implements keyword is used. Interface forms a contract with your class that force your class to have all methods defined by the interface must appear in the class. This all enforce check is done at compile time by compiler i.e. A class that implements an interface must implement all of the methods described in the interface.
Syntax:
class class_name implements Interface_Name {
//all methods of interface
//block of code for class
}
|
Note:
1. A class extends another class.
2. An interface extends another interface.
3. A class implements an interface.
Example:
Output:
Multiple inheritance in java.
- 1. A class can implements multiple interfaces.
- 2. An interface can extends multiple interfaces.
Example:
Output:
Why multiple inheritance is possible in case of interfaces but not possible in case of classes?
Multiple inheritance is possible in case of interfaces but not possible in case of classes because there is no ambiguity problem in case of interfaces as implementation is provided by class that implements interfaces not by interfaces itself.
Example:
Output:
An interface extends another interface Example:
Output:
Methods of an interface are public abstract by default.
Data members of an interface are public static final by default.
Interface is abstract by default that’s why no abstract keyword is used in interface declaration.
Marker/Tagging Interfaces:
An interface with no methods is known as marker or tagged interface.
Why marker interface used:
It provides some useful information to JVM/compiler so that JVM/compiler performs some special operations on it. It is used for better readability of code. Example: Serializable, Clonnable etc.
Syntax:
public interface Interface_Name {
}
|
Why an interface can’t have constructor?
Because interface not have any instance fields so nothing to construct. Now the question arise how interface can be inherited without constructor because subclass constructor call super class constructor. Now we have two cases. First case is when an interface extends other interface, in this case there is no issue because no interface have constructor and hence no super class constructor call. Second case when a class implements an interface, in this case there is no inheritance because class implements interface not extends interface.