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
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;








1















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.










share|improve this question
























  • You can't access someData1() in either case.

    – Nicholas K
    Mar 22 at 18:26

















1















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.










share|improve this question
























  • You can't access someData1() in either case.

    – Nicholas K
    Mar 22 at 18:26













1












1








1








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.










share|improve this question
















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 22 at 18:28







Hasnain Ali

















asked Mar 22 at 18:21









Hasnain AliHasnain Ali

404




404












  • 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
















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












1 Answer
1






active

oldest

votes


















0














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 :)






share|improve this answer

























    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
    );



    );













    draft saved

    draft discarded


















    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









    0














    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 :)






    share|improve this answer





























      0














      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 :)






      share|improve this answer



























        0












        0








        0







        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 :)






        share|improve this answer















        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 :)







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Mar 22 at 18:59

























        answered Mar 22 at 18:49









        johnjohn

        1,63152028




        1,63152028





























            draft saved

            draft discarded
















































            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.




            draft saved


            draft discarded














            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





















































            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
            LlkpnVjK kB1d9,jivkx7bt5OPlWXmEFc lR

            Popular posts from this blog

            Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

            SQL error code 1064 with creating Laravel foreign keysForeign key constraints: When to use ON UPDATE and ON DELETEDropping column with foreign key Laravel error: General error: 1025 Error on renameLaravel SQL Can't create tableLaravel Migration foreign key errorLaravel php artisan migrate:refresh giving a syntax errorSQLSTATE[42S01]: Base table or view already exists or Base table or view already exists: 1050 Tableerror in migrating laravel file to xampp serverSyntax error or access violation: 1064:syntax to use near 'unsigned not null, modelName varchar(191) not null, title varchar(191) not nLaravel cannot create new table field in mysqlLaravel 5.7:Last migration creates table but is not registered in the migration table

            은진 송씨 목차 역사 본관 분파 인물 조선 왕실과의 인척 관계 집성촌 항렬자 인구 같이 보기 각주 둘러보기 메뉴은진 송씨세종실록 149권, 지리지 충청도 공주목 은진현