Puzzle on Inner Classes and Class Hiding - java

I have a sample code to solve which is based on inner classes:
package inner;
class A {
void m() {
System.out.println("Outer");
}
}
public class TestInner {
public static void main(String[] args) {
new TestInner().go();
}
private void go() {
new A().m();
class A{
void m(){
System.out.println("Inner");
}
}
new A().m();
}
class A{
void m(){
System.out.println("Middle");
}
}
}
The output given by above sample code is:
Middle
Inner
And my question is, given that I dont want to use the package name to create an object, how can I print the output as:
Outer
Inner

Since using a package is so obviously the answer, I assume you are looking for something obtuse.
You can add an outer class and call that.
class B extends A { }
// in TestInner.go()
new B().m();
class A{
void m(){
System.out.println("Inner");
}
}
new A().m();

public class TestInner {
public static void main(String[] args) {
new TestInner().go();
}
private void go() {
new inner.A().m(); //will produce output "Outer"
class A{
void m(){
System.out.println("Inner");
}
}
new A().m(); //will produce output "Inner"
}
class A{
void m(){
System.out.println("Middle");
}
}

Related

Why do we use inheritance in Java when one class can already access another class fields?

I have two scenarios
Without Extending:
class A
{
public void print()
{
System.out.println("Class A");
}
}
class B
{
B()
{
A obj=new A();
obj.print();
}
}
public class Main
{
public static void main(String[] args) {
B obj=new B();
}
}
With inheritance:
class A
{
public void print()
{
System.out.println("Class A");
}
}
class B extends A
{
B()
{
A obj=new A();
obj.print();
}
}
public class Main
{
public static void main(String[] args) {
B obj=new B();
}
}
As both work as same then why do we need to extend another class?
And what is the key difference between both methods?

Accessing class method inside interface in Java

public interface Counter{
class Base1{
protected int count1;
public Base1(){
count1=0;
}
#Override
public void putCount(){
System.out.println(count1);
}
}
}
How to access putCount() in interface's class method and count1 variable?
Try this:
Counter.Base1 myBase1 = new Counter.Base1();
myBase1.putCount();
The concept is called inner class, if you want to find further information. As count1 is protected, you cannot access it from the outside.
We tried to understand your query & this is what we got.
Plz check it
public interface Counter{
class Base1{
protected static int count1;
public Pblm(){
count1=0;
}
public static void putCount(){
System.out.println(count1);
}
}
}
The example below show how to call method of class inside an interface.
interface Outer1 {
public abstract void show();
class Inner1 {
public void display() {
System.out.println("Hello 1");
}
}
}
public class Test extends Outer1.Inner1 {
public static void main(String args[]) {
Test t1 = new Test(); t1.display();
}
}
See more at: link

Using method of one class from another class using 'this' keyword in constructor

I have two nested classes inside a class with the outer class extending another class. The structure is something like this.
public class EXTENSION_CLASS
{
public int Get_Value()
{
return(100);
}
}
public class OUTER extends EXTENSION_CLASS
{
public static class NESTED1
{
public void Method1()
{
int value=0;
value=Get_Value();
System.out.println("Method1: "+value);
}
}
public static class NESTED2
{
NESTED1 Nested1_Instance=new NESTED1();
public void Method2()
{
Nested1_Instance.Method1();
}
}
public void run()
{
NESTED2 Nested2_Instance=new NESTED2();
Nested2_Instance.Method2();
}
public static void main (String[] args)
{
OUTER New_Class=new OUTER();
New_Class.run();
}
}
I'm expecting the output: "Method1: 100". But, am getting the output: "OUTER.java:16: error: non-static method Get_Value() cannot be referenced from a static context value=Get_Value();". How can i make this working?
Cheers !
Rajesh.
One approach would be to have an instance of NESTED1 in NESTED2. For example:
private static class NESTED2
{
private NESTED1 nested1;
public NESTED2 (NESTED1 nested1) {
this.nested1 = nested1;
}
public void Method2()
{
nested1.Method1();
}
}
private static class NESTED2
{
public void Method2(NESTED1 nested1Instance)
{
nested1Instance.Method1();
}
}
That should do it with your class structure. Instead, with a modification like so....
private static class NESTED1
{
public *statc* void Method1()
{
...
}
}
private static class NESTED2
{
public *static* void Method2()
{
NESTED1.Method1();
}
}
... you could get away with no creation of objects.
If you make the methods static, you don't need to instantiate(create) a class object to call them first.

Is there a way to access an anonymous class inside another anonymous class?

interface A {
void show();
}
public class Static {
public static void main(String args[]) {
A a = new A(){
public void show(){
System.out.println("In anonymous Class");
A b =new A(){
public void show(){
System.out.println("In nested Anonymous Class");
}
};
}
};
//a.show();
}
}
If I want the to print "In nested Anonymous Class", what should I use instead of a.show()?
//EDITED LATER
Thanks guys But unfortunately mis-typed the code....I didn't mean anonymous class inside a method...but inside the class itself. Sorry for the mistake. Here is the corrected code
interface A {
void show();
}
public class Static {
public static void main(String args[]) {
A a = new A() {
public void show() {
System.out.println("In anonymous Class");
};
A b = new A() {
public void show() {
System.out.println("In nested Anonymous Class");
}
};
};
a.show();
}
}
Normally, it's not possible, since A is an interface and interfaces don't have fields. However, it is possible to access this field using reflection. It is a bit of hack though and I wouldn't suggest using this in the "real world"!
interface A {
void show();
}
public class Static {
public static void main(String args[]) throws IllegalArgumentException, IllegalAccessException, SecurityException, NoSuchFieldException {
A a = new A() {
public void show() {
System.out.println("In anonymous Class");
};
public A b = new A() {
public void show() {
System.out.println("In nested Anonymous Class");
}
};
};
// Get the anonymous Class object
Class<? extends A> anonymousClass = a.getClass();
// Get field "b"
Field fieldB = anonymousClass.getField("b");
// Get the value of b in instance a and cast it to A
A b = (A) fieldB.get(a);
// Show!
b.show();
}
}
Note: a better way might be to simply declare a getter on your interface for variable b.
make a call to b.show(); just after class declaration.
A b =new A(){
public void show(){
System.out.println("In nested Anonymous Class");
}
};
b.show();
There is nothing you should use instead of a.show(). That line should be where you put it, and uncommented. Additionally you need b.show() inside:
public static void main(String args[]) {
A a = new A(){
public void show(){
System.out.println("In anonymous Class");
A b =new A(){
public void show(){
System.out.println("In nested Anonymous Class");
}
};
b.show();
}
};
a.show();
}

How to let Subclass cover the inner class of the Class?

package tt;
class Out {
class Inner {
void print() {
System.out.println("i anm inner1");
}
}
public void run() {
Inner in = new Inner();
in.print();
}
}
class Out2 extends Out{
class Inner{
void print() {
System.out.println("i anm inner2");
}
}
}
public class Test {
/**
* #param args
*/
public static void main(String[] args) {
new Out2().run(); // 打印 i anm inner2
}
}
This program prints out ("i anm inner1") now.
How can I make the program print("i anm inner2")?
Instead of covering the inner class you could cover (override) a factory method in the base class, you the Out2.Inner must extend the Out.Inner
class Out {
class Inner {
void print() {
System.out.println("i anm inner1");
}
}
public Inner createInner(){
return new Inner();
}
public void run() {
Inner in = createInner();
in.print();
}
}
class Out2 extends Out {
class Inner extends Out.Inner {
void print() {
System.out.println("i anm inner2");
}
}
public Out.Inner createInner(){
return new Inner();
}
}
Either call the print() of the child's inner class
new Out2().new Inner().print();
Or, override run() inside Out2
#Override
public void run() {
Inner in = new Inner();
in.print();
}
you should override run() in out2

Categories

Resources