Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Java
  4. problem with inheritance

problem with inheritance

Scheduled Pinned Locked Moved Java
oophelpquestion
10 Posts 8 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Asaf Shay
    wrote on last edited by
    #1

    package exam804;
    class Base
    {
    public static void foo(Base bObj)
    {
    System.out.println("In Base.foo()");
    bObj.bar();
    }

    public void bar()
    {
    	System.out.println("In Base.bar()");
    }
    

    }

    class Derived extends Base
    {
    public static void foo(Base bObj)
    {
    System.out.println("In Derived.foo()");
    bObj.bar();
    }

    public void bar()
    {
    	System.out.println("In Derived.bar()");
    }
    

    }

    class OverrideTest
    {
    public static void main(String[] args)
    {
    Base bObj = new Derived();
    bObj.foo(bObj);
    }
    }

    why the output is "In Base.foo() In Derived.bar()" and not "In Derived.foo() In Derived.bar()" ?

    L L M U A 6 Replies Last reply
    0
    • A Asaf Shay

      package exam804;
      class Base
      {
      public static void foo(Base bObj)
      {
      System.out.println("In Base.foo()");
      bObj.bar();
      }

      public void bar()
      {
      	System.out.println("In Base.bar()");
      }
      

      }

      class Derived extends Base
      {
      public static void foo(Base bObj)
      {
      System.out.println("In Derived.foo()");
      bObj.bar();
      }

      public void bar()
      {
      	System.out.println("In Derived.bar()");
      }
      

      }

      class OverrideTest
      {
      public static void main(String[] args)
      {
      Base bObj = new Derived();
      bObj.foo(bObj);
      }
      }

      why the output is "In Base.foo() In Derived.bar()" and not "In Derived.foo() In Derived.bar()" ?

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      Why is foo declared as static?

      Veni, vidi, abiit domum

      A 1 Reply Last reply
      0
      • A Asaf Shay

        package exam804;
        class Base
        {
        public static void foo(Base bObj)
        {
        System.out.println("In Base.foo()");
        bObj.bar();
        }

        public void bar()
        {
        	System.out.println("In Base.bar()");
        }
        

        }

        class Derived extends Base
        {
        public static void foo(Base bObj)
        {
        System.out.println("In Derived.foo()");
        bObj.bar();
        }

        public void bar()
        {
        	System.out.println("In Derived.bar()");
        }
        

        }

        class OverrideTest
        {
        public static void main(String[] args)
        {
        Base bObj = new Derived();
        bObj.foo(bObj);
        }
        }

        why the output is "In Base.foo() In Derived.bar()" and not "In Derived.foo() In Derived.bar()" ?

        L Offline
        L Offline
        linaast
        wrote on last edited by
        #3

        You do not need to create the instance into base class. If you have base class, leave it, every class, which extends the base class as father class makes instances to it automatically. All you need is to put "super" modifier, if u need to call any function from your base class. I suggest you to do like this in this case:

        public class OverrideTest{
        public static void main(String[] args){
        Derived dObj = new Derived();
        dObj.foo();
        }
        }

        public class Derived extends Base{
        public void foo(){
        System.out.println("In Derived.foo()");
        super.foo();
        }
        }

        public class Base{
        public void foo(){
        System.out.println("In Base.foo()");
        }
        }

        Now your output will be: "In Derived.foo()" "In Base.foo()" You do not need to use this:

        Base bObj = new Derived();

        If You want to call the foo() method of Base class directly from yor Main class, remove the foo() method from your Derived class. In this case, the foo() method will be called from Base class automatically.

        A 1 Reply Last reply
        0
        • L Lost User

          Why is foo declared as static?

          Veni, vidi, abiit domum

          A Offline
          A Offline
          Asaf Shay
          wrote on last edited by
          #4

          i don't know .i take this question from exam

          J 1 Reply Last reply
          0
          • L linaast

            You do not need to create the instance into base class. If you have base class, leave it, every class, which extends the base class as father class makes instances to it automatically. All you need is to put "super" modifier, if u need to call any function from your base class. I suggest you to do like this in this case:

            public class OverrideTest{
            public static void main(String[] args){
            Derived dObj = new Derived();
            dObj.foo();
            }
            }

            public class Derived extends Base{
            public void foo(){
            System.out.println("In Derived.foo()");
            super.foo();
            }
            }

            public class Base{
            public void foo(){
            System.out.println("In Base.foo()");
            }
            }

            Now your output will be: "In Derived.foo()" "In Base.foo()" You do not need to use this:

            Base bObj = new Derived();

            If You want to call the foo() method of Base class directly from yor Main class, remove the foo() method from your Derived class. In this case, the foo() method will be called from Base class automatically.

            A Offline
            A Offline
            Asaf Shay
            wrote on last edited by
            #5

            this question is from exam .so i don't want to make the code better or different . i only want to know what i ask .

            1 Reply Last reply
            0
            • A Asaf Shay

              i don't know .i take this question from exam

              J Offline
              J Offline
              jschell
              wrote on last edited by
              #6

              Asaf Shay wrote:

              i don't know .i take this question from exam

              However that is the clue as to the answer to the question.

              1 Reply Last reply
              0
              • A Asaf Shay

                package exam804;
                class Base
                {
                public static void foo(Base bObj)
                {
                System.out.println("In Base.foo()");
                bObj.bar();
                }

                public void bar()
                {
                	System.out.println("In Base.bar()");
                }
                

                }

                class Derived extends Base
                {
                public static void foo(Base bObj)
                {
                System.out.println("In Derived.foo()");
                bObj.bar();
                }

                public void bar()
                {
                	System.out.println("In Derived.bar()");
                }
                

                }

                class OverrideTest
                {
                public static void main(String[] args)
                {
                Base bObj = new Derived();
                bObj.foo(bObj);
                }
                }

                why the output is "In Base.foo() In Derived.bar()" and not "In Derived.foo() In Derived.bar()" ?

                M Offline
                M Offline
                Mr_z
                wrote on last edited by
                #7

                שלום אסף -- אלה התיקונים שצריך להכניס כדי שהתוצאה תראה כפי שרצית 1] Derived bObj = new Derived() ; 2] class Derived extends Base public static void foo(Derived bObj) אני חושב שהתוצאה הלא נכונה נגרמת בגלל "דריסה" לא נכונה של המחלקה היורשת

                1 Reply Last reply
                0
                • A Asaf Shay

                  package exam804;
                  class Base
                  {
                  public static void foo(Base bObj)
                  {
                  System.out.println("In Base.foo()");
                  bObj.bar();
                  }

                  public void bar()
                  {
                  	System.out.println("In Base.bar()");
                  }
                  

                  }

                  class Derived extends Base
                  {
                  public static void foo(Base bObj)
                  {
                  System.out.println("In Derived.foo()");
                  bObj.bar();
                  }

                  public void bar()
                  {
                  	System.out.println("In Derived.bar()");
                  }
                  

                  }

                  class OverrideTest
                  {
                  public static void main(String[] args)
                  {
                  Base bObj = new Derived();
                  bObj.foo(bObj);
                  }
                  }

                  why the output is "In Base.foo() In Derived.bar()" and not "In Derived.foo() In Derived.bar()" ?

                  U Offline
                  U Offline
                  Usama Moud
                  wrote on last edited by
                  #8

                  foo() in Base class is declared as Static. Therefore it is not belonged to specific object but common for all objects. bObj is an object of Base class, therefore it will call foo() in Base class, because foo() is Static.

                  1 Reply Last reply
                  0
                  • A Asaf Shay

                    package exam804;
                    class Base
                    {
                    public static void foo(Base bObj)
                    {
                    System.out.println("In Base.foo()");
                    bObj.bar();
                    }

                    public void bar()
                    {
                    	System.out.println("In Base.bar()");
                    }
                    

                    }

                    class Derived extends Base
                    {
                    public static void foo(Base bObj)
                    {
                    System.out.println("In Derived.foo()");
                    bObj.bar();
                    }

                    public void bar()
                    {
                    	System.out.println("In Derived.bar()");
                    }
                    

                    }

                    class OverrideTest
                    {
                    public static void main(String[] args)
                    {
                    Base bObj = new Derived();
                    bObj.foo(bObj);
                    }
                    }

                    why the output is "In Base.foo() In Derived.bar()" and not "In Derived.foo() In Derived.bar()" ?

                    A Offline
                    A Offline
                    angrybobcat
                    wrote on last edited by
                    #9

                    To explain that, take a look at what is executed and why. First of all, you create a new Derived-Instance as an instance of Base. Take a look at how you call the static method foo(): You dont access it in a static way (to use exact the phrase the compiler will warn you with) - which would be Base.foo() or Derived.foo() - but you call it on an instance. As mentioned, bObj is an instance of Base (because you declared it as that) and thus the static Method Base.foo() is called, which prints the "In Base.foo()"-Part of your output. If you declared it as an instance of Derived, the static method Derived.foo() would have been called there. To explain the part "In Derived.bar()" take a look at what is called next: In Base.bar() you call bObj.bar(); bObj was instantiated with new Derived(), so the method bar() on that object is overriden with the implementation in Derived, which prints then "In Derived.bar()". I hope that explains inheritance a bit better. ab

                    1 Reply Last reply
                    0
                    • A Asaf Shay

                      package exam804;
                      class Base
                      {
                      public static void foo(Base bObj)
                      {
                      System.out.println("In Base.foo()");
                      bObj.bar();
                      }

                      public void bar()
                      {
                      	System.out.println("In Base.bar()");
                      }
                      

                      }

                      class Derived extends Base
                      {
                      public static void foo(Base bObj)
                      {
                      System.out.println("In Derived.foo()");
                      bObj.bar();
                      }

                      public void bar()
                      {
                      	System.out.println("In Derived.bar()");
                      }
                      

                      }

                      class OverrideTest
                      {
                      public static void main(String[] args)
                      {
                      Base bObj = new Derived();
                      bObj.foo(bObj);
                      }
                      }

                      why the output is "In Base.foo() In Derived.bar()" and not "In Derived.foo() In Derived.bar()" ?

                      V Offline
                      V Offline
                      Vivek Vermani
                      wrote on last edited by
                      #10

                      Java doesn't allow overriding static methods and runtime polymorphism on static methods. Why Java Doesn't allow run time Polymorphism by overriding static methods.

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups