in Education by
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. JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
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 :)

Related questions

0 votes
    Which of these keywords is used by a class to use an interface defined previously? (a) import (b) Import (c ... & Packages of Java Select the correct answer from above options...
asked Feb 23, 2022 in Education by JackTerrance
0 votes
    Which of the following is the correct way of implementing an interface salary by class manager? (a) class manager ... Packages of Java Select the correct answer from above options...
asked Feb 23, 2022 in Education by JackTerrance
0 votes
    Which of the following is the correct way of implementing an interface A by class B? (a) class B extends A{ ... & Packages of Java Select the correct answer from above options...
asked Feb 23, 2022 in Education by JackTerrance
0 votes
    Which of these methods can be used to get reference to a component that was removed from a container? (a) ... Event Handling of Java Select the correct answer from above options...
asked Feb 24, 2022 in Education by JackTerrance
0 votes
    Which class provides an interface for invoking JavaScript methods and examining JavaScript properties? (a) ... for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Oct 21, 2021 in Education by JackTerrance
0 votes
    If we need to inherit from a base class and want to pass something from the constructor of the inherited class to the constructor of the base class, how can we do that?...
asked Jan 16, 2021 in Technology by JackTerrance
0 votes
    Which of these methods is used to retrieve the elements in properties object at specific location? (a) get() ... Framework of Java Select the correct answer from above options...
asked Mar 1, 2022 in Education by JackTerrance
0 votes
    What is synchronization in reference to a thread? (a) It's a process of handling situations when two or ... Multithreading of Java Select the correct answer from above options...
asked Mar 1, 2022 in Education by JackTerrance
0 votes
    Which of these methods can be used to obtain the reference to the container that generated a ContainerEvent? (a) ... Handling of Java Select the correct answer from above options...
asked Feb 24, 2022 in Education by JackTerrance
0 votes
    Earlier I used to think that Java is Pass by Reference, but During my latest research iI came to the conclusion that ... and why is it Select the correct answer from above options...
asked Jan 21, 2022 in Education by JackTerrance
0 votes
    I have two modules. App and module Test. Module app contains MainActivity with button. Module Test contains ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 30, 2022 in Education by JackTerrance
0 votes
    How can we move from one desired step to another step? (a) breakpoints (b) System.out.println (c) logger. ... & Servlet of Java Select the correct answer from above options...
asked Feb 22, 2022 in Education by JackTerrance
0 votes
    Which component can be used for sending messages from one application to another? (a) server (b) client (c) mq ... & Servlet of Java Select the correct answer from above options...
asked Feb 22, 2022 in Education by JackTerrance
0 votes
    Since XmlSerializer can not serialize any other properties when the class is inherited from List , I try to ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 3, 2022 in Education by JackTerrance
0 votes
    Which of this keyword must be used to inherit a class? (a) super (b) this (c) extent (d) ... java programming questions and answers pdf, java interview questions for beginners...
asked Oct 25, 2021 in Education by JackTerrance
...