simple java question v.inheritance

Discussion in 'OT Technology' started by HardTech, Jan 12, 2006.

  1. HardTech

    HardTech hungry

    Joined:
    May 5, 2000
    Messages:
    28,103
    Likes Received:
    1
    Location:
    NorCal
    hey guys

    I'm kind of embarassed to ask these questions, but I looked online for some help and couldn't find an understandable answer to what I was looking for.

    Anyway, it's about inheritance

    class human:
    Code:
    public class Human {
      int age;
      double height;
      int weight;
      String name;
      
      public Human (int a, double h, int w, String n) {
        age = a;
        height = h;
        weight = w;
        name = n;
      }
      
      public void setAge(int a) {
        age = a;
      }
      public int getAge() {
        return age;
      }
      
      public void setHeight(double h) {
        height = h;
      }
      public double getHeight() {
        return height;
      }
      
      public void setWeight(int w) {
        weight = w;
      }
      public int getWeight() {
        return weight;
      }
      
      public void setName(String n) {
        name = n;
      }
      public String getName() {
        return name;
      }
    }
    class student
    Code:
    public class Student extends Human {
     double gpa;
     
     public Student(int a, double h, int w, String n, double g) {
       super(a, h, w, n);
       gpa = g;
     }
     
     public void setGPA(double g) {
       gpa = g;
     }
     public double getGPA() {
       return gpa;
     }
    }
    class example
    Code:
    public class Example {
      public static void main (String[] args) {
        Human michael = new Human(22, 2.3, 169, "Michael");
        Human kim = new Student(23, 2.1, 150, "James", 3.5);
        
        System.out.println(michael.getAge());
        System.out.println(kim.getGPA()); //does not work!!
        
      }
    }
    if Student is a subclass of Human and I'm creating an instance of Student, why can't I use the method inside Student?

    I understand the first section of my instantiation is Human, but since the second part is Student, wouldn't it be a Student?
     
  2. zanyspy_dude

    zanyspy_dude King of teh n00bz

    Joined:
    Aug 29, 2002
    Messages:
    4,473
    Likes Received:
    0
    Location:
    Indianapolis, IN
    Code:
    public class Example {
      public static void main (String[] args) {
        Human michael = new Human(22, 2.3, 169, "Michael");
        Human kim = new Student(23, 2.1, 150, "James", 3.5);
        
        System.out.println(michael.getAge());
        System.out.println(((Student) kim).getGPA());
        
      }
    }
    
    
    yeilds
    22
    3.5
    Code:
    public class Example {
      public static void main (String[] args) {
        Human michael = new Human(22, 2.3, 169, "Michael");
        Student kim = new Student(23, 2.1, 150, "James", 3.5);
        
        System.out.println(michael.getAge());
        System.out.println(kim.getGPA());
        
      }
    }
    also get the answer 22 and 3.5

    the reason for this is you are making a student, but calling it a human. If you want to go down the inheritance hirarchy, you need to cast down to it's true type of student. If you don't really need kim to be a human type then you should just change her instancation to be student.

    EDIT: If you're at bloomington and i'm starting a java class right now....are we in the same class?
     
    Last edited: Jan 12, 2006
  3. Deevan

    Deevan Active Member

    Joined:
    Jul 12, 2005
    Messages:
    11,719
    Likes Received:
    0
    Location:
    Orlando, FL USA Posts: 4
    In the classs where you set her as a student you don't use the weight attribute. Create a class that has gpa as the second attribute. You nmay have to change your method calls too.
     
  4. HardTech

    HardTech hungry

    Joined:
    May 5, 2000
    Messages:
    28,103
    Likes Received:
    1
    Location:
    NorCal

    that makes sense...

    what class are you in? If you're just starting, you're probably in A201 (or A202 since this is the Spring semester). I'm in S531
     
  5. zanyspy_dude

    zanyspy_dude King of teh n00bz

    Joined:
    Aug 29, 2002
    Messages:
    4,473
    Likes Received:
    0
    Location:
    Indianapolis, IN
    c343 We're making a postfix calculater implimenting a stack as our first project
     

Share This Page