0

If there are two methods with same name in Parent class and Child Class, for example:

Parent class:

public abstract class Employee implements Payments {
    private String name;
    protected double basicSalary;

    public Employee(String name, double basicSalary) {
        this.basicSalary = basicSalary;
        this.name = name;
    }

    public void display() {
        System.out.println( " Name: " + name + " - Basic Salary: " + basicSalary +"SR" );
    }
}

Child class:

public class Faculty extends Employee {
    private String degree;
    private int teachingHours;

    public Faculty(String name, double salary, String degree, int teachingHours) {
        super(name, salary);
        this.degree = degree;
        this.teachingHours = teachingHours;
    }   

    public void display() {
        System.out.println( " Name: " +getName() + " - Degree:" +degree);
    }

And I create an object like this:

Employee[] arrEm = new Employee[4];
arrEm[0] = new Faculty("ahmad", 1000, "Phd", 10);

So if I write

arrEm[0].display();

this way the method display() will be used in child. But in case we want to use the method display in Parent Class, how can it be done?

Thanks in advance !

Melquiades
  • 8,496
  • 1
  • 31
  • 46
Nawaf Abdu
  • 615
  • 1
  • 7
  • 13

4 Answers4

2

This is because first you do: Employee [] arrEm = new Employee [4]; this will make arrEm array for objects. Then you do this: arrEm[0]= new Faculty("ahmad" , 1000 , "Phd" , 10 ); You use the arrEm[0] to refer a child class object so it will use display of the child. This is a normal case of polymorphism. You can search for it on google. It's useful. If you want to use the parent display use

arrEm[0] = new Employee;

or there is a way to upcast like

child a= new child();

Parent p=(Parent)a;

this a.display() will use the display in the parent class.

MiB
  • 575
  • 2
  • 10
  • 26
SectorCodec
  • 166
  • 1
  • 1
  • 5
0

You have to call it as the following :

super.display();
Ozgen
  • 1,072
  • 10
  • 19
  • sorry i dont understand yet .. I try write " arrEm[0].super.dislay(); " he give me an error and if i write " super.display(); " i dont selecte the object so how java will know i mean this arrEm[0] or arrEm[1] ? and i try write " super.dislay(); " in main class he give me error – Nawaf Abdu Apr 03 '15 at 16:55
0

Then either don't declare display method in child class or create instance of parent class instead of child class likerrEm[0]= new Employee("ahmad" , 1000 )(though i don't think you want this)or call parent method from child class like super.display()

M Sach
  • 33,416
  • 76
  • 221
  • 314
0

If you want to call display method of parent class, you should add super.dislay() to display() method of Faculty class.

public class Faculty extends Employee {
private String degree;
private int teachingHours;

public Faculty( String name , double salary , String degree , int teachingHours) 
{
    super(name , salary );
    this.degree=degree;
    this.teachingHours = teachingHours;
}   
public void display()
{
    //calling display() method of parent class
    Super.display();
    System.out.println( " Name: " +getName() + " - Degree:" +degree);
}

or If you do not want to call it from child class then create object using parent class constructor not child class.

  Employee [] arrEm = new Employee [4];
  //calling parent class constructor
  arrEm[0]= new Employee("ahmad");

Here you can not make use of members of Faculty class(degree, teachingHours).

Bhagwati Malav
  • 3,349
  • 2
  • 20
  • 33