Results 1 to 8 of 8
  1. #1
    lazerwing is offline Member
    Join Date
    Jan 2014
    Posts
    1
    Rep Power
    0

    Default Do inherited methods use their instance variables?

    Do inherited methods use their instance variables or do they use the ones in the method that inherits them?
    For example, Class B extends Class A. Class A and B both have the instance variable "potato". A client program tries to use method "cut" using an object of Class B, but class B has no cut method. So, class B uses the "cut" method inherited from class A. What I want to know is will that cut class A's potato or class B's?

  2. #2
    superhaNds is offline Senior Member
    Join Date
    Apr 2013
    Location
    Sweden
    Posts
    268
    Rep Power
    2

    Default Re: Do inherited methods use their instance variables?

    if class B extends class A then class B has all the behavior and data fields of class A. if it is an instance of B, then the 'potato' belongs to the instance of B, it is inherited.

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,785
    Blog Entries
    7
    Rep Power
    21

    Default Re: Do inherited methods use their instance variables?

    Quote Originally Posted by superhaNds View Post
    if class B extends class A then class B has all the behavior and data fields of class A. if it is an instance of B, then the 'potato' belongs to the instance of B, it is inherited.
    Are you sure? Instance variables aren't inherited (they're hidden); see this:

    Java Code:
    class A {
    	String s= "A potato";
    	
    	public void method() { System.out.println(s); }
    }
    
    class B extends A {
    	
    	String s= "B potato";
    }
    
    public class T {
    
    	public static void main(String [] args) {
    
    		new B().method();
    	}
    }
    It prints 'A potato' ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  4. #4
    superhaNds is offline Senior Member
    Join Date
    Apr 2013
    Location
    Sweden
    Posts
    268
    Rep Power
    2

    Default Re: Do inherited methods use their instance variables?

    Quote Originally Posted by JosAH View Post
    Are you sure? Instance variables aren't inherited (they're hidden); see this:

    Java Code:
    class A {
    	String s= "A potato";
    	
    	public void method() { System.out.println(s); }
    }
    
    class B extends A {
    	
    	String s= "B potato";
    }
    
    public class T {
    
    	public static void main(String [] args) {
    
    		new B().method();
    	}
    }
    It prints 'A potato' ...

    kind regards,

    Jos

    A subclass inherits all the members (fields, methods, and nested classes) from its superclass apart from private members.
    Last edited by superhaNds; 01-08-2014 at 01:25 PM.

  5. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,785
    Blog Entries
    7
    Rep Power
    21

    Default Re: Do inherited methods use their instance variables?

    Quote Originally Posted by superhaNds View Post
    A subclass inherits all the members (fields, methods, and nested classes) from its superclass apart from private members.
    Have you tried my (counter) example? (b.t.w. there are no private fields in the example)

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  6. #6
    superhaNds is offline Senior Member
    Join Date
    Apr 2013
    Location
    Sweden
    Posts
    268
    Rep Power
    2

    Default Re: Do inherited methods use their instance variables?

    In your example, you declare another variable with the same name in the subclass. Now the variable is hidden.

    BUT, class B has inherited variable s from class A, you can access it directly. All the non private members are inherited.

    I misunderstood the OP's meaning, which in his example class B has a variable with the same name declared in the subclass. My statement is true nonetheless.

  7. #7
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,785
    Blog Entries
    7
    Rep Power
    21

    Default Re: Do inherited methods use their instance variables?

    Quote Originally Posted by superhaNds View Post
    In your example, you declare another variable with the same name in the subclass. Now the variable is hidden.

    BUT, class B has inherited variable s from class A, you can access it directly. All the non private members are inherited.

    I misunderstood the OP's meaning, which in his example class B has a variable with the same name declared in the subclass. My statement is true nonetheless.
    I didn't misread nor misunderstood the OP's question and my example is exactly what s/he had in mind; as you can see from the example, the variable from class A is used; no variables can be overridden, only methods are subject to that. Your statement mixes things up.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  8. #8
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default Re: Do inherited methods use their instance variables?

    Quote Originally Posted by superhaNds View Post
    In your example, you declare another variable with the same name in the subclass. Now the variable is hidden.

    BUT, class B has inherited variable s from class A, you can access it directly. All the non private members are inherited.

    I misunderstood the OP's meaning, which in his example class B has a variable with the same name declared in the subclass. My statement is true nonetheless.
    It has access to them (put it that way) as long as they are public or protected (the subclass does not have access to private fields of the super class, nor to the default access fields if it is not in the same package as the super class) and are not hidden.

    If they are hidden it still has access to them, but only through the super reference.

Similar Threads

  1. Replies: 10
    Last Post: 01-09-2012, 09:57 PM
  2. Using non-inherited methods of subclass
    By jammas615 in forum Advanced Java
    Replies: 3
    Last Post: 12-27-2011, 06:23 AM
  3. Replies: 12
    Last Post: 03-16-2010, 07:45 PM
  4. accessing instance variables from static methods
    By ravian in forum New To Java
    Replies: 7
    Last Post: 03-01-2009, 11:09 PM
  5. hiding inherited methods
    By java_fun2007 in forum New To Java
    Replies: 1
    Last Post: 01-05-2008, 03:16 PM

Posting Permissions

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