- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
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
Accessing variables of two interfaces, which are same from an implementing class in java?
An interface in Java is similar to class but, it contains only abstract methods and fields which are final and static.
You can implement multiple interfaces using a single class in Java. Whenever both interfaces have same name, since all the fields of an interface are static by default, you can access them using the name of the interface as −
Example
interface MyInterface1{ public static int num = 100; public void display(); } interface MyInterface2{ public static int num = 1000; public void show(); } public class InterfaceExample implements MyInterface1, MyInterface2{ public static int num = 10000; public void display() { System.out.println("This is the implementation of the display method"); } public void show() { System.out.println("This is the implementation of the show method"); } public static void main(String args[]) { InterfaceExample obj = new InterfaceExample(); System.out.println("num field of the interface MyInterface1"+MyInterface1.num); System.out.println("num field of the interface MyInterface2"+MyInterface2.num); System.out.println("num field of the class InterfaceExample "+obj.num); } }
Output
num field of the interface MyInterface1 100 num field of the interface MyInterface2 1000 num field of the class InterfaceExample 10000
- Related Articles
- What are class variables, instance variables and local variables in Java?
- Accessing a Java class in other package.
- List the Interfaces That a Class Implements in Java
- What are the types of variables a class can have in Java?
- What are nested interfaces in Java?
- Declare static variables and methods in an abstract class in Java
- How to access variables from enclosing class using lambda in Java?
- Class and Static Variables in Java
- Accessing Kotlin extension functions from Java
- Why are interfaces introduced in Java?\n
- What are the SAM Interfaces in Java?
- What are the interfaces implemented by Array class in C#?
- Get class from an object in Java
- Accessing variables of Int and Float without initializing in C
- Evolution of interfaces in Java

Advertisements