Results 1 to 4 of 4

Thread: Help with java inheritance !!!

  1. #1
    Join Date
    May 2011
    Beans
    143
    Distro
    Ubuntu 12.04 Precise Pangolin

    Help with java inheritance !!!

    Code:
    class A { int a = 10; }
    class B extends A { int a = 20; }
    
    
    class C {
    	
    	public static void main(String[] args) {
    		A a = new B(); // why does this work ??
    		B b = new A(); // why does this not ??
    	}
    	
    }
    I just can't understand why

    BASE_CLASS <var_name> = new DERIVED_CLASS (will work)
    DERIVED_CLASS <var_name> = new BASE_CLASS (will not work)

    HELP !!!!!!!

  2. #2
    Join Date
    Sep 2009
    Location
    Freiburg/Germany
    Beans
    1,112
    Distro
    Ubuntu 12.10 Quantal Quetzal

    Re: Help with java inheritance !!!

    If b is a variable of Type B it needs to ysupport all the operations that type B supports. This is only guaranted if its value is an instance of B or one of its subclasses, so you'll get a compile time error if you try to assign a value to b that is not such an instance.

    It may be more obvious if you define B as

    Code:
    class B extends A { int b = 20; }
    Then if b is of type B it has to have the class variables a (inherited from A) and b.
    As doesn't have b if it is an instance of A it is not allowed to be such an instance.
    ClassicMenu Indicator - classic GNOME menu for Unity
    Unsettings - configuration program for the Unity
    Privacy Indicator - easily switch privacy settings in Unity
    Arronax - create and modify app starters

  3. #3
    Join Date
    Nov 2012
    Location
    Halloween Town
    Beans
    Hidden!
    Distro
    Xubuntu Development Release

    Re: Help with java inheritance !!!

    Java Inheritance defines an is-a relationship between a superclass and its subclasses. This means that an object of a subclass can be used wherever an object of the superclass can be used. The inheritance relationship is transitive: if class x extends class y, then a class z, which extends class x, will also inherit from class y.
    Please mark your thread as solved if you get a satisfactory response.

  4. #4
    Join Date
    Jul 2007
    Location
    Poland
    Beans
    4,301
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: Help with java inheritance !!!

    to visualize the problem better
    Code:
    class HasFourLegs {} // base class
    class Dog extends HasFourLegs { <dog stuff> }
    class Table extends HasFourLegs { <furniture stuff> }
    
    HasFourLegs a = new Dog(); // makes sense
    Dog b = new HasFourLegs(); // doesn't make sense, especially because it would also mean that Dog is compatible with Table
    in other words inheritance is a one way street. Every DerivedClass object is of BaseClass type at the same time, but not every object under the umbrella of BaseClass belongs to DerivedClass
    Last edited by Vaphell; March 18th, 2013 at 08:53 PM.
    if your question is answered, mark the thread as [SOLVED]. Thx.
    To post code or command output, use [code] tags.
    Check your bash script here // BashFAQ // BashPitfalls

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •