Skip to content
Advertisement

Print a value of an extended class object from mother class object

I’ve declared a table that has the mother class type and i’ve filled it with many objects that have the extended classe type, everything looks fine and the table is successfully filled, the problem is when i’m trying to access to the table values i cannot ( in the exemple i’m trying to get the salaire attribut )
MOTHER CLASS

package TP4_exo2;

     public class personne {
        private String nom,prenom,date_de_naissance;
    
        
        public personne(){}
        public personne(String nom, String prenom, String date_de_naissance) {
            this.nom = nom;
            this.prenom = prenom;
            this.date_de_naissance = date_de_naissance;
        }
    
        
        public void affiche() {
            System.out.println("nnom :"+this.nom+
                    "nprenom :"+this.prenom+
                    "ndate de naissance :"+this.date_de_naissance);
        }
    }

SUB CLASS 1

package TP4_exo2;

public class employé extends personne {
   
   Double salaire;

   public employé() {
       super();
       salaire=0.0;
   }

   public employé(String nom, String prenom, String date_de_naissance,Double salaire) {
       super(nom,prenom,date_de_naissance);
       this.salaire=salaire;
   }


   public void affiche() {
       super.affiche();
       System.out.print("salaire :"+this.salaire);
   }

}



main class


    package TP4_exo2;
    
    import java.util.Scanner;
    
    public class programme4 {
    
    
        public static void main(String[] args) {
            personne tab [] =  new personne[2];
            Scanner sc = new Scanner(System.in);
            int i,j;
            
            for(i=0;i<tab.length;i++) {
                
                personne emp1;//declaration
                
                System.out.println("Donner le nom "+i);//demande informations
                String nom = sc.next();
                System.out.println("Donner le prenom "+i);
                String prenom = sc.next();  
                System.out.println("Donner la date de naissance "+i);
                String date = sc.next();
                System.out.println("Donner le salaire "+i);
                Double salaire = sc.nextDouble();
                
                emp1 =  new employé(nom,prenom,date,salaire);//instanier l'obj avec ls info
                tab[i] = emp1;//affecter au tableau
    
            }
            
        
            
            for(i=0;i<tab.length;i++) {
                System.out.println("EMPLOYER "+i+"n");
    
                if(tab[i].salaire>45000)**//Exception in thread "main" 
               //java.lang.Error: Unresolved compilation problem: 
               //salaire cannot be resolved or is not a field**
                {
                    tab[i].affiche();
                }
            }
            
            
        }
    
    }


Advertisement

Answer

You have told the compiler that tab contains personne objects. The compiler only knows what you tell it.

The compiler does not execute your program. It doesn’t know that your program may have added an employé instance to the array. For that matter, it doesn’t follow every method call in your program checking for whether any of those methods might have added different objects.

Since the compiler can only know for certain that tab contains objects of type personne or possibly a subtype of it, you will have to check each element’s type yourself:

if (tab[i] instanceof employé emp && emp.salaire > 45000)
{
    tab[i].affiche();
}

If you are using a version of Java older than Java 14, you will have to perform the cast manually:

if (tab[i] instanceof employé)
{
    employé emp = (employé) tab[i];
    if (emp.salaire > 45000)
    {
        tab[i].affiche();
    }
}

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement