wondering how it is possible to call public m method?
public class Test1 {
public static void main(String[] args) {
Test1 test = new Test1() {
public void m() {
System.out.println("m");
}
};
}
}
I don't believe you can. You'd have to create an interface or subclass. (Well, okay, that's probably not true. You could probably do it with reflection.)
E.g., like this (where you call it via test.m() after construction):
public class Test1 {
public static void main(String[] args) {
SubTest1 test = new SubTest1() {
public void m() {
System.out.println("m");
}
};
test.m();
}
private static abstract class SubTest1 extends Test1 {
public abstract void m();
}
}
Or like this, where it happens during construction:
public class Test1 {
public static void main(String[] args) {
SubTest1 test = new SubTest1() {
public void m() {
System.out.println("m");
}
};
}
private static abstract class SubTest1 extends Test1 {
public SubTest1() {
this.m();
}
public abstract void m();
}
}
You can't define an anonymous class constructor, so that last uses the constructor of the SubTest1 class and the abstract method.
You cannot directly invoke m since test is of type Test1 which does not contain a method called m, but you should never find yourself in a situation like this. The whole point of anonymous classes is to alter some already-existent aspect of the base class's functionality, so adding new methods makes no sense. Consider rethinking your design or using a named class instead.
Of course, if you won't care about test in the future you could do this:
new Test1() {
public void m() {
System.out.println("m");
}
}.m();
Although you would rarely want to do something like this, it could be useful if you're working with Thread or Runnable and need to invoke the run method.
If Test1 had a method called "m" you could just call test.m() after you instantiated the inner class:
public class Test1 {
public static void main(String[] args) {
Test1 test = new Test1() {
public void m() {
System.out.println("New Behavior");
}
};
test.m();
}
public void m() {
System.out.println ("Default Behavior");
}
}
Running this would output:
New Behavior
Related
I'm trying to write a factory class that takes in a class inherited from a specific abstract base class and calls a static method on that class to perform some logic and data manipulation before creating one or more instances of that class based on that manipulated data. So far, this is what I have:
public abstract class Foobar {
public static void sayHi() {}
}
public class Foo extends Foobar {
public static void sayHi() {
System.out.println("Hi from Foo!");
}
}
public class Bar extends Foobar {
public static void sayHi() {
System.out.println("Hi from Bar!");
}
}
public class PolymorphicFoobar {
public PolymorphicFoobar(Class<Foobar> cls) {
// Do some logic before creating an instance
cls.sayHi();
}
}
class Playground {
public static void main(String[ ] args) {
// Neither works
new PolymorphicFoobar(Foo.class);
new PolymorphicFoobar((Class<Foobar>)Bar.class);
}
}
You can do it with reflection and a capture-of wildcard; like
public PolymorphicFoobar(Class<? extends Foobar> cls) {
try {
Method sayHi = cls.getMethod("sayHi");
sayHi.invoke(cls);
} catch (Exception e) {
e.printStackTrace();
}
}
And then to invoke it, the syntax is very similar to what you had (you're missing new, but otherwise the first form is good). Like,
public static void main(String[] args) {
new PolymorphicFoobar(Foo.class);
new PolymorphicFoobar(Bar.class);
}
Outputs
Hi from Foo!
Hi from Bar!
In this class abstract class object is instantiated by overriding the getNum(), what is the purpose of this?
public abstract class AbstractTest {
public int getNum() {
return 45;
}
public static void main(String[] args) // main function
{
AbstractTest t = new AbstractTest() // From this point didn't understand
{
public int getNum() // function
{
return 22;
}
}; //use of this
System.out.println(t.getNum()); // output
}
}
The instantiation in your main() method is simply an inline class definition of a concrete instance of the abstract class AbstractTest. To be clear, the variable t is an anonymous, non abstract class instance. The following code would achieve the same thing:
public class ConcreteTest extends AbstractTest {
#Override
public int getNum() {
return 22;
}
}
public static void main (String [] args) {
ConcreteTest t = new ConcreteTest();
System.out.println(t.getNum());
}
There are instances in the course of development where it can be cumbersome to have to create a formal class definition. For example, if you only need a single instance of the abstract AbstractTest class, it would be easier to use an inline definition.
We call this 'Anonymous Class': When you need to create and use a class, but do not need to give its name or reused use, you can use an anonymous class. Here is the offical doc. Not only used for abstract class, can also be used for interface and general extensible class.
interface Base {
void print();
}
public static void main(String[] args) {
Base aInterface = new Base() {
#Override
public void print() {
System.out.println("A anonymous implement.");
}
};
Thread aThread = new Thread() {
#Override
public void run() {
super.run();
}
};
}
interface Y {
void search(String name);
}
class A implements Y {
void search(String name) {
//Is it possible to say: "If I was called from class B then do a search("B");
}
}
class B extends A {
}
public class Main {
public static void main(String[] args) {
B b = new B();
b.search();
}
}
Given the above code is it possible to reason in superclass which subclass was used for calling a method?
The reason I want to do this is because the code in Search is very similar for all Subclasses, the only thing that changes is the Classname, so I thought there is no need to Override in each subclass. I have updated the code to reflect this. Please let me know if there is a better way of doing it/
Calling this.getClass() inside your search method will give you the concrete class of the current instance.
For example:
class Example
{
static class A {
public void search() {
System.out.println(getClass());
}
}
static class B extends A {}
public static void main (String[] args) throws java.lang.Exception
{
new A().search();
new B().search();
}
}
outputs
class Example$A
class Example$B
The cleanest way to do it is to override the method in each subclass.
interface Y {
void search();
}
class A implements Y {
public void search(){
search("A");
}
protected void search(String name) {
// implement your searching algoithm here
}
}
class B extends A {
public void search(){
search("B");
}
}
public class Main {
public static void main(String[] args) {
B b = new B();
b.search();
}
}
That's the way inheritance is suppose to works. A super class should not know its subclasses.
And, in case you extends your class B, you can easily either:
-Keep the same behaviour as B:
class C extends B {
// do nothing, when calling search, it calls the method implemented in B
}
-Change the behaviour to search for "C"
class C extends B {
public void search(){
search("C"); // or search("whateveryouwant")
}
}
You can simply override the method in class B.
The other way could be to write the search() method as
void search() {
if (this.getClass().equals(B.class)) {
//The logic for B
} else if (this.getClass().equals(A.class)) {
//The logic for A
}
}
You have to provide the fully qualified name for the class.
Better follow template pattern.
interface Y {
void search(String name);
}
abstract class AbstractionTemplate implements Y{
#Override
public void search(String name) {
//a lot of code.
System.out.println("common stuff start");
doImplspecificStuffOnly();
System.out.println("common stuff end");
//a lot of code.
}
abstract void doImplspecificStuffOnly();
}
class A extends AbstractionTemplate{
#Override
void doImplspecificStuffOnly() {
System.out.println("a's stuff");
}
}
class B extends A {
#Override
void doImplspecificStuffOnly() {
System.out.println("B's stuff");
}
}
public class Main {
public static void main(String[] args) {
B b = new B();
b.search("hey");
}
}
package innerclasstest;
interface Demo {
}
class Bar {
public void call() {
Foo f = new Foo();
f.doStuff(new Demo() {
public void fall() {
System.out.println("In method args...");
}
});
}
}
class Foo {
public void doStuff(Demo demo) {
System.out.println("In stuff");
}
}
public class ClassArg {
public static void main(String[] args) {
Bar b = new Bar();
b.call();
}
}
In the above example how we can call the anonymous class method Fall. in there is any way to call this method.I don't know which approach I should pick to call this method.
The only reason you cannot write
demo.fall();
is that you didn't announce that method in the interface. Change it like:
interface Demo {
void fall();
}
and then it works.
If you want to do different things with an annonymous class you need to assign it to a variable, as the class is not reusable.
Something like:
Demo extendedDemo = new Demo() {
public void fall() {
System.out.println("In method args...");
}
};
you can use it then for your call:
f.doStuff(extendedDemo);
You can call the internal method at declaration if doStuff can be executed after:
Demo extendedDemo = new Demo() {
public void fall() {
System.out.println("In method args...");
}
}.fall();
If you can't call doStuff later you can call the internal method using reflection:
Method m = extendedDemo.getClass().getMethod("fall", new Class[]{});
m.invoke(extendedDemo, new Class[]{});
Anonymous classes can't be referred anywhere other than the place they have been declared. To call the method it has to be declared in the interface first. Here is your complete listing with the interface tweaked a bit to make use of the method:
interface Demo {
public void fall();
}
class Bar {
public void call() {
Foo f = new Foo();
f.doStuff(new Demo() {
public void fall() {
System.out.println("In method args...");
}
});
}
}
class Foo {
public void doStuff(Demo demo) {
System.out.println("In stuff");
demo.fall();
}
}
public class ClassArg {
public static void main(String[] args) {
Bar b = new Bar();
b.call();
}
}
The output will be:
In stuff
In method args...
See http://docs.oracle.com/javase/tutorial/java/javaOO/anonymousclasses.html
In your situation :
public void doStuff(Demo demo) {
System.out.println("In stuff");
}
in this method call demo.fall() if the Demo interface provides the fall() method declaration.
I need to write some importers. They need all the same initialization. So I try to write an abstract class, which does all the initialization and also has the main method, so that all sub-classes just need to implement run() to do their specific import work:
public abstract class AbstractImporter {
public AbstractImporter() {
// Initialization
}
public abstract void run();
public static void main(String[] args) {
AbstractImporter importer = new AbstractImporter();
importer.run();
}
}
public class ConcreteClass() {
public void run() {
// Do some importing
}
}
Of course it fails to create an instance of this abstract class (new AbstractImporter()).
Does anybody has any idea how to solve that? TIA!
Obviously you need a concrete class - anonymous or otherwise.
Better to move the main method to another class and instantiate the appropriate concrete class based on data (either your domain specific or a constant) and then run it. This way each implementation can be independent of other implementations.
public abstract class AbstractImporter {
public AbstractImporter() {
// Initialization
}
public abstract void run();
}
public class ConcreteImporter1 extends AbstractImporter {
public void run() {
//do something
}
}
public class ImporterMain() {
public static void main(String[] args) {
AbstractImporter importer = createImporter(args[1]);
importer.run();
}
private static AbstractImporter createImporter(String type) {
if (type.equals("1")) {
return new ConcreteImporter1();
}
....
}
}
new AbstracterImporter() {
public void run() {
// ...
}
};
I apologize for current lack of formatting, currently on a mobile device.
public abstract class AbstractImporter {
public AbstractImporter() {
// Initialization
}
public abstract void run();
public static void main(String[] args) {
AbstractImporter importer = new AbstractImporter(){
public void run() {
System.out.println("Anonymous implementation");
}
};
importer.run();
}
}
You cannot create an instance of an abstract class.
public abstract class AbstractImporter {
public AbstractImporter() {
// Initialization
}
public abstract void run();
}
public class ConcreteClass extends AbstractImporter{
public void run(){
//Implementation
}
public static void main(String args[]){
AbstractImporter ai = new ConcreteClass();
ai.run();
}
}