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.
Related
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.
Let's say I have an interface A, defined as follows:
public interface A {
public void a();
}
It includes method called void a();
I have a class which implements this interface and has only one method:
public class AImpl implements A {
#Override
public void a() {
System.out.println("Do something");
}
}
Q: If in the main class, I call interface method, will it call the implementation belonging to class which implements the interface?
For example:
public static void main(String[] args){
A aa;
aa.a();
}
Will this print "Do something"?
A aa = new AImpl();
aa.a();
Here your reference variable is interface A type But actual Object is AImpl.
When you define a new interface, you are defining a new reference data
type. You can use interface names anywhere you can use any other data
type name. If you define a reference variable whose type is an
interface, any object you assign to it must be an instance of a class
that implements the interface.
Read more on Documentation
A Interface reference can hold Object of AImpl as it implements the A interface.
It depends on the runtime type of the object. See:
This is your interface:
public interface A {
public void a();
}
And this is your class:
public class AImpl implements A {
public void a() {
println("I am AImpl");
}
}
There is another implementation:
public class AnotherAImpl implements A {
public void a() {
println("I am another AImpl");
}
}
So have a look at the this main method:
public static void main(String[] args){
A aa;
aa = new AImpl();
aa.a(); // prints I am AImpl
aa = new AnotherAImpl();
aa.a(); // now it prints I am another AImpl
}
You could probably find this out by running the code yourself.
Currently you will get a NullPointerException because you haven't assigned anything to the variable aa. Changing your code to the following will invoke the method and output the text:
public static void main(String[] args){
A aa = new AImpl();
aa.a();
}
You need to invoke the routine on the actual AImpl object
A aa = new AImpl();
aa.a();
which here is equivalent to following
AImpl aa = new AImpl();
aa.a();
Your sample will raise an error since you're trying invoke a method on an uninitalized object.
No. I will not.
You have declared a variable. You should initialze it first with instance of an object in this case your class AImpl.
public static void main(String[] args){
A aa = new AImp();
aa.a();
}
Given the following block of code:
public class Trial {
public static void main (String[] args){
B obj = new B();
obj.doMethod(); #prints "From A".
}
}
class A {
private void method(){System.out.print("from A");}
public void doMethod(){method();}
}
class B extends A {
public void method(){System.out.print("from B");}
public void doMethod(){super.doMethod();}
}
It turns out that the method() from class A is invoked. Why is this?
You explicitly implement it that way. super calls method from base class which is A
public void doMethod(){super.doMethod();}
So the method chaining is like this:
B.doMethod() -> A.doMethod() -> A.method() -> "from A"
I think your question is if in class A private void method(){System.out.print("from A");} is private then why is printing "from A" in class B.
Answer is very simple you can't call method() of A class form any other class .But you can call it with object of its own.
when you calls super.doMethod(); then its function of super and method() is its own method so it can call it.
Because, see below:
class B extends A {
public void method(){System.out.print("from B");}
public void doMethod(){super.doMethod();}
}
Here in Class B's doMethod() you're invoiking Class A's doMethod() using super.doMethod(). So obviously it's printing Class A's doMethod().
You call the doMethod with super keyword. It's means it will call parent implementation
More on super keyword
Your code gives simple object creation (B obj = new B();) and a call using super. Super is used like other people mentioned for parent class. Things could have been different if you try something like (A obj = new B();), which is more interesting.
method() in class A is private and private methods can't be overriden. And when overriding it's better to use #Override annotion.
class B extends A {
#Override
public void method(){System.out.print("from B");} // Compile error
}
A similar thing happens, if you change the method to a static method.
class A {
public static void method(){System.out.print("from A");}
}
class B extends A {
public static void method(){System.out.print("from B");}
}
I have a base class in Java. In that class I want to create a private class and I want to access the object of that private class in the base class. How can I do that?
Thanks in advance!
Do you mean this:
class Test {
private Inner inner = new Inner();
private class Inner {
public void foo() {}
}
// later somewhere
public void bar() {
inner.foo();
}
}
You can access an object of an inner class by creating it and remembering its reference. Just like an instance of any other class.
public enum Outer {;
private static class Nested {
private Nested() { }
}
public static Object getNested() {
return new Nested();
}
}
public class Main {
public static void main(String... args) {
System.out.println("I have an "+ Outer.newNested());
}
}
prints
I have an Outer$Nested#3f0ef90c
A good example is from Arrays. This creates an instance of a private nested class which implements a public interface which makes it useful.
public static <T> List<T> asList(T... a) {
return new ArrayList<T>(a);
}
/**
* #serial include
*/
private static class ArrayList<E> extends AbstractList<E>
implements RandomAccess, java.io.Serializable
{
PrivateClass c = new PrivateClass();
c.getSomeObject(); //??
You can use the above code in your base class; provided the private class is an inner class of your base class.
class Test {
private class Inner {
public void foo() {
System.out.println("vsahdashdashd");
}
}
// later somewhere
public void bar() {
new Inner().foo();
}
}
class javaapplication9 extends Test
{
public static void main(String[] args) {
Test inner = new Test();
inner.bar();
}
you can access private member from this type.
Suppose I have a class A:
public class A {
public A(){....}
public void method1() {...}
};
And an instance of that class:
A anA = new A();
Is there any way to override the method1() only for anA?
This question arises when I write a small painting program in which I have to extend the JPanel class several times just to make minor changes to the different panels that have slightly different characteristics.
You can do the following:
A anA = new A() {
public void method1() {
...
}
};
This is the same as:
private static class myA extends A {
public void method1() {
...
}
}
A anA = new myA();
Only with the exception that in this case myA can be reused. That's not possible with anonymous classes.
You can create a an new anonymous class on the fly, as long as you are using the no-arg constructor of your class A:
A anA = new A() {
#Override
public void method1() {
...
}
};
Note that what you want to do is very close to what is known as a lambda, which should come along the next release 8 of Java SE.
I like to do this kind of thing with a delegate, or "strategy pattern".
public interface ADelegate {
public void method1();
}
public class A {
public A(){....}
public ADelegate delegate;
public final void method1() { delegate.method1(); }
};
A anA = new A();
anA.delegate = new ADelegate() {
public void method1() { ... }
};