• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

Interface, abstract and concreate class relation with variable

 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
GoodDay EveryOne

Anyone who could help me with this doubt, I will greatly appreciate it. Please.


abstract class Test{ int i =10;}
interface inter{int i = 2;}
public class NewMain57 extends Test implements inter {
int i = 9;

public static void main(String[] args) {
inter n = new NewMain57();
System.out.println(n.i); -----> Prints 2;

Test n = new NewMain57();
System.out.println(n.i); -----> Prints 10;

NewMain57 n = new NewMain57();
System.out.println(n.i); -----> Prints 9;


}

}

Is this right(although i ran in netbeans , got these answers). Is there any way we can get the variable i of inter interface into NewMain57 class (other than this) as this class inherits the variable of inter interface which is public static and constant.
 
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mushraf Khan wrote:GoodDay EveryOne

Anyone who could help me with this doubt, I will greatly appreciate it. Please.


abstract class Test{ int i =10;}
interface inter{int i = 2;}
public class NewMain57 extends Test implements inter {
int i = 9;

public static void main(String[] args) {
inter n = new NewMain57();
System.out.println(n.i); -----> Prints 2;

Test n = new NewMain57();
System.out.println(n.i); -----> Prints 10;

NewMain57 n = new NewMain57();
System.out.println(n.i); -----> Prints 9;


}

}

Is this right(although i ran in netbeans , got these answers). Is there any way we can get the variable i of inter interface into NewMain57 class (other than this) as this class inherits the variable of inter interface which is public static and constant.




hello Mushraf Khan,

well first frankly i didn't get your Q first(about "Is this...) ... but then i run your program and from your Q may be you want, with using of NewMain()'s object you want to access inter's variable(i=2) right??

so about answer i did try and found that we cant access using NewMain 's object cause it will access only local variables(i=9) .. so we can directly call interface's variable as it is "public static variable"

so answer is System.out.println(inter.i);//answer =2

otherwise create another variable

interface inter{int i = 2;
int j=1;}

and now you can access using NewMain's object as

NewMain nw = new NewMain();
System.out.println(nw.j);//print 1

(i hope you got answer )

Thanking you.
 
Ranch Hand
Posts: 58
Eclipse IDE C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Member variables, if redefined in the inheritance tree, do not exhibit run-time polymorphism. It is the type of the reference variable(and not the object it refers to), that decides which one would be used and this is decided at compile time. That justifies your results.
As Amey mentioned, interface member variables are "public static" and can always be accessed as "inter.i". You may use casting on the NewMain57 object as well to get the required access:
 
Mushraf Khan
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot. Yes, Amey , my question was to access interface variable from main. Its clear to me now. Thanks Sebanti also for the explaination
 
Amey Ambulgekar
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mushraf Khan wrote:Thanks a lot. Yes, Amey , my question was to access interface variable from main. Its clear to me now. Thanks Sebanti also for the explaination



Welcome Khan

and thanking you Sebanti for best explanation
 
You don't know me, but I've been looking all over the world for. Thanks to the help from this tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic