1

I've following class

class MyClass implements Intrfc {

String pickmeUp = "Its Me";

public static void main(String[] args){

Intrfc ob = new MyClass();
ob.pickmeUp;  ---> How can I access this way ?

  }

}

Is there any way to access class variable using Interface type ?

2
  • 3
    Unrelated: always use meaningful names, even in examples. And names that you can pronounce. Intrfc violates both of those properties ... so just call it MyInterface, or MyExampleInterface. Commented Apr 17, 2019 at 14:04
  • Assuming you're happy to use encapsulation and have control over the interface... Read access: declare a getter in the interface, implement it in the class by returning your field. Write access: declare a setter in the interface, implement it in the class by setting your field. Use the setter/getter to access/change the value of [your field / whatever] when referencing an object implementing that interface. Commented Apr 17, 2019 at 14:06

4 Answers 4

5

Is there any way to access class variable using Interface type ?

No. That is the whole point of an interface.

And yes, interfaces only give you behavior (methods), not "state" (variables/fields). That is how things are in Java.

Of course, you can always use instanceof to check if the actual object is of some more specific type, to then cast to that type. But as said, that defeats the purpose of using interfaces!

Sign up to request clarification or add additional context in comments.

Comments

4

No, you can't access the class variable using interface type, but the interface can define method that can access to the variable.

interface Intrfc {

    public String getPickmeUp();

}


class MyClass implements Intrfc {

    String pickmeUp = "Its Me";

    public String getPickmeUp(){
        return pickmeUp;
    }

    public static void main(String[] args){

        Intrfc ob = new MyClass();
        ob.getPickmeUp();

    }

}

Comments

2

In this definition:

class MyClass implements Intrfc {
    String pickmeUp = "Its Me";
}

the field pickmeUp is not even a member of Intrfc interface, so there is no possibility to reach for it using just the interface. pickmeUp is a member of a concrete class - MyClass.

Comments

2

If you want to use the method of a class using the object of an interface you can do it as follows:

//Interface:
public interface TestOne {  
    int a = 5;
    void test();    
    static void testOne(){      
        System.out.println("Great!!!");

    }

    default void testTwo(){

        System.out.println("Wow!!!");

    }
}
//-------------------
//Class implementing it:
package SP;
public class TestInterfacesImp implements Test1, TestOne{   
    @Override
    public void test() {
        System.out.println("I Love java");      
    }

        public void testM() {
            System.out.println("I Love java too");
        }

    public static void main(String[] args) {
        TestOne.testOne();      
        TestOne obj = new TestInterfacesImp();
        obj.testTwo();
        TestInterfacesImp objImp = new TestInterfacesImp();
        objImp.test();
        ((TestInterfacesImp) obj).testM(); //Here casting is done we have casted to class type


    }
}

Hope this helps...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.