1

I am new to java and I remember in c++ we did something like CLASSNAME::Fn() to avoid ambiguity in inheritance.

Here's my code and I want to have same display methods in both classes and access them explicitly.

public class Main {
    public static void main(String args[]){
    Emplo e = new Emplo("samuel",19,"designer",465);
    e.display();  // here i want to call both display()
    }
}

public class Person {
    String name;
    int age;

    Person(String s, int a){
        name = s;
        age = a;
    }

    public void dispaly(){
        System.out.println("name: "+name+"\nage: "+age);
    }
}


public class Emplo extends Person {
    String desg;
    double sal;
    Emplo(String s,int a,String d, double sa){
        super(s,a);
        desg=d;
        sal=sa;
    }
    void display(){
        System.out.println("desg: "+desg+"\nsal: "+sal);
    }
}
Bohemian
  • 412,405
  • 93
  • 575
  • 722

5 Answers5

2

In java, you can't not call the specific method implementation of the class of the instance.

That is, you can't "bypass" a sub-class method and call a super-class version of the method; calling the super-class method can only be done from within the subclass using super.someMethod().

You can't even invoke a super-super class's version, ie you can't do something like super.super.someMethod()

Bohemian
  • 412,405
  • 93
  • 575
  • 722
  • If a method is `static`, it is not "inherited" and you *can* invoke specific implementations using `ClassName.someStaticMethod()` without having an instance. – Bohemian Oct 02 '14 at 05:10
  • Methods like display are they worth inheriting or is it okay to keep them static? –  Oct 02 '14 at 05:13
  • In your code the are not static - they don't have the `static` keyword, so they are instance methods. And they have to be instance methods, because they need access to instance fields. Like methods, static fields also have the `static` keyword, but it would be a design flaw to make either the fields or the methods static, because each instance of Person has its own name etc – Bohemian Oct 02 '14 at 05:33
0

In the second display method call the super class display method by using super keywod as : super.display(); (should be the first statement of the method)

and there will be no ambiguity because that display method will be called whose object is being created that means that the display() method of Employee will be called in this case

so if you want to call the display method of Person class then you should create the object of that class and reference by That class type like :

Person p = new Person(your data); p.display() // here display method of person will be called
and No you cannot call both methods from the same reference

Nawed Shaikh
  • 419
  • 5
  • 22
0

In your Emplo class display() method super.dispaly() indicates display() method of immediate super class i.e Person class.

 void display(){   // here in Emplo class you can't give more restrictive modifier(i.e  `public` to     `default`. since in `Person` class it is `public` so it must be `public`.(overriding rule)
        super.dispaly();
        System.out.println("desg: "+desg+"\nsal: "+sal);
    }

so put public modifier here:

  public void display(){
       super.dispaly();
        System.out.println("desg: "+desg+"\nsal: "+sal);
  }

`

Rustam
  • 6,485
  • 1
  • 25
  • 25
0

First of in here you are use two different method. display() in Emplo and dispaly() in Person. SO there is no point of talking ambiguity or overriding make that correct.

Suppose you are corrected that. Then you can't code keep this way

public void display(){ // method in Person
    System.out.println("name: "+name+"\nage: "+age);
} 

Then

void display(){ // method in Emplo
    System.out.println("desg: "+desg+"\nsal: "+sal);
}

You are using weaker modifier to override, So you can't compile this code. You can make public the method in Emplo.

And answer for your last question. you can't do it. can't call both method.

Ruchira Gayan Ranaweera
  • 34,993
  • 17
  • 75
  • 115
0

Perhaps this answers your question:

public void myMethod()
{   //inherited method
    super.myMethod(); //calls base class method
    //...    add more code to inherited method
}

for details see original source: In Java, how do I call a base class's method from the overriding method in a derived class?