Results 1 to 4 of 4
Like Tree1Likes
  • 1 Post By JosAH

Thread: Attributes and Inheritance

  1. #1
    student91 is offline Member
    Join Date
    Nov 2011
    Posts
    4
    Rep Power
    0

    Default Attributes and Inheritance

    Hey all,

    I would like to ask a quick question. If you have a class inheriting from anther one, for example a Student class inheriting from a Person class. Then the Student class can have access to the methods and attributes of the Person class and use them as needed.

    Now my question is: Does the Person class has access to the attributes in Student class? I'm guessing no, however I need to confirm

    Thanks :)

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is online now Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,957
    Rep Power
    8

    Default Re: Attributes and Inheritance

    It depends on what exactly you mean. For example, your Student class could override a getter function from Parent. Then if another function in Parent called that getter, it would call the getter in Student.

    Either way, you could test your assumptions (and help us answer you better) if you just write a small program (an SSCCE) that tests your questions.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,517
    Blog Entries
    7
    Rep Power
    20

    Default Re: Attributes and Inheritance

    Suppose you write a Person class and I write a Student class (extending your Person class) a year later. Would your Person class be able to use my Student class's methods or member variables?

    kind regards,

    Jos
    student91 likes this.
    cenosillicaphobia: the fear for an empty beer glass

  4. #4
    student91 is offline Member
    Join Date
    Nov 2011
    Posts
    4
    Rep Power
    0

    Default Re: Attributes and Inheritance

    Given a simple example

    public class Person{
    private String name;
    private int age;

    public Person(String name, int age){
    this.name = name;
    this.age = age;
    }

    public String getName(){
    return this.name;
    }

    public int getAge(){
    return this.age;
    }
    }


    public class Student extends Person{
    public String school;

    public Student(String name, String school, int age){
    super(name,age);
    this.school = school;
    }


    }

    Now I am using Reflection to get the declared fields of a particular class. therefore if I do Studnet.class.getDeclaredFields() in the student class it will return an array with one element: school. However I dont think there is a way from Person to do Person.class.getDeclaringFields() and the array will also include school right? It will just contain name and age.

Similar Threads

  1. Attributes vs Parameters
    By java4amanda in forum Java Servlet
    Replies: 1
    Last Post: 01-18-2012, 11:46 AM
  2. Parsing XML attributes
    By javanewbie2020 in forum XML
    Replies: 1
    Last Post: 11-02-2010, 12:39 PM
  3. Get CSS Attributes using Java
    By justinmifsud in forum Advanced Java
    Replies: 0
    Last Post: 09-20-2010, 04:11 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •