- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to hide unsupported interface methods from class in Java?
Actually you can’t, once you implement an interface it is a must to provide implementation to all its methods or, make the class abstract. There is no way to skip methods of an interface without implementing (unless they are default methods). Still, if you try to skip implementing methods of an interface a compile-time error would be generated.
Example
interface MyInterface{ public static int num = 100; public void sample(); public void getDetails(); public void setNumber(int num); public void setString(String data); } public class InterfaceExample implements MyInterface{ public static int num = 10000; public void sample() { System.out.println("This is the implementation of the sample method"); } public static void main(String args[]) { InterfaceExample obj = new InterfaceExample(); obj.sample(); } }
Output
Compile-time error
InterfaceExample.java:8: error: InterfaceExample is not abstract and does not override abstract method setString(String) in MyInterface public class InterfaceExample implements MyInterface{ ^ 1 error
But, you can implement these unwanted/unsupported methods and throw an exception such as UnsupportedOperationException or, IllegalStateException from them.
Example
interface MyInterface{ public void sample(); public void getDetails(); public void setNumber(int num); public void setString(String data); } public class InterfaceExample implements MyInterface{ public void getDetails() { try { throw new UnsupportedOperationException(); } catch(UnsupportedOperationException ex) { System.out.println("Method not supported"); } } public void setNumber(int num) { try { throw new UnsupportedOperationException(); } catch(UnsupportedOperationException ex) { System.out.println("Method not supported"); } } public void setString(String data) { try { throw new UnsupportedOperationException(); } catch(UnsupportedOperationException ex) { System.out.println("Method not supported"); } } public void sample() { System.out.println("This is the implementation of the sample method"); } public static void main(String args[]) { InterfaceExample obj = new InterfaceExample(); obj.sample(); obj.getDetails(); obj.setNumber(21); obj.setString("data"); } }
Output
This is the implementation of the sample method Method not supported Method not supported Method not supported
- Related Articles
- Java Interface methods
- How to override only few methods of interface in Java?
- How to access the private methods of a class from outside of the class in Java?
- Must we implement all the methods in a class that implements an interface in Java?
- How to write a class inside an interface in Java?
- How to write/declare an interface inside a class in Java?
- Can we change the access specifier from (public) while implementing methods from interface in Java?
- BitSet class methods in Java
- Differences between Interface and class in Java
- Difference Between Class and Interface in Java
- How to implement an interface using an anonymous inner class in Java?
- What happens if a class does not implement all the abstract methods of an interface in java?
- Can we overload methods of an interface in Java?
- Math class methods in Java Programming
- Methods of StringBuffer class in Java.

Advertisements