-1

abstract class Parent { int i = 9;

void display() {
    System.out.println("base");
}

}

class Child extends Parent { int i = 8;

public void display() {

    System.out.println("derived");
}

}

public class A {

public static void main(String args[]) {
    Parent a1 = new Child();
    a1.display(); // calls child class method
    System.out.println(a1.i);// calls parent class member

}

}

  • when i run this code display method of derived class is being called – user2677261 Aug 13 '13 at 05:58
  • That's the normal behavior since the object is a `Child` even if the variable is declared as `Parent`. Again, what's your real specific question? – Luiggi Mendoza Aug 13 '13 at 05:58
  • Why is `A` extending `Child`? I see no reason for that, as none of the inherited properties are used. – PurkkaKoodari Aug 13 '13 at 05:59
  • 2
    Sigh... What abstract class are we talking about? What the heck is the variable "i" doing in there? Why, if he wants to call Parent.display(), did he create a Child? Why is someone editing this post without fixing the obvious spelling issues? Lots of good questions here. – billjamesdev Aug 13 '13 at 06:04
  • ok even if i remove A extending child i am unable to get get method of Parent class – user2677261 Aug 13 '13 at 06:05
  • @user2677261 Yes, it has nothing to do with your problem, but it still complicates your code for no reason. – PurkkaKoodari Aug 13 '13 at 06:06
  • Sorry, @user2677261, I think the point is.. Child derived from Parent and overrode the display method... if you want to call Parent's display method, create a PARENT. At the A class level, it doesn't even know there IS a Parent.display anymore. If, in Child.display, you want to call Parent.display, do that with super.display. – billjamesdev Aug 13 '13 at 06:07
  • is your `Parent` class is abstract .? then you should declare it... – Prabhaker A Aug 13 '13 at 06:10
  • @BillJames OP wants to call the `display()` method of the `Parent` class on a `Child`, which overrides it. This is not possible in Java. – PurkkaKoodari Aug 13 '13 at 06:12
  • @Pietu1998 I can get that, but nowhere in his question does he seem to SAY that. And wth is "abstract" about any of this? And "overriding property"? Heck with it, I'm changing the title. – billjamesdev Aug 13 '13 at 06:13
  • There, IF that's the real question, then you have several answers below now... all of them basically the same... you can't do that. – billjamesdev Aug 13 '13 at 06:15
  • @BillJames my answer is not the same as others. – Luiggi Mendoza Aug 13 '13 at 06:17
  • what if Parent is a abstract class – user2677261 Aug 13 '13 at 06:29
  • @user2677261 then you can't call `new Parent` and the `Parent` class can have `abstract` methods with no definition. – Luiggi Mendoza Aug 13 '13 at 06:40
  • I can even have methods in abstract class – user2677261 Aug 13 '13 at 06:46
  • @user2677261 Edit your question to add more info directly in the question so others don't need to read all comments. – PhoneixS Aug 13 '13 at 06:46
  • suppose the class which is extending the abstract class is overriding the method of abstract class which is not abstract one then how to access the method of abstract class across different classes – user2677261 Aug 13 '13 at 06:49

5 Answers5

1

You cannot access Parent class display() method directly without creating an object of it. But you can access it from you child class.like this

 class Child extends Parent {
   int i = 8;
   public void display() {
      super.display();
      System.out.println("derived");
   }
 }

According to java docs Accessing Superclass Members

If your method overrides one of its superclass's methods, you can invoke the overridden method through the use of the keyword super. You can also use super to refer to a hidden field (although hiding fields is discouraged).

Prabhaker A
  • 8,317
  • 1
  • 18
  • 24
  • Wether the base cklass is abstract doesn't matter. You can of course invoke base class methods from an abstract class, once you have an object. You just can't instantiate an abstract class. – Devolus Aug 13 '13 at 06:12
  • This doesn't help in OP's question, which was if `display()` was overridden without the `super.display()` call. Also if you override a method with just `super.method()` the you don't have to override it at all. – PurkkaKoodari Aug 13 '13 at 06:14
1

when i run this code display method of derived class is being called

Looks like you have programmed in other programming languages like C++ or C# where you must specify the method as virtual in order to use the subclass method. In Java, all the methods except private, final and static are virtual.

Knowing this, since Child class already overrides display method from Parent class, when you do this:

Parent a1 = new Child();
a1.display();

a1.display will execute the display method of the actual object reference class, in this case, Child#display.

In order to fix the code, change the initialization of a1 to be a new Parent:

Parent a1 = new Parent();
a1.display();

More info:


After your edit, now you face the problem when printing a1 in the console, probably printing 9 instead of 8. What you're doing here is hiding the field. Fields do not get overridden, so the value of the i variable will depend on the actual class type declared for the variable. Since you have declared a1 as Parent, then a1.i will take the value of Parent#i.

In order to fix this code, change the a1 variable type to Child:

Child a1 = new Child();
System.out.println(a1.i);
a1.display();

Or a even better solution: NEVER try to override a field, and what's worse, don't try to access to class fields from other classes directly, instead use the respective getters and setters.

More info:


How to access methods of ancestor class when intermediate parent class overrides it?

Assuming this is your real question, then the answer is simple: you can't.

Community
  • 1
  • 1
Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
0

If I understand your question right you are trying to invoke the PARENTS display method using CHILD instance.

With just the derived class object there's NO way to instruct the compiler to make the non-virtual call to the base-class version.

Jayamohan
  • 12,734
  • 2
  • 27
  • 41
0

You just cannot. If you want the code in parent class to be executed then you either have to create a parent class (note: Parent is not abstract class) or you have to provide call to super.someMethod() in one of the methods of the child. For example:

public void display() {
        super.display();
        System.out.println("derived");
    }
ps-aux
  • 11,627
  • 25
  • 81
  • 128
0

I think what you are trying to ask is why the variables of parent class is displayed and method of child class.

Firstly, overriding is not applied to variables,it is applied on methods. Whatever class the reference belongs to, that variable will be displayed straight away.In your code,

    Parent a1 = new Child();  
    System.out.println(a1.i);

Here a1 is a reference to Parent class, so variable of Parent class is displayed.

In case of methods, it is decided at run time which version of methods would be called. That method is called whose object is created, here object of Child class is crated Parent a1 =new Child(); . See the part new Child() and hence the Child class method. But remember, this method should be present in the class that the reference belongs. For example

   Class Parent
    {
    }

   Class Child extends Parent
    { 
      public void display() 
        {
          System.out.println("derived");
        }
      public static void main (String []args)
        {
         Parent a1 = new Child();
         a1.display();    //Error  
        }  
      }

To call the base class method in your original example, use super as others have mentioned. Or simply create an object of parent class

Parent o = new Parent();
o.display();              //prints base
Malwaregeek
  • 2,274
  • 3
  • 15
  • 18