Results 1 to 2 of 2
  1. #1
    mac's Avatar
    mac
    mac is offline Member
    Join Date
    Dec 2009
    Location
    St. Petersburg, Russia
    Posts
    81
    Rep Power
    0

    Default inheritence questions

    First
    Java Code:
    public abstract class PreSetter {
    	public StringBuffer sourceFile = new StringBuffer("aa");
    }
    Then
    Java Code:
    public class Main extends PreSetter {
    	sourceFile.append("hello");
    }
    Then
    Java Code:
    public class Arguments extends PreSetter {
    	System.out.println(sourceFile); // blank. Why?
    }
    I thought that when using PreSetter class, i am crearing a new Object sourceFile, reference of which is accessed by Main, and assigned value "hello".
    Class Arguments later attempt to print out that reference, but it seems as if it is being "new"(ed) and assigned "aa" on access.

    Can you help me understand this?

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Your code doesn't compile so I'm not sure how you're calling things. You may want to post a small bit of code that compiles and runs and demonstrates your problem. e.g.,
    Java Code:
    public class MacTest {
      public static void main(String[] args) {
        System.out.print("Arguments: ");
        new Arguments();
        System.out.print("Arguments2: ");
        new Arguments2();
    
      }
    }
    
    abstract class PreSetter {
        public StringBuffer sourceFile = new StringBuffer("aa");
    }
    
    class Main extends PreSetter {
      
      Main() {
        sourceFile.append("hello");
      }
    }
    
    class Arguments extends PreSetter {
      Arguments() {
        System.out.println(sourceFile); 
      }
    }

Similar Threads

  1. Replies: 1
    Last Post: 02-08-2010, 02:57 PM
  2. Getting around inheritence
    By nick2price in forum New To Java
    Replies: 3
    Last Post: 01-18-2009, 07:48 AM
  3. Replies: 1
    Last Post: 11-18-2008, 05:48 PM
  4. [SOLVED] Problem with code - inheritence
    By yalla in forum New To Java
    Replies: 1
    Last Post: 03-30-2008, 07:11 AM
  5. 3 Questions
    By hiranya in forum AWT / Swing
    Replies: 4
    Last Post: 11-14-2007, 05:57 AM

Posting Permissions

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