Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Question regarding superclasses & subclasses

  1. #1
    Junior Member
    Join Date
    Mar 2013
    Posts
    15
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Question regarding superclasses & subclasses

    Hello

    I'm trying to understand some basics of classes that extends from other classes. I wonder if I could get some help.

    Assume I have the following class

    public class Person{
    	private String name;
    	private int age;
    	private String adress;
    	public Person(String n, int ag, String adr){
    		name = n;
    		age = ag;
    		adress = adr;
    	}
    }

    and this one below

    public class Student extends Person{
    	private double gpa;
    	public Student(String n, int ag, String adr, double gpa) {
    		super(n, ag, adr);
    		this.gpa = gpa;
    	}
    }

    What's the difference between these two lines of code below? The parameters are not interesting though, I'm just trying to figure out what they are instances of.

    Student student = new Student("name1", 20, "adress1", 50);
    Person student2 = new Student("name", 21, "adress2", 50);

    I mean in the first one it's Student student = new Student(...). On the second line there is [B]Person[/B] student2 = new Student(...).

    Is student2 a "Student" or "Person"?
    Thanks in advance.

    Best regards,
    Robin.
    Last edited by robin_; April 3rd, 2013 at 04:28 AM. Reason: bad grammar


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Question regarding superclasses & subclasses

    student2 is a variable of type Person, which holds a Student object which happens to also be a Person (due to polymorphism).

    You can check this quite handily using the instanceof method:

    if(student2 instanceof Student)
    {
        System.out.println("student2 holds a Student object");
    }
    if(student2 instanceof Person)
    {
        System.out.println("student2 holds a Person object");
    }

  3. #3
    Junior Member
    Join Date
    Mar 2013
    Posts
    15
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Question regarding superclasses & subclasses

    That gives me the impression of student2 being a Person as well as a Student, correct?

    However, I did create a method (getNumber) in the class Student that did return a number (just for testing), and the compiler said that "getNumber" was undefined for the type Person (which I can understand of course since Student extends Person). But what does this mean? I thought that since student2 was a Student as well as a Person, student2 would be able to use methods from both the class Student and Person?

    What's the difference between student & student2?

    Because this also worked
    if(student instanceof Person)
    		{
    		    System.out.println("student holds a Person object");
    		}

  4. #4
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Question regarding superclasses & subclasses

    Student is a Person, but a Person is not necessarily a Student. A Person could be a Teacher, Staff, etc.

    In general there's no way for the compiler to know before-hand if a Person variable does indeed hold a Student object.

    For example:

    Person p;
    if(Math.random() > 0.5)
    {
        p = new Student();
    }
    else
    {
        p = new Person();
    }
    // is p a Student? Don't know, must assume p is a Person.

    The compiler therefore must assume the variable holds a Person object. Since we've established that a Student is a Person, it is perfectly valid to treat every Student as a Person. That means you don't get access to any of the special features of a Student without special handling.

    Person p;
    if(Math.random() > 0.5)
    {
        p = new Student();
    }
    else
    {
        p = new Person();
    }
    if(p instanceof Student)
    {
        // we now know p is a Student
        Student s = (Student) p;
        // s is guaranteed to be hold a Student, we can access features specific to to Students
        s.getNumber();
    }
    else
    {
        System.out.println("p is not a Student");
    }

  5. The Following User Says Thank You to helloworld922 For This Useful Post:

    robin_ (April 4th, 2013)

  6. #5
    Junior Member
    Join Date
    Mar 2013
    Posts
    15
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Question regarding superclasses & subclasses

    Oh now I see, thanks a lot! I might come back later with some more questions , but this was very helpful thank you!

Similar Threads

  1. Loops & Series Question
    By nuttaay in forum Loops & Control Statements
    Replies: 5
    Last Post: November 29th, 2011, 01:46 PM