I'm working on a game, and this question came up: if a variable is set to an instance of a class, and that classes constructor sets the variable, what does the variable end up as? For example:
public class MainClass {
public static MainInterface mainInterface;
public static void main(String[] args) {
mainInterface = new SubClass1();
}
}
public interface MainInterface {
public void method();
}
public class SubClass1 implements MainInterface {
public SubClass1() {
MainClass.mainInterface = new SubClass2();
}
public void method() { }
}
public class SubClass2 implements MainInterface {
public void method() { }
}
So, in this example, would mainInterface be an instance of SubClass1 or SubClass2?
Test it and find out! :) A simple System.out.println(mainInterface.getClass()) will print out the class value.
I'm fairly certain that the process works like this:
SubClass1's constructor is called, setting mainInterface to a new
SubClass2 object.
SubClass1's constructor finishes and is assigned to mainInterface, resulting in mainInterface being the newly created SubClass1 object.
Related
My main Class has 2 inner class, 1 of them is thread, I don't know how my inner class 2 can access (Or how to know var1 is true or false) value of inner class 1, this is my example, thanks!
public class InnerClass {
public class InnerClass1 implements NativeKeyListener {
public boolean var1;
}
public class InnerClass2 implements Runnable{
#Override
public void run() {
while (true) {
var1...
}
}
}
}
You cannot access nonstatic variables/methods/inner classes unless you have instantiated the object (i.e. created an instance of the object). You need an InnerClass1 object before you can store or get anything out of it. Until you do something like InnerClass1 foo = new InnerClass1(), there is no var1 anywhere.
Anyway, I think you are misusing inner classes. I'd suggest if you haven't already walking through the Java Tutorials Trail to get a basic idea of how classes, fields, and instantiation work in Java.
You can do it by an interface or class that is implemented by innerclass1. Try this:
public interface NativeKeyListener {
boolean a();
}
public class InnerClass {
static NativeKeyListener m() {
class InnerClass1 implements NativeKeyListener {
public boolean var1;
public boolean a() {
return var1;
}
}
return new InnerClass1();
}
public class InnerClass2 implements Runnable {
public void run() {
NativeKeyListener i = InnerClass.m();
i.a();
}
}
}
How can I override instance methods of super class as static in subclass? I think this is impossible but is there any indirect way?
public class A {
public void test(){
System.out.println("");
}
}
public class B extends A{
public static void test(){//test() in B cannot override test() in A
//overriding method is static
}
}
You can't, since a static method is not an instance method. You could override the instance method with an instance method that calls the static method.
public class B extends A {
#Override
public void test(){
staticTest();
}
public static void staticTest() {
}
}
I'm not sure how much sense that would make though.
can somebody help me please properly extend my method USBtoUSART in Java?
I managed to extend it, but I have problem, to create a new instance of subclass.
public class USBtoUSART extends DesktopApplication1View implements SerialPortEventListener{
public USBtoUSART(SingleFrameApplication app){
super(app);
}
}
public class DesktopApplication1View extends FrameView {
SingleFrameApplication ap;
USBtoUSART serial = new USBtoUSART(ap);
public DesktopApplication1View(SingleFrameApplication app) {
super(app);
}
}
I wanted USBtoUSART to be a subclass of DesktopApplication1View, but I get error message of the app will crash. I think because the USBtoUSART serial = new USBtoUSART(ap); sequence.
Thanks for any help, it's eating me alive ...
As #MadProgrammer already said: you cannot create a new object of a class that holds a subclass as a field and initializes it. This will create a StackOverflowError because of the circular dependency that you introduce.
Consider following example:
public class A {
C c;
B b = new B(c);
public A(C c) {
System.out.println("A constructor");
}
}
public class B extends A {
public B(C c) {
super(c);
}
}
public class C {
}
public class Test {
public static void main(String[] args) {
new A(new C());
// new B(new C());
}
}
Both the creation of a new A and a new B will return a StackOverflowError because they both will construct class A that creates an instance of class B that creates an instance of class A, etc.
Edit to use your example:
public class USBtoUSART extends DesktopApplication1View implements SerialPortEventListener{
public USBtoUSART(SingleFrameApplication app){
super(app);
}
}
public class DesktopApplication1View extends FrameView {
SingleFrameApplication ap;
public DesktopApplication1View(SingleFrameApplication app) {
super(app);
}
}
public static void Main(string[] args){
USBtoUSART serial = new USBtoUSART(new SingleFrameApplication());
}
This will happen:
Define serial as an object of USBtoUSART
Instantiate a new USBtoUSART object. This will perform the following actions:
Enter the constructor of USBtoUSART
Enter the constructor of its superclass, DesktopApplication1View
Enter the constructor of its superclass, FrameView
Meanwhile, the parameter SingleFrameApplication will be passed to every constructor.
It sounds as if your general architecture setup is wrong. Do you perhaps want to extend SingleFrameApplication? You'll have to use a parameter in your constructor if you want USBtoUSART to be instantiated.
Alternatively, do something like this:
public class USBtoUSART extends DesktopApplication1View implements SerialPortEventListener{
public USBtoUSART(){
super(new SingleFrameApplication());
}
}
public static void Main(string[] args){
USBtoUSART serial = new USBtoUSART();
}
But I don't know if this is applicable in your situation.
I need to refactor class extracting abstract superclass.
E.g.
UpperClass {
NestedClass {
UpperClass.this.someMethod();
}
}
Like:
AbstractUpperClass {
NestedClass {
?????.this.someMethod();
}
}
After I plan inherit AbstractUpperClass in 2 classes UpperClass1 and UpperClass2.
But I don't know how to refactor this inner class because it inovokes method of enclosing class. Does it possible?
Thanks.
The trick here is knowing how the inner class works. It's essentially just a "normal", static class, but whose constructor implicitly gets a reference to the enclosing class. So, this:
public class TopLevel {
public void go() {
new Inner().bar();
}
public void foo() { }
public class Inner {
public void bar() {
TopLevel.this.foo();
}
}
}
is equivalent to this:
public class TopLevel {
public void go() {
new Inner(this).bar(); // explicitly passing in "this"
}
public void foo() { }
public static class Inner {
private final TopLevel parent; // note that we have this new field
public Inner(TopLevel parent) { // note this new constructor
this.parent = parent;
}
public void bar() { // we use the explicit reference instead
parent.foo(); // of the implicit TopLevel.this
}
}
}
So, with all that said, the way to refactor your inner class to be a top-level class is to add an explicit field referencing the UpperClass instance, and passing this reference into the NestedClass constructor. In other words, be like that second code snippet instead of the first.
I'm not sure if my question title describes my situation aptly, so my apologies if it doesn't! Anyway, let's say I have the following code snippet (visibility is as stated):
public class ChildClass extends ParentClass {
// more code
private void myMethod() {
MyClass mine = new MyClass() {
public void anotherMethod() {
// insert code to access a method in ParentClass
}
};
}
}
Is it possible for code within anotherMethod() to access a protected method found in ParentClass? If so, how can this be done?
I've tried something like...
(ParentClass.this).parentMethod();
...but obviously it doesn't work due to scope issues.
This compiles fine:
class MyClass {
}
class ParentClass {
protected void parentMethod() {
}
}
class ChildClass extends ParentClass {
private void myMethod() {
MyClass mine = new MyClass() {
public void anotherMethod() {
parentMethod(); // this works
}
};
}
}
A non-static inner class can access all methods of the enclosing class as if it were it's own methods:
public class Test {
public int getOne() {
return 1;
}
public class Inner {
public int getEnclosingOne() {
return getOne(); // this works...
}
}
}
A static inner class can not, as a static inner class is not bound to an instance of the parent class. That can only call static methods on the enclosing class.
As for methods when taking into account inheritance, an method in a non-static inner class can use all the methods of the enclosing (outer) class.
The interesting part is Test2.super.getOne() which indeed obtains getOne() from Test2.super, which is a Test. This is just like Test2 would access the method, namely using super though prefixed with Test2 to indicate you're accessing the namespace of the outer class.
public class Test2 extends Test {
public int getInnerOuterParentOne() {
Inner2 inner2 = new Inner2();
return inner2.getOuterParentOne();
}
public int getInnerOuterOne() {
Inner2 inner2 = new Inner2();
return inner2.getOuterOne();
}
public int getOne() {
return 2;
}
public class Inner2 {
public int getOuterOne() {
return getOne();
}
public int getOuterParentOne() {
return Test2.super.getOne();
}
}
public static void main(String[] args) {
Test2 test2 = new Test2();
System.out.println(test2.getInnerOuterOne()); // 2
System.out.println(test2.getInnerOuterParentOne()); // 1
}
}
There is no way to access "parent class method" in Java, irrelatively to visibility (except for super.parentMethod() in subclass's parentMethod()).
That is, if ChildClass overrides parentMethod(), there is no way to call ParentClass.parentMethod() (bypassing ChildClass.parentMethod()) from other methods of ChildClass.
However, if ChildClass doesn't override parentMethod(), that method is inherited by ChildClass, so that you can access it as a ChildClass's method, i.e. simply as parentMethod().