PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197
How to properly inherit fields?-VBForums
Results 1 to 3 of 3

Thread: How to properly inherit fields?

  1. #1

    Thread Starter
    New Member
    Join Date
    Nov 2019
    Posts
    1

    Question How to properly inherit fields?

    I'm sort of new to Java and have only been working with it for the past couple of months (because I'm in college) and I've been trying to get this whole inheritance thing down. I tried making a basic Superclass class and a Subclass class. My goal is to make Subclass able to read "m" while "m" is able to be changed. I'm not quite sure what I'm doing wrong, so here's my code:
    Code:
    public class Superclass {
    	public int m;
    	public void setM() {
                   //I tried this after it didn't inherit, but it didn't help.
    		this.m = 12;
    	}
    }
    
    
    public class Subclass extends Superclass {
    	
    	public void returnM() {
    		System.out.println(m);
    	}
    }
    public class CallingClass {
    
    	public static void main(String[] args) {
    		Subclass s = new Subclass();
    		s.returnM();
    	}
    }
    When I initiate the code, it prints "0". How do I fix this?
    (Sorry if this is written poorly, I've never used this site before and don't know how to properly quote my code.)

  2. #2
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    103,482

    Re: How to properly inherit fields?

    Why would you expect anything else? The only code there that changes the value of m from the default of zero is that setM method but you never call that method. If you never assign a value to m then it will always have the default value, which is zero, so the fact that displaying the value of m displays zero means that it is working exactly as you should expect.

    By the way, returnM isn't a very good name for a method that doesn't actually return anything. Generally speaking, if you want to expose a field publicly then you should do so via a pair of methods, e.g.
    java Code:
    1. private int number;
    2.  
    3. public int get_Number()
    4. {
    5.     return number;
    6. }
    7.  
    8. public void set_Number(int value)
    9. {
    10.     number = value;
    11. }
    In that case, any subclass will have access to those methods but not the field. If you want subclasses to be able to access the field directly, it should be declared protected.

  3. #3
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    32,565

    Re: How to properly inherit fields?

    The code did exectly what you told it to do. The reason it didn't print out 12 as you expected was because your expectations were incorect to begin with. You created your subclass class but you never called the setM method... so it never had the chance to set it. Code doesn't magically get called, it has to be invoked by something.


    Code:
    public class Superclass {
    	public int m;
    	public void setM() {
                   //I tried this after it didn't inherit, but it didn't help.
    		this.m = 12;
    	}
    }
    
    
    public class Subclass extends Superclass {
    	
    	public void pintM() {
    		System.out.println(m);
    	}
    }
    public class CallingClass {
    
    	public static void main(String[] args) {
    		Subclass s = new Subclass();
                    s.setM();
    		s.printM();
    	}
    }
    And I agree with jmc that returnM was poorly named. The general convention for getters and setters in Java is getX and setX and those should only get and set propertyy values.. anything else should be called what they are. To that end, I changed it to "printM"

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width