how to call implementation class methods in run method without hardcoding - java

interface Myinterface
{
boolean run();
}
class MyClass implements Myintr
{
boolean run()
{
boolean status=false;
disp();//hard coded
show();//hard coded
here above two methods are hard coded how can we call without hard coding
}
public void disp()
{
System.out.println("Hello Person");
}
public void show()
{
System.out.println("Welcome");
}
}
class Mainclass
{
public static void main(String args[])
{
Class aClass=Class.forName("Myclass");
Object obj=aClass.newInstance();
Myinterface myinter=(Myinterface)obj.run();
}
}

Your question is extremely unclear, but I suspect you're looking for something like this:
MyInterface.java:
interface MyInterface {
void run();
}
Impl1.java:
class Impl1 implements MyInterface {
#Override
public void run() {
System.out.println("Hello Person");
}
}
Impl2.java:
class Impl2 implements MyInterface {
#Override
public void run() {
System.out.println("Welcome");
}
}
MainClass.java:
class MainClass {
public static void main(String args[]) throws ClassNotFoundException, InstantiationException, IllegalAccessException {
Class<?> aClass = Class.forName("com.mypackage.Impl1");
Object obj = aClass.newInstance();
MyInterface myinter = (MyInterface)obj;
myinter.run();
}
}

Related

Is it possible to call an overridden method outside the main class?

public class OverrideAnimals {
public void makeSound() {
System.out.println("animal sounds!");
}
public static void main(String[] args) throws IOException {
OverrideAnimals animals=new OverrideAnimals ();
OverrideAnimals dog=new PolyDog();
}
}
class PolyDog extends OverrideAnimals {
#Override
public void makeSound(){
System.out.println("arf!!! arf!!! arf!!!");
}
}
No. The class that overrides can, however, call "super".
class PolyDog extends OverrideAnimals {
public void superMakeSound() {
super.makeSound();
}
#Override
public void makeSound() {
// ..

can we define abstract class inside the interface in java?

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();
}
}

Call method inside inner class

class aa {
public void bb() {
class cc {
public void dd() {
System.out.println("hello");
}
}
}
}
How to call dd() method in main method?
class Solution {
public static void main(String arg[]) {
/* i want to call dd() here */
}
}
To call an instance method, you need an instance of that method e.g.
class aa {
interface ii {
public void dd();
}
public ii bb() {
// you can only call the method of a public interface or class
// as cc implements ii, this allows you to call the method.
class cc implements ii {
public void dd() {
System.out.println("hello");
}
}
return new cc();
}
}
later
new aa().bb().dd();
class aa {
public void bb() {
}
static class cc {
void dd() {
System.out.println("hello");
}
}
public static void main(String[] args) {
cc c = new aa.cc();
c.dd();
}
}
You inner class should be in class aa not in method of class aa
And cc class should be static
you can call it using calling bb() call from main like,
public static void main(String... s){
new aa().bb()
}
And modify bb()like,
public void bb()
{
class cc{
public void dd()
{
System.out.println("hello");
}
}
new cc().dd();
}

How can I call the most specific method using generics?

Having the following example:
public class Test {
public static class A {}
public static void main(String[] args) {
A a = new A();
m1(a);
}
public static <T> void m1(T t) {
// t.getClass().getSimpleName() is A
// t instanceof A is true
m2(t);
}
/* Not called */
public static void m2(A a) {
System.out.println("A");
}
public static void m2(Object o) {
// o.getClass().getSimpleName() is A
// o instanceof A is true
System.out.println("O");
}
}
I don't understand why m2(Object o) is chosen instead of m2(A a). As you can see, when m2(t) is called, t "is an A".
Output:
actual
O
expected
A
How can I use generics for the situation above so that m2(A a) is chosen?
Edit:
I'd like to have a general solution that will work even if I add a type B (similar to A).
...
public static void main(String[] args) {
A a = new A();
m1(a);
B b = new B();
m1(b);
}
...
public static void m2(B b) {
System.out.println("B");
}
...
Output:
actual
O
O
expected
A
B
You have to do:
public static <T extends A> void m1(T t) {
m2(t);
}
Otherwise the compiler cannot infer that the passed parameter is compliant with m2(A a) and with pick m2(Object o) instead.
You are looking for double dispatch which Java does not support. I do not think that generics can help here, but there's the visitor design pattern with which you can emulate it:
public class Test {
public static interface Visitable {
void accept(Visitor visitor);
}
public static class A implements Visitable {
#Override
public void accept(Visitor visitor) {
visitor.visit(this);
}
}
public static class B implements Visitable {
#Override
public void accept(Visitor visitor) {
visitor.visit(this);
}
}
public static interface Visitor {
void visit(A a);
void visit(B b);
}
public static class PrintingVisitor implements Visitor {
#Override
public void visit(A a) {
System.out.println("A");
}
#Override
public void visit(B b) {
System.out.println("B");
}
}
public static void main(String[] args) {
Visitable visitable = new A();
m(visitable);
visitable = new B();
m(visitable);
}
public static void m(Visitable visitable) {
visitable.accept(new PrintingVisitor());
}
}

How to write an abstract main class

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();
}
}

Categories