Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #37179

Re: Using main from abstract class in concrete class

Path csiph.com!eternal-september.org!feeder.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From Eric Sosman <esosman@comcast-dot-net.invalid>
Newsgroups comp.lang.java.programmer
Subject Re: Using main from abstract class in concrete class
Date Sun, 13 Aug 2017 10:32:11 -0400
Organization A noiseless patient Spider
Lines 67
Message-ID <ompnlf$7j6$1@dont-email.me> (permalink)
References <87h8xbx0ig.fsf@munus.decebal.nl>
Mime-Version 1.0
Content-Type text/plain; charset=utf-8; format=flowed
Content-Transfer-Encoding 7bit
Injection-Date Sun, 13 Aug 2017 14:27:59 -0000 (UTC)
Injection-Info mx02.eternal-september.org; posting-host="bb447eb85c06a8624ccc0ad7d72950f6"; logging-data="7782"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+WNS8AP+Ue6900xpHSfazd"
User-Agent Mozilla/5.0 (Windows NT 10.0; WOW64; rv:52.0) Gecko/20100101 Thunderbird/52.2.1
In-Reply-To <87h8xbx0ig.fsf@munus.decebal.nl>
Content-Language en-US
Cancel-Lock sha1:Yi143REPH0cEW6vJig/xTlvMGTc=
Xref csiph.com comp.lang.java.programmer:37179

Show key headers only | View raw


On 8/13/2017 5:47 AM, Cecil Westerhof wrote:
> I have an abstract class. There can be several concrete classes that
> extend this class. Is it possible to write a main in the abstract
> class which will be used by the concrete classes and that in the main
> a instance of the concrete class is created?

     It is certainly possible for an abstract class to implement a
`public static void main(String[])' method, and it is also possible
for the JVM to start executing your program by calling that method.

     That method can create instances of any (accessible) class it
wants, including concrete subclasses of its own abstract class.
There is, however, no automatic way for the method to learn about
any particular class -- since the method is `static' it has no `this'
on which it could call getClass(), for example.  So the method will
either need to know or be told what class to instantiate:

	public abstract class Abstract {
	    public static void main(String[] args) {
	        // "I know what I want"
	        Abstract known = new ConcreteSubclass();

	        // "I do what I'm told"
	        // (exception-handling omitted)
	        Class<?> clown = Class.forName(args[0]);
	        Abstract learned = (Abstract) clown.newInstance();

	        ...
	    }
	}

     Finally, as to "used by the concrete classes" -- it's a `public'
method, so methods of the subclasses can certainly call it if that's
what you mean by "use."  What *won't* work is to tell the JVM to run
the main() method of a subclass that doesn't actually have one, and
have the JVM somehow know to run Abstract::main in its place.  Static
methods are not inherited (nor inheritable).  You could give each
concrete subclass its own main() that in turn calls the main() of
the abstract class:

	class ConcreteSubclass extends Abstract {
	    public static void main(String[] args) {
	        Abstract.main(args);
	        // super.main(args) might work; not sure off-hand
	    }
	}

However, in this situation I think it'd be simpler for the subclass
to do its own instantiating and just hand the object to a method of
the superclass:

	public abstract class Abstract2 {
	    protected static void start(Abstract2 obj, String[] args) {
	        // obj is the desired instance
	        ...
	    }
	}

	class Concrete2 extends Abstract2 {
	    public static void main(String[] args) {
	        Abstract.start(new Concrete2(), args);
	    }
	}

-- 
esosman@comcast-dot-net.invalid
Twelve hundred fifty-six days to go.

Back to comp.lang.java.programmer | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Using main from abstract class in concrete class Cecil Westerhof <Cecil@decebal.nl> - 2017-08-13 11:47 +0200
  Re: Using main from abstract class in concrete class Andreas Leitgeb <avl@logic.at> - 2017-08-13 10:59 +0000
    Re: Using main from abstract class in concrete class Andreas Leitgeb <avl@logic.at> - 2017-08-13 11:38 +0000
  Re: Using main from abstract class in concrete class Cecil Westerhof <Cecil@decebal.nl> - 2017-08-13 15:43 +0200
  Re: Using main from abstract class in concrete class Eric Sosman <esosman@comcast-dot-net.invalid> - 2017-08-13 10:32 -0400
    Re: Using main from abstract class in concrete class Eric Sosman <esosman@comcast-dot-net.invalid> - 2017-08-13 10:37 -0400
    Re: Using main from abstract class in concrete class Cecil Westerhof <Cecil@decebal.nl> - 2017-08-13 17:17 +0200
      Re: Using main from abstract class in concrete class Andreas Leitgeb <avl@logic.at> - 2017-08-17 18:56 +0000
        Re: Using main from abstract class in concrete class Cecil Westerhof <Cecil@decebal.nl> - 2017-08-18 06:37 +0200
          Re: Using main from abstract class in concrete class Andreas Leitgeb <avl@logic.at> - 2017-08-18 19:48 +0000
            Re: Using main from abstract class in concrete class Cecil Westerhof <Cecil@decebal.nl> - 2017-08-18 23:37 +0200
              Re: Using main from abstract class in concrete class Cecil Westerhof <Cecil@decebal.nl> - 2017-08-19 01:32 +0200
  Re: Using main from abstract class in concrete class Joshua Cranmer 🐧 <Pidgeot18@verizon.invalid> - 2017-08-13 12:03 -0500

csiph-web