problem with inheritance
-
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()" ?
-
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()" ?
-
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()" ?
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.
-
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.
-
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()" ?
-
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()" ?
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.
-
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()" ?
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
-
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()" ?
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.