
- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Java Interface methods
The methods in interface are abstract by default. This means methods in an interface will have only the method signature and no contents inside. Let us see an example −
Example
interface Car{ public void carSpeed(); public void sleep(); } class Porsche implements Car{ public void carSpeed(){ System.out.println("The speed of the Porsche is too much"); } public void sleep(){ System.out.println("Sleeping for few milliseconds"); } } public class Demo{ public static void main(String[] args){ Porsche my_car = new Porsche(); my_car.carSpeed(); my_car.sleep(); } }
Output
The speed of the Porsche is too much Sleeping for few milliseconds
An interface named ‘Car’ is defined with two functions named ‘carSpeed’ and ‘sleep’. Npw, this interface is implemented by a class named ‘Porsche’. This class defines the ‘carSpeed’ and ‘sleep’ whereas the interface had just defined them and didn’t have a body. Now, a class named Demo contains the main function that creates an instance of the Porsche class. This instance is called on ‘carSpeed’ and ‘sleep’ functions.
- Related Questions & Answers
- Can we overload methods of an interface in Java?
- Can we write an interface without any methods in java?
- How to hide unsupported interface methods from class in Java?
- How to override only few methods of interface in Java?
- Can we use private methods in an interface in Java 9?
- What are the modifiers allowed for methods in an Interface in java?
- Can the abstract methods of an interface throw an exception in java?
- What happens if we overload default methods of an interface in java?
- What kind of variables/methods defined in an interface in Java 9?
- Why do we need private methods in an interface in Java 9?
- Is it mandatory to override the default methods of an interface in Java?
- What are the rules for private methods in an interface in Java 9?
- What are the advantages of private methods in an interface in Java 9?
- Interface in Java
- Must we implement all the methods in a class that implements an interface in Java?
Advertisements