how to call inner class's method from static main() method - java

Trying to create 1 interface and 2 concrete classes inside a Parent class. This will qualify the enclosing classes to be Inner classes.
public class Test2 {
interface A{
public void call();
}
class B implements A{
public void call(){
System.out.println("inside class B");
}
}
class C extends B implements A{
public void call(){
super.call();
}
}
public static void main(String[] args) {
A a = new C();
a.call();
}
}
Now I am not really sure how to create the object of class C inside the static main() method and call class C's call() method.
Right now I am getting problem in the line : A a = new C();

Here the inner class is not static, so you need to create an instance of outer class and then invoke new,
A a = new Test2().new C();
But in this case, you can make the inner class static,
static class C extends B implements A
then it's ok to use,
A a = new C()

To instantiate an inner class, you must first instantiate the outer class. Then, create the inner object within the outer object with this syntax:
OuterClass.InnerClass innerObject = outerObject.new InnerClass();
So you need to use :
A a = new Test2().new C();
Refer the Java Tutorial.

You should do this
A a = new Test2().new C();

Related

call class with in a class from other java program

If there are two classes in a java program and we have another java program how can we use the function of 2nd class of first java program to 2nd java program e.g
One java program
Public class A
{
Public class B
{
void a();
void b();
}
}
Second java program
Public class C
{
i want to call void a() and void b() here
}
You can do it by inheritance.
public class C extends B {
public static void main(String args[]){
C foo = new C();
foo.a();
foo.b();
}
}
In C, you would need to create an instance of B. Let's call this instance bTest. You could then call these methods like this:
B bTest = new B();
bTest.a();
bTest.b();
Now, if you made a() and b() static methods, you would call them using the name of the class they are in rather than using an instance of it, as such:
B.a();
B.b();
Keep in mind that you will have to import B if it is not in the same package as C.
Since your inner class is not static one you have to create an object of A first and then create object of B.
A a = new A();
A.B b = a.new B();
b.a();
b.b();

How outer class is able to access the inner member class directly

When we define inner classes, I understand that static inner classes are accessed with the outer class and inner member class exist with the instance of the outer class.
The confusion is, if I want to hold an instance of an non-private inner member class, then the variable declaration goes like:
OuterClass.InnerClass innerObject = outerObject.new InnerClass();
How outer class is able to reference the inner member class like this? What is happening here?
Sample Code:
public class NestedClasses {
public static void main(String[] args) {
A.B b = new A().new B(); // <- How A is directly accessing B, B is not defined as static.
A.StaticClass staticClass = new A.StaticClass();
}
}
class A {
static int x;
int y;
A() {
System.out.println(this.getClass().getName());
}
static class StaticClass {
StaticClass() {
System.out.println(this.getClass().getName());
}
}
class B {
B() {
System.out.println(this.getClass().getName());
}
}
}

Can an innerclass also be a subclass and also

Can a innerclass also be a subclass. Also one more thing in this set of java planguage it's not allowing me to create a instance of subclass even though I already created a instance of my encapsulating class for the innerclass.
public class Main {
Main OpTypes[] = new Main[3];
public static void main(String[] args) {
Main c = new Main();
c.OpTypes[0] = new Division(6,3);
Jool x = new Jool();
}
public class Jool {
public Jool() {
}
}
}
If you try it, you'll find that an inner class can extend a class.
Also, since the inner class is not static, it requires an instance of the outer class when constructing it. In the code below, you'll see two ways of doing that.
Method test shows the most common way, which is to do it from an instance (non-static) method of the outer class, in which case the outer class is implicit.
Method main shows how to do it outside of an instance method of the outer class, in which case you have to give an outer class instance before the new operator.
class MyBaseClass {
}
class Main {
public static void main(String[] args) {
Main c = new Main();
Jool x = c.new Jool(); // "c" explicitly used as outer class instance
}
public void test() {
Jool x = new Jool(); // "this" implicitly used as outer class instance
}
public class Jool extends MyBaseClass { // Inner class extends unrelated class
}
}

How to access outer outer class(not outer class) property?

For example,if A has inner class B,B has inner class C,all have a property "name",I know C can access name in B by B.this.name,but how to access name in A from C?
public class A{
String name="A";
public class B{
String name="B";
public class C{
String name="C";
public C(){
//how to print name in A?
//System.out.println(B.A.name);
//System.out.println(B.A.this.name);
//System.out.println(B.this.A.name);
//System.out.println(B.this.A.this.name);
}
}
C c=new C();
}
B b=new B();
public static void main(String[] args){
new A();
}
}
I tried so many syntax but they cannot compile,also when search java outer class,I found most of them are about outer class only, not outer outer class.
Use A.this.name to access the outer most class. Or any other class.
Using System.out.println(A.this.name);

How to extends Abstract Inner Class in java

I confused if
Abstract Class A{method();method2();}
And Other Class B Which Have Inner Class C
Class B{Abstract Class C{method(){//body}}}
And now Question is how to extends Class C b/C Abstract Class must be extends else
this is Unused class.
First, let's make it simpler - this has nothing to do with Android directly, and you don't need your A class at all. Here's what you want:
class Outer {
abstract class Inner {
}
}
class Child extends Outer.Inner {
}
That doesn't compile, because when you create an instance of Child you need to provide an instance of Outer to the Inner constructor:
Test.java:6: error: an enclosing instance that contains Outer.Inner is required
class Child extends Outer.Inner {
^
1 error
There are two options that can fix this:
If you don't need to refer to an implicit instance of Outer from Inner, you could make Inner a static nested class instead:
static abstract class Inner {
}
You could change Child to accept a reference to an instance of Outer, and use that to call the Inner constructor, which uses slightly surprising syntax, but works:
Child(Outer outer) {
// Calls Inner constructor, providing
// outer as the containing instance
outer.super();
}
Note that these are alternatives - you should pick which one you want based on whether or not the inner class really needs to be an inner class.
You simply extend it
class B{abstract class C{abstract void method();}}
class D extends B{
class E extends C{
void method(){
System.out.println("Hello World");
}
}
}
Or slightly more complicated without extending outer class
class B{abstract class C{abstract void method();}}
public class F extends B.C{
F(B b){
b.super();
}
void method(){
System.out.println("Hello World");
}
public static void main(String[] args){
B b = new B();
F f = new F(b);
f.method();
}
}

Categories