Call method inside inner class - java

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

Related

how to call implementation class methods in run method without hardcoding

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

In Java is it possible to check at runtime on which subclass a method was called?

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

How to call parent method of multiple nested inner class

I want to call A.f() from B.f(), but both are inner classes, if I write the traditional way, it does not compiles.
Any easy way without a temporary variable like the A _this in the code?
class MyClass {
[...]
class A {
public void f(){System.out.println("A.f");};
public void g(){System.out.println("A.g");};
}
class B {
public void f(){System.out.println("B.f");};
}
public A a() {
return new A() {
public void g() {
// I want to avoid this step
final A _this = this;
new B() {
public void f() {
System.out.println("foo");
// this works
_this.f();
// but this does not compile
A.this.f();
}
}.f();
}
};
}
[...]
}
You need to surround the code in brackets properly and then A.this.f() compiles fine E.g.
class A {
public void f() {
System.out.println("A.f");
}
public void g() {
System.out.println("A.g");
}
public A a() {
return new A() {
public void g() {
=
new B() {
public void f() {
System.out.println("foo");
A.this.f();
}
}.f();
}
};
}
public static void main(String[] args) {
new A().a().g();
}
}
class B {
public void f() {
System.out.println("B.f");
}
}
Updated : You can replace _this.f(); with new MyClass().new A().f(); but it will result in creation of new Object.

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

How to dynamically override superclass functions using generics

If you have a base class that is in a jar file that looks like:
public class A {
public void awesome(int i){
}
}
...which is extended by these classes (also in a jar) as follows:
public class A1 extends A {
#Override
public void awesome(int i){
}
}
and
public class A2 extends A {
#Override
public void awesome(int i){
}
}
...is it possible to override the base function in a generic way?
Say there is an implementation that was being added via anonymous inner class - can you code that such that the entire anonymous inner implementation only appears once?
So instead of:
public class Test {
public static void main(String args[]){
A1 mySpecialA1 = new A1(){
#Override
public void awesome(int i){
//awesome implementation
}
};
A2 mySpecialA2 = new A2(){
#Override
public void awesome(int i){
//awesome implementation
}
};
}
}
...you could have (this is where it breaks down):
public class SpecialAFactory {
public static <T extends A> getSpecialA(){
return new T(){
#Override
public void awesome(int i){
//only once
}
};
}
}
So ultimately you would be passing in the subclass that you want to get a new anonymous instance of.
Although you cannot do it with generics, there is a simple, easy to understand, solution that lets you avoid code duplication in cases like that:
public class Test {
public static void main(String args[]){
A1 mySpecialA1 = new A1(){
#Override
public void awesome(int i){
awesomeImplementation(i);
}
};
A2 mySpecialA2 = new A2(){
#Override
public void awesome(int i){
awesomeImplementation(i);
}
};
}
private static void awesomeImplementation(int i) {
//awesome implementation
}
}

Categories