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();
}
}
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!
I am new to this concept, i know the property of interface and abstract.
when i explain the concept to my friends, they asked me to create abstract class inside the interface.
please tell me , is it possible to create abstract class inside the interface.
i googled, but i am not able find the exact answer for my question.
i tried the below code ,but i dont know how to cal the AbstractMethod.
interface Student {
public abstract class Subject {
public void AbstractMethod(){
System.out.println("hi");
}
}
}
class Data implements Student {
public void ClassMethod() {
System.out.println("method 2");
}
}
public class NewClass {
public static void main(String[] args) {
Data s=new Data();
Student.Subject obj=new Student.Subject();// compiler error
s.ClassMethod();
}
}
Wouldn't something like this be better?
interface Student {
public abstract void sayHi();
}
class Data implements Student {
#Override
public void sayHi() {
System.out.println("method 2");
}
}
Yes, you can. Here in the below example, I used Anonymous Class but you can use Lambda Expression also
interface Student {
public abstract class Subject {
public abstract void AbstractMethod();
public void show(){
System.out.println("Show Method");
}
}
}
class Data implements Student {
public void ClassMethod() {
System.out.println("method 2");
}
}
public class NewClass {
public static void main(String[] args) {
Data s=new Data();
Student.Subject obj=new Student.Subject(){
public void AbstractMethod(){
System.out.println("hi");
}
};
obj.show();
obj.AbstractMethod();
s.ClassMethod();
}
}
Can Someone tell me with an example why an class should be defined inside an interface.
The below is the simple code i was trying.
interface Watsapp
{
class A
{
public void Validate()
{
}
};
abstract public void SendText();
public void SendPic();
};
its totally depends on logic requirements.
whenever we declare inner class, it treats as a data member so here also you can treat this class as a data member
just assume scenario some one needs object of A inside Interface and there is no class right now.
see eg.
public interface Watsapp
{
class A
{
public void Validate()
{
}
public String iDoSomething()
{
return "i did";
}
};
public A objOfA = new A();
abstract public void SendText();
public void SendPic();
};
And main Class is bellow:
public class TestMain {
public static void main(String[] str){
System.out.println( Watsapp.objOfA.iDoSomething());
}
}
mostly people create anonymous class for one time use, but here You created a class with name.
see:
public interface Watsapp
{
/*class A
{
public void Validate()
{
}
public String iDoSomething()
{
return "i did";
}
};*/
Thread t = new Thread()
{
public void run() {
// something ...
}
};
abstract public void SendText();
public void SendPic();
};
Thank you.
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
public class FirstClass
{
private FirstClass()
{
new Thread(new Thread()).start();
}
public static void checkSomething(FirstClass clas)
{
//doing something
}
private class Thread implements Runnable
{
#Override
public void run() {
checkSomething(????);
}
}
public static void main(String[] args)
{
new FirstClass();
}
}
my question is, what to write in ???? to get class FirstClass, i cannot write "this", coz i would get Thread.
You can write FirstClass.this to access the enclosing class instance.