Inherit Properties From Another Class Using Interface ReferenceHow do I call one constructor from another in Java?Efficiency of Java “Double Brace Initialization”?MOQ - how to mock an interface that needs to be cast to another interface?Python class inherits objectC++ interfaces and inheritanceReferencing through super class/interface reference - JavaHow should I have explained the difference between an Interface and an Abstract class?Why not inherit from List<T>?Do I really have a car in my garage?Java: Implementing an interfacing by inheriting a class

Multi tool use
Why was Germany not as successful as other Europeans in establishing overseas colonies?
How to have a sharp product image?
Controversial area of mathematics
What was the first Intel x86 processor with "Base + Index * Scale + Displacement" addressing mode?
Examples of non trivial equivalence relations , I mean equivalence relations without the expression " same ... as" in their definition?
Was there a shared-world project before "Thieves World"?
Document starts having heaps of errors in the middle, but the code doesn't have any problems in it
Combinable filters
Why do games have consumables?
How can I practically buy stocks?
How to creep the reader out with what seems like a normal person?
Does the sign matter for proportionality?
How to reduce LED flash rate (frequency)
French for 'It must be my imagination'?
how to find the equation of a circle given points of the circle
Why does nature favour the Laplacian?
Fizzy, soft, pop and still drinks
Is there any limitation with Arduino Nano serial communication distance?
Does Gita support doctrine of eternal cycle of birth and death for evil people?
How to verbalise code in Mathematica?
How could Tony Stark make this in Endgame?
How can Republicans who favour free markets, consistently express anger when they don't like the outcome of that choice?
How exactly does Hawking radiation decrease the mass of black holes?
A Note on N!
Inherit Properties From Another Class Using Interface Reference
How do I call one constructor from another in Java?Efficiency of Java “Double Brace Initialization”?MOQ - how to mock an interface that needs to be cast to another interface?Python class inherits objectC++ interfaces and inheritanceReferencing through super class/interface reference - JavaHow should I have explained the difference between an Interface and an Abstract class?Why not inherit from List<T>?Do I really have a car in my garage?Java: Implementing an interfacing by inheriting a class
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
I'm not sure how exactly to phrase my question.
So, I have an interface reference and I'm creating a new object. The new object obviously implements said interface. The initial class inherits another class. That sub-class inherits the super class. However, I cannot access data from super class from the main method without casting the reference first. I'll show an example below
public class a
public int getSomeData1()
return someData;
public class b extends a implements someInterface
// Some behavior.
public class c extends b implements someInterface
// Some behavior.
public class Main
public static void main(String[] args)
someInterface obj = new b();
obj.someData1(); // I cannot access someData1().
c anotherObj = new c();
c.getSomeData1(); // This works however.
How can I have obj.someData1()
actually get the data from class a rather than casting it to a
.
java inheritance
add a comment |
I'm not sure how exactly to phrase my question.
So, I have an interface reference and I'm creating a new object. The new object obviously implements said interface. The initial class inherits another class. That sub-class inherits the super class. However, I cannot access data from super class from the main method without casting the reference first. I'll show an example below
public class a
public int getSomeData1()
return someData;
public class b extends a implements someInterface
// Some behavior.
public class c extends b implements someInterface
// Some behavior.
public class Main
public static void main(String[] args)
someInterface obj = new b();
obj.someData1(); // I cannot access someData1().
c anotherObj = new c();
c.getSomeData1(); // This works however.
How can I have obj.someData1()
actually get the data from class a rather than casting it to a
.
java inheritance
You can't accesssomeData1()
in either case.
– Nicholas K
Mar 22 at 18:26
add a comment |
I'm not sure how exactly to phrase my question.
So, I have an interface reference and I'm creating a new object. The new object obviously implements said interface. The initial class inherits another class. That sub-class inherits the super class. However, I cannot access data from super class from the main method without casting the reference first. I'll show an example below
public class a
public int getSomeData1()
return someData;
public class b extends a implements someInterface
// Some behavior.
public class c extends b implements someInterface
// Some behavior.
public class Main
public static void main(String[] args)
someInterface obj = new b();
obj.someData1(); // I cannot access someData1().
c anotherObj = new c();
c.getSomeData1(); // This works however.
How can I have obj.someData1()
actually get the data from class a rather than casting it to a
.
java inheritance
I'm not sure how exactly to phrase my question.
So, I have an interface reference and I'm creating a new object. The new object obviously implements said interface. The initial class inherits another class. That sub-class inherits the super class. However, I cannot access data from super class from the main method without casting the reference first. I'll show an example below
public class a
public int getSomeData1()
return someData;
public class b extends a implements someInterface
// Some behavior.
public class c extends b implements someInterface
// Some behavior.
public class Main
public static void main(String[] args)
someInterface obj = new b();
obj.someData1(); // I cannot access someData1().
c anotherObj = new c();
c.getSomeData1(); // This works however.
How can I have obj.someData1()
actually get the data from class a rather than casting it to a
.
java inheritance
java inheritance
edited Mar 22 at 18:28
Hasnain Ali
asked Mar 22 at 18:21
Hasnain AliHasnain Ali
404
404
You can't accesssomeData1()
in either case.
– Nicholas K
Mar 22 at 18:26
add a comment |
You can't accesssomeData1()
in either case.
– Nicholas K
Mar 22 at 18:26
You can't access
someData1()
in either case.– Nicholas K
Mar 22 at 18:26
You can't access
someData1()
in either case.– Nicholas K
Mar 22 at 18:26
add a comment |
1 Answer
1
active
oldest
votes
Just remember the rule that method invocations allowed by the compiler are based solely on the declared type of the reference, regardless of the object type.
If it is not very clear, here is another version of this rule: what is on the left side defines methods you can call, no matter what is on the right :)
Here are a few examples to make it more clear:
public interface Animal
void voice();
public class Dog implements Animal
public void voice()
System.out.println("bark bark");
public void run()
// impl
When you create a dog like this:
Animal dog1 = new Dog();
The reference type which is Animal
defines which methods are allowed for you to call. So basically you can only call:
dog1.voice();
When you create a dog like this:
Dog dog2 = new Dog();
The reference type which is Dog
, so you are allowed to call:
dog2.voice();
dog2.run();
This rule remains also when you have class inheritance, not only when you implement an interface. Let's say we have something like:
public class SpecialDog extends Dog
public void superPower()
And those are examples of what you can call:
Animal dog1 = new SpecialDog();
dog1.voice(); // only this
Dog dog2 = new SpecialDog();
// here you can call everything that Dog contains
dog2.voice();
dog2.run();
SpecialDog dog3 = new SpecialDog();
// here you can call all 3 methods
// this is the SpecialDog method
dog3.superPower();
// those 2 are inherited from Dog, so SpecialDog also has them
dog3.voice();
dog3.run();
In other cases, you need to upcast/downcast to be able to call some specific method.
Happy Hacking :)
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55305700%2finherit-properties-from-another-class-using-interface-reference%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Just remember the rule that method invocations allowed by the compiler are based solely on the declared type of the reference, regardless of the object type.
If it is not very clear, here is another version of this rule: what is on the left side defines methods you can call, no matter what is on the right :)
Here are a few examples to make it more clear:
public interface Animal
void voice();
public class Dog implements Animal
public void voice()
System.out.println("bark bark");
public void run()
// impl
When you create a dog like this:
Animal dog1 = new Dog();
The reference type which is Animal
defines which methods are allowed for you to call. So basically you can only call:
dog1.voice();
When you create a dog like this:
Dog dog2 = new Dog();
The reference type which is Dog
, so you are allowed to call:
dog2.voice();
dog2.run();
This rule remains also when you have class inheritance, not only when you implement an interface. Let's say we have something like:
public class SpecialDog extends Dog
public void superPower()
And those are examples of what you can call:
Animal dog1 = new SpecialDog();
dog1.voice(); // only this
Dog dog2 = new SpecialDog();
// here you can call everything that Dog contains
dog2.voice();
dog2.run();
SpecialDog dog3 = new SpecialDog();
// here you can call all 3 methods
// this is the SpecialDog method
dog3.superPower();
// those 2 are inherited from Dog, so SpecialDog also has them
dog3.voice();
dog3.run();
In other cases, you need to upcast/downcast to be able to call some specific method.
Happy Hacking :)
add a comment |
Just remember the rule that method invocations allowed by the compiler are based solely on the declared type of the reference, regardless of the object type.
If it is not very clear, here is another version of this rule: what is on the left side defines methods you can call, no matter what is on the right :)
Here are a few examples to make it more clear:
public interface Animal
void voice();
public class Dog implements Animal
public void voice()
System.out.println("bark bark");
public void run()
// impl
When you create a dog like this:
Animal dog1 = new Dog();
The reference type which is Animal
defines which methods are allowed for you to call. So basically you can only call:
dog1.voice();
When you create a dog like this:
Dog dog2 = new Dog();
The reference type which is Dog
, so you are allowed to call:
dog2.voice();
dog2.run();
This rule remains also when you have class inheritance, not only when you implement an interface. Let's say we have something like:
public class SpecialDog extends Dog
public void superPower()
And those are examples of what you can call:
Animal dog1 = new SpecialDog();
dog1.voice(); // only this
Dog dog2 = new SpecialDog();
// here you can call everything that Dog contains
dog2.voice();
dog2.run();
SpecialDog dog3 = new SpecialDog();
// here you can call all 3 methods
// this is the SpecialDog method
dog3.superPower();
// those 2 are inherited from Dog, so SpecialDog also has them
dog3.voice();
dog3.run();
In other cases, you need to upcast/downcast to be able to call some specific method.
Happy Hacking :)
add a comment |
Just remember the rule that method invocations allowed by the compiler are based solely on the declared type of the reference, regardless of the object type.
If it is not very clear, here is another version of this rule: what is on the left side defines methods you can call, no matter what is on the right :)
Here are a few examples to make it more clear:
public interface Animal
void voice();
public class Dog implements Animal
public void voice()
System.out.println("bark bark");
public void run()
// impl
When you create a dog like this:
Animal dog1 = new Dog();
The reference type which is Animal
defines which methods are allowed for you to call. So basically you can only call:
dog1.voice();
When you create a dog like this:
Dog dog2 = new Dog();
The reference type which is Dog
, so you are allowed to call:
dog2.voice();
dog2.run();
This rule remains also when you have class inheritance, not only when you implement an interface. Let's say we have something like:
public class SpecialDog extends Dog
public void superPower()
And those are examples of what you can call:
Animal dog1 = new SpecialDog();
dog1.voice(); // only this
Dog dog2 = new SpecialDog();
// here you can call everything that Dog contains
dog2.voice();
dog2.run();
SpecialDog dog3 = new SpecialDog();
// here you can call all 3 methods
// this is the SpecialDog method
dog3.superPower();
// those 2 are inherited from Dog, so SpecialDog also has them
dog3.voice();
dog3.run();
In other cases, you need to upcast/downcast to be able to call some specific method.
Happy Hacking :)
Just remember the rule that method invocations allowed by the compiler are based solely on the declared type of the reference, regardless of the object type.
If it is not very clear, here is another version of this rule: what is on the left side defines methods you can call, no matter what is on the right :)
Here are a few examples to make it more clear:
public interface Animal
void voice();
public class Dog implements Animal
public void voice()
System.out.println("bark bark");
public void run()
// impl
When you create a dog like this:
Animal dog1 = new Dog();
The reference type which is Animal
defines which methods are allowed for you to call. So basically you can only call:
dog1.voice();
When you create a dog like this:
Dog dog2 = new Dog();
The reference type which is Dog
, so you are allowed to call:
dog2.voice();
dog2.run();
This rule remains also when you have class inheritance, not only when you implement an interface. Let's say we have something like:
public class SpecialDog extends Dog
public void superPower()
And those are examples of what you can call:
Animal dog1 = new SpecialDog();
dog1.voice(); // only this
Dog dog2 = new SpecialDog();
// here you can call everything that Dog contains
dog2.voice();
dog2.run();
SpecialDog dog3 = new SpecialDog();
// here you can call all 3 methods
// this is the SpecialDog method
dog3.superPower();
// those 2 are inherited from Dog, so SpecialDog also has them
dog3.voice();
dog3.run();
In other cases, you need to upcast/downcast to be able to call some specific method.
Happy Hacking :)
edited Mar 22 at 18:59
answered Mar 22 at 18:49


johnjohn
1,63152028
1,63152028
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55305700%2finherit-properties-from-another-class-using-interface-reference%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
j,fsRMmu2xw0KW brnVJjn4XhJEvmgc,gD1Gd01V0S9zdSxya 1W,hpdj,x8,wHGo FfehksZ7
You can't access
someData1()
in either case.– Nicholas K
Mar 22 at 18:26