Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #37186
| From | Joshua Cranmer 🐧 <Pidgeot18@verizon.invalid> |
|---|---|
| Newsgroups | comp.lang.java.programmer |
| Subject | Re: Using main from abstract class in concrete class |
| Date | 2017-08-13 12:03 -0500 |
| Organization | A noiseless patient Spider |
| Message-ID | <omq0h2$33q$1@dont-email.me> (permalink) |
| References | <87h8xbx0ig.fsf@munus.decebal.nl> |
On 8/13/2017 4: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? Well, your question is gibberish, but I'm assuming what you're really asking is "can I have a static method that is invoked by dynamic dispatch on the receiver class?" The answer to that question is "not really." So static methods are inherited in scope, such that calling 'Sub.foo' looks for a static method in Sub first and then looks for it in Super if it couldn't find it. But static methods have no concept of a "this" pointer like instance methods do. Thus, there is nothing that could possibly be dispatched on. You can work around this flaw by using Class objects and using reflection to search for the method. Of course, the superclass method still doesn't have a notion of the current class object being invoked, so you'd have to pass the class parameter in. And you face the issue that this kind of reflection is not first-class in Java. An alternative? Well, factories! Constructors are essentially a kind of static method [1], and it's very desirable to be able to have some sort of generic construction mechanism. The standard design pattern is to have a factory class, which has a method that can actually construct the object in question. Now you can add on to this factory class other methods that can dispatch on the factory class instance--which is effectively the same as class object itself. [1] Okay, internally they're a special nonvirtual method. But given that you need to know the type of the receiver to execute the new opcode, they're more like a static method than an instance method in terms of use. -- Beware of bugs in the above code; I have only proved it correct, not tried it. -- Donald E. Knuth
Back to comp.lang.java.programmer | Previous | Next — Previous in thread | Find similar | Unroll 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