Object Oriented Programming in java
Declare an interface called “MyFirstInterface”. Declare integer type variable called “x”. Declare an abstract method called “display()”.
- Try to declare the variable with/without public, static and final keywords. Is there any difference between these two approaches? Why?
- Declare the abstract method with/without abstract keyword. Is there any difference between these two approaches? Why?
- Implement this into a class called “IntefaceImplemented” . Override all the abstract methods. Try to change the value of x inside this method and print the value of x. Is it possible for you to change x? why?
Tagged:
Answers
How to declare an interface in Java?
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.
Syntax:
Abstract method in interface
All the methods of an interface are public abstract by default. You cannot have concrete (regular methods with body) methods in an interface.
Output: