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
Related
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?
Is there a way on "Rewriting" a function.
Pseudo:
function a() {print "B"}
function a() {print "C"}
Output: C
Overriding
class MyClass {
public void myMethod () {
System.out.println("MyClass");
}
}
class MySubClass extends MyClass {
#Override
public void myMethod () {
System.out.println("MySubClass");
}
public static void main (String[] args) {
MyClass a = new MyClass();
a.myMethod(); // "MyClass"
MySubClass b = new MySubClass();
b.myMethod(); // "MySubClass"
}
}
In this example, MySubClass overrides the inherited method myMethod.
Overloading
class MyClass {
public void myMethod () {
System.out.println("myMethod");
}
public void myMethod (int i) {
System.out.println(i * 2);
}
public void myMethod (String s) {
System.out.println("Hello, " + s);
}
public static void main (String[] args) {
MyClass a = new MyClass();
a.myMethod(); // "myMethod"
a.myMethod(33); // "66"
a.myMethod("Jeremy") // "Hello, Jeremy"
}
}
In this example, MyClass has multiple definitions of the method myMethod, but they accept different arguments.
Simply rewrite the method in its subclass.
public class Something {
public Something() {
}
public void printHi() {
System.out.println("Hi");
}
}
public class SomethingElse extends Something {
public SomethingElse() {
}
public void printHi() {
System.out.println("I refuse to say hi!");
}
}
Something something = new Something();
something.printHi(); // prints Hi
SomethingElse somethingElse = new SomethingElse();
somethingElse.printHi(); // prints I refuse to say hi!
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
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();
}
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");
}
}