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

    Default extends question

    Say, there is a need to split my code's operation into 2 from the insepction. Dependng on what is being done, i will be assigning some values and would like said values to be available when i call the next class.

    In this example, int i needs to be available further down my program.

    Entry point

    Java Code:
    public class VarSet {
    	int i;
    
    	public VarSet(String[] args) {
    		if (args[0].equals("batch")) {
    
    			i = 12;
    			new Batch(args);
    
    		} else if (args[0].equals("jsp")) {
    			i = 22;
    			new JSP().doSometing();
    		}
    	}
    
    	public static void main(String[] args) {
    		new VarSet(args);
    	}
    
    }
    Then in Batch(), Program is insisting on the underlined constructor and does not let me set up constructor in bold.

    :confused:Question : Why? Idea is that i am all done with original args[] by now, second class my application hits has no business worrying about it


    Java Code:
    public class Batch extends VarSet {
    
    [U]	public Batch(String[] args) {
    		super(args);
    	}[/U]
    
    [B]	public Batch() {
    
    	}[/B]
    
    	protected void doSomething() {
    		System.out.println(i);
    	}
    
    }
    Please advise and "thanks for reading!"

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

    Default

    This is not what inheritance is for and I would strongly advise you not to do this. You only extend VarSet if the Batch class truly "is-a" VarSet, such as a Dog is-a(n) Animal, a JPanel is-a JComponent.

    But here you probably need to extend by composition rather than by inheritance. With composition, the relationship is a "has-a" type rather than an "is-a" type.

    So your best bet is to create your Batch class that doesn't extend VarSet, give VarSet a Batch field if necessary, and pass the necessary information (i) into Batch via a method or constructor parameter.

  3. #3
    mac's Avatar
    mac
    mac is offline Member
    Join Date
    Dec 2009
    Location
    St. Petersburg, Russia
    Posts
    81
    Rep Power
    0

    Default

    Google told me that,

    "Your superclass does not have a constructor that doesn't take arguments. Each subclass must call a constructor of its superclass for proper initialisation; if you leave this out, the compiler assumes you are calling a non-arg constructor"

    Now things are looking a bit better (no error), but another problem comes up:

    Java Code:
    public class VarSet {
    
    	int i;
    
    	public VarSet() {
    
    	}
    
    	public VarSet(String[] args) {
    
    		if (args[0].equals("batch")) {
    			System.out.println("batch");
    			i = 12;
    			new Batch().doSomething();
    
    		} else if (args[0].equals("jsp")) {
    			i = 22;
    			new JSP().doSometing();
    		}
    	}
    
    	public static void main(String[] args) {
    		new VarSet(args);
    	}
    
    }

    Java Code:
    public class Batch extends VarSet {
    
    	public Batch() {
    	}
    
    	protected void doSomething() {
    		System.out.println(i);
    	}
    
    }

    i is always 0; Value of 12 does not get assigned or is not know. Why?

  4. #4
    mac's Avatar
    mac
    mac is offline Member
    Join Date
    Dec 2009
    Location
    St. Petersburg, Russia
    Posts
    81
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    This is not what inheritance is for and I would strongly advise you not to do this. You only extend VarSet if the Batch class truly "is-a" VarSet, such as a Dog is-a(n) Animal, a JPanel is-a JComponent.

    But here you probably need to extend by composition rather than by inheritance. With composition, the relationship is a "has-a" type rather than an "is-a" type.

    So your best bet is to create your Batch class that doesn't extend VarSet, give VarSet a Batch field if necessary, and pass the necessary information (i) into Batch via a method or constructor parameter.
    Fubarable, thank you. I understand. In fact this is what i have right now and trying to get away from.

    Basically here is the problem i am trying to solve:

    User may pass me 20 parameters, each of which needs to be validated etc, or user may pass me a file containing 200 much of the same parameters. At this time, i have 2 different classes that take care of each respective case.

    Notice that depending on what needs to happen and how params are passed, processing is largely the same, but is not entirely the same.

    Once all data massaging is done, both call a Launcher class that does something with this data.

    I would like to redesign this to avoid essentially the same steps, in such a way that

    Class 1 gets user request whatever it may be and does what it can with the data

    Class 2 extends class 1 and adds some additional massaging, specific to his purpose only.

    This way instead of 2 classes that are big and hard to maintain, i will have 3 classes, one is a consolidated big one and 2 small handling differences only

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

    Default

    Again, you are using inheritance incorrectly and inappropriately. Your new Batch object is just that, a completely new object with a new i variable that is completely different from the i being set by VarSet.

    I cannot state this more emphatically: Do not misuse inheritance in this way. This is not what inheritance is for.

    Rather, why not get rid of the extends VarSet business and give the doSomething method an int parameter where you can pass "i" into this method.

    Edit: note that this was made in response to post #3 of this thread.
    Last edited by Fubarable; 04-03-2010 at 03:26 AM.

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

  7. #7
    mac's Avatar
    mac
    mac is offline Member
    Join Date
    Dec 2009
    Location
    St. Petersburg, Russia
    Posts
    81
    Rep Power
    0

    Default

    Perhaps, names i chose for my classes represent a poor choice.

    Batch actually is an extension of VarSet in a sence that everything VarSet knows, Batch should know as well. Everything. Not a subset of it.

    In this event, isn't 'extends' a way to go?

  8. #8
    mac's Avatar
    mac
    mac is offline Member
    Join Date
    Dec 2009
    Location
    St. Petersburg, Russia
    Posts
    81
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    Perhaps you want to have a look at the Decorator design pattern as it sounds as if it might be applicable to your problem.
    Ok thank you my friend. We seems to be responding to each other's different threads.

  9. #9
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    952
    Rep Power
    5

    Default

    As Fubarable told you, you really don't seem to want to extend VarSet here (although it is not at all clear what you are trying to do). I think I will be a severe understatement when I say that it is extremely unusual for a superclass to instantiate one of its own subclasses in its constructor. I won't quite say that such a thing is impossible, but I would think it is very rarely useful.*

    In any case, when you instantiate your new Batch, you are not passing any args, so the Batch() constructor in turn calls the VarSet() constructor (the one that doesn't take args) and i is never initialized.

    Perhaps if you described what you are trying to accomplish, we could point you in the right direction.

    -Gary-

    * Java's Object class, for instance, needs to instantiate Strings and Exceptions in certain cases.

Similar Threads

  1. Thread Count Extends Beyond Pool
    By jvermast in forum Threads and Synchronization
    Replies: 1
    Last Post: 04-01-2010, 08:47 AM
  2. Replies: 3
    Last Post: 03-19-2009, 06:00 PM
  3. My icon extends my JButton
    By hitmen in forum AWT / Swing
    Replies: 5
    Last Post: 03-06-2009, 08:49 AM
  4. Replies: 4
    Last Post: 02-13-2009, 08:42 AM
  5. Implements MyClass extends JFrame
    By coco in forum AWT / Swing
    Replies: 1
    Last Post: 08-06-2007, 04:43 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
  •