0
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);?

7
  • 1
    p1 is a person, not a employer. Commented Aug 23, 2016 at 12:20
  • 1
    Please put more effort into formatting your code when you post it. It's all over the place at the moment - and this doesn't like a minimal example to demonstrate the problem. Commented Aug 23, 2016 at 12:21
  • You should also read read stackoverflow.com/questions/513832 Commented Aug 23, 2016 at 12:21
  • 1
    You cannot cast a Class to a Subclass. You can the other way around, but that won't change the code that will be executed. Commented Aug 23, 2016 at 12:21
  • what is the expected op? Commented Aug 23, 2016 at 12:24

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

thank you so much for the answers . it worked and helped me a lot :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.