ekaf
ekaf

Reputation: 153

what is wrong in this java code on inheritance?

package inheritencelatest;
public class InheritenceLatest {

    public static void main(String[] args) {

        person p1=new person(12,"ekaf","atlanta","male");
        employe e1=new employe(23,"ekaf","atlanta","male",12000);       

         e1.ageCategory();
         e1.genderIdentity();
         e1.display();
         ((employe)p1).display();
       }

}

 class person {
      int age;
      String name;
      String Address;
      String ageGroup;
      protected String  gender;
      private String canNotBeAccesed;    //cant be accessed by subclass    

      person(int age,String name,String Address,String gender) {
          this.age=age;
          this.name=name;
          this.Address=Address;
          this.gender=gender;
      }

      String ageCategory() { 
          if(age<10)
          return("kid");
          else if(age<20 &&age>10)
          return("Teen");
          else if(age<50&&age>20)
          return("adult");
          else
          return("senior");
       }

     protected void genderIdentity() {
       if("male".equals(gender))
       System.out.println("male");
       else if(gender=="female")
       System.out.println("female");
       else 
       System.out.println("Special");
     }

 }


class employe extends person {
    double salary;

    employe(int age,String name,String Address,String gender,int salary) {
       super(age,name,Address,gender);
       this.salary=salary;
       this.name=name;
    }

    @Override
    protected  void genderIdentity() { 
       if(gender=="male")
          salary=salary;
       if(gender=="female")
          salary=1.5*salary;
       else 
          salary=2*salary;    
    }

    void display() {
       System.out.println(age +" " +name+" "+Address+" "+salary+" "+gender+ " "+ageGroup);
    }

}

when i run this code i get output as :

male
23 ekaf atlanat 24000.0 male null

Exception in thread "main" java.lang.ClassCastException:inheritencelatest.person cannot be cast to inheritencelatest.employe at inheritencelatest.InheritenceLatest.main(InheritenceLatest.java:17)

MY DOUBTS ARE

how does the salary becomes 24000? Why does the agecategory is null , didn't object e1 get value of ageGroup when i write e1.agecategory? Why does ((employe)p1).display(); is raising exception ?

What could be the use of
person p2=new employe(23,"ekaf","atlanat","male",12000);?

Upvotes: 0

Views: 86

Answers (1)

Dennis Soemers
Dennis Soemers

Reputation: 8478

how salary becomes 24000?

In the genderIdentity() method of your employe class, you use == to compare gender to "male". This condition is false, because gender and "male" are different String objects (even if they have the same contents/text). You should use .equals() to compare the two Strings instead. Now, the salary gets doubled because neither gender == "male" nor gender == "female" returns true.

why the agecategory is null didnt object e1 get age category when i write e1.agecategory?

When you write e1.agecategory(), you call the method named agecategory(). This method only returns a String object, but doesn't actually do anything else with it. You don't have any code anywhere that assigns a value to the agegroup variable (which would have to look like agegroup = <something>;, so the variable keeps its default value of null.

why ((employe)p1).display(); is raising exception ?

It's because, with (employe)p1, you try to cast the p1 object to the employe type. This cannot be done because p1 was defined only to be a person, and not an employe. The other way around would work fine, because by writing class employe extends person, every employe is known to also be a person.

what could be the use of person p2=new employe(23,"ekaf","atlanat","male",12000);

Doing this can be useful because you are creating a variable p2 that can reference any kind of person, and it is not limited to only referencing employe objects. This is useful in, for example, cases where you want to be able to handle any kind of person. Java does still remember that this particular person is an employe though, so you can cast it to the more specific employe type later and call employe-specific methods on it.

Upvotes: 2

Related Questions