Call parent method from an instance - java

I was looking at this topic:
java Access parent method from imported child class
But I'm still not sure the proper terminology for what I'm trying to do.
I have an Instance of Test and I want to call a method from the "parent" that created the instance.
public class Main {
public Main {
Test test1 = new Test();
}
public void showMessage(String message) {
System.out.println(message);
}
}
public class Test {
public Test {
//how do I call Main.showMessage("test is running")?
}
}
The answer in the topic I listed above was:
Assuming the "parent" is a class you're extending and the method you're calling is NOT static, the following should do the trick:
super.toggleVisibility();
If it's a static method - it's even Simpler:
Main.showMessage();
The issue:
I'm pretty sure I can't use super() cause I'm not extending a class. And I'm not sure if Main.showMessage(); will work because I haven't referenced the parent Main within the Test class.

You can pass an instance of Main into Test, and I'd use an interface to help decouple things:
public class Main implements Parent {
public Main() {
Test2 test2 = new Test2(this);
}
public void showMessage(String message) {
System.out.println(message);
}
public static void main(String[] args) {
new Main();
}
}
interface Parent {
void showMessage(String message);
}
class Test2 {
public Test2(Parent parent) {
parent.showMessage("I am running from in Test");
}
}

Create an instance of the desired class and call the desired method:
public class Test {
public Test() {
//how do I call Main.showMessage("test is running")?
Main main = new Main();
main.showMessage("test is running");
}
}
As noted by #BrianRoach, by your current code, this will generate a StackOverflowError since it is an infinite loop (Main instance that creates a Test instance in its constructor that creates a Test instance in its constructor ...)
So, another option may be passing a Main class instance to Test constructor:
public class Test {
public Test(Main main) {
main.showMessage("test is running");
}
}
Then, in Main constructor:
public Main() {
Test test1 = new Test(this);
}

Related

Error Using Interfaces: class Main inherits unrelated defaults for display() from types first and second public class Main implements first, second

Not getting why Im getting a Compilation error for this code. Like which property of Interface Im missing out while predicting the output.
public class Main implements first, second
{
public static void main(String[] args) {
Main object = new Main();
object.display();
}
}
interface first {
default public void display() {
System.out.println("This is the display function of interface first");
}
}
interface second {
default public void display() {
System.out.println("This is the display function of interface second");
}
}
Java is a very straight forward and explicit language, that doesn't leave much space for uncertainty. That's why basically everything ambiguous will give you a compilation error.
You have to tell Java which display method you actually want to use:
public class Main implements first, second {
#Override
public void display() {
first.super.display();
// or: second.super.display();
}
}
You have to define the display() method implementation in the Main class.
Something like this:
public class Main implements first, second
{
#Override
public void display() {
System.out.println("This is the display function of main");
}
public static void main(String[] args) {
Main object = new Main();
object.display();
}
}
Or if you want to use one of the methods defined in first or second interfaces as is you can do define it
public class Main implements first, second
{
#Override
first.super.display();
public static void main(String[] args) {
Main object = new Main();
object.display();
}
}
As mentioned by Benjamin
Unless you override it inside Main class Java cannot know what method to call in this case :)

Invoking method of anonymous class

Java 7
First of all, I'm going to simplify the example to avoid posting unnecesary code. My specific concrete example a little bit complicated, but I' try to preserve the point.
public class Test {
public static void main(String[] args){
Test t = new Test(){ //<---------------------------------------------------------
public void m(){ // |
Test t = new Test(){// |
public void m(){// |
//Here I need to invoke the most inclosing class's m() method
}
//other actions
};
}
public void someMethod(){
//action
}
};
}
public void m(){
}
}
Is it possible to do in Java? I mean, to invoke the method of anonymous class that way?
No it's impossible because there is no reference to the anonymous classes.
This is the only possible way to call the instance m() method :
new Test(){
public void m(){
}
}.m();
By definition according to the oracle documentation here :
Anonymous classes enable you to make your code more concise. They
enable you to declare and instantiate a class at the same time. They
are like local classes except that they do not have a name. Use them
if you need to use a local class only once
So if you have to use one of the methods of your class you have to create a local one.
You cannot access the methods of the anonymous class using normal java, but you are able using reflection:
Test t = new Test{
public void m() {
System.out.println("Welcome to my class");
}
};
Class<?> c = t.getClass();
Method m = c.getDeclaredMethod("m");
// m.setaccessible(true); // if private
m.invoke(t);
Here is a way to do it:
public class Test
{
public static void main(String[] args)
{
Test t = new Test()
{
public void m() // this one will be called
{
Runnable r = new Runnable()
{
#Override
public void run()
{
m();
}
};
Test t = new Test()
{
public void m()
{
r.run();
}
};
}
};
}
public void m()
{
}
}
If the method returns a value, use Callable<V> instead.

how to invoke method of annoymous class from the main method of the outer class

I'm new to Java and is trying to learn the concept of anonymous class. Could someone please tell me how I can invoke the 'awesomeMethod' from the main method of the LocallClassExample?
public class LocalClassExample {
interface Awesome {
public void awesomeMethod();
}
class AwesomeClass {
public int finalInt= 10;
Awesome a1 = new Awesome() {
#Override
public void awesomeMethod() {
System.out.println(finalInt);
}
};
}
public static void main(String[] args) {
}
}
Consider this:
new AwesomeClass().a1.awesomeMethod();
will invoke the method awesomeMethod() on the member variable a1 (which is something Awesome) of the newly created instance of AwesomeClass.
It will get more tricky once your main is outside of your AwesomeClass - and more so once it's outside of the package. In these cases you'd have to provide a getter like
public Awesome getAwesome() {
return a1;
}
Which would when invoked still execute the method as defined in your anonymous class.
Try to use this to create inner class object as:
public static void main(String[] args) {
LocalClassExample.AwesomeClass oi = new LocalClassExample().new AwesomeClass();
oi.awesomeMethod();
}

Instances of classes

Does a non-static method create an instance of the class in which it is declared? If not why this code works?
import java.awt.*;
public class Main extends Frame {
public Main () {
//super keyword needs an istance of the class don't it?
super ("My frame");
super.setSize (200,100);
}
public static void main (String [ ] args) {
new Main();
}
}
If a non-static method creates an instance of the class the next code should work...
import.java.applet.*;
public class Main extends Applet {
public void print () {
System.out.println ("Hi");
}
public void init () {
this.print();
}
}
A non-static method can only be accessed in the context of an instance that already exists.
public class Foo {
public static void someStaticMethod() { /* ... */ }
public void someNonStaticMethod() { /* ... */ }
}
Elsewhere:
Foo.someStaticMethod(); // this works
Foo.someNonStaticMethod(); // this DOESN'T work
Foo foo = new Foo();
foo.someNonStaticMethod(); // but this does
Within a non-static method, you have access to an instance by default (implicitly), or can refer to it explicitly using the this keyword. In your example:
public class Main extends Frame {
public Main () {
//super keyword needs an istance of the class don't it?
super ("My frame");
super.setSize (200,100);
}
public static void main (String [ ] args) {
new Main();
}
}
...the instance in question in the call to super is the implicit instance you create with new Main().
Instances of classes in Java are created by calling a constructor using the new keyword:
Main main = new Main();
public void Main () { } however is not a constructor, but a instance method (which, in your example, never gets called).
public class Main {
public static void main(String[] args) {
Main main = new Main(); // create instance
main.Main(); // call method 'Main'
new Main().Main(); // or both at once
}
public Main() {
// this is the (default) constructor
}
public void Main() {
// this is an instance method (whose name 'should' start lowercase
}
}
Does a non-static method create an instance of the class in which it is declared?
No.
why this code works?
Because when such a method is called, it should be called from an already instantiated instance. "this"/"super" refer to the implicit parameter, which is the instance in question (instantiated and passed in from elsewhere, with "new"). If instantiation has not occurred, a NullPointerException will immediately be thrown.
Well actually in the main question I wrote a wrong code (I've been out for all the day and I'm quite tired) anyway it helped me to understand more. Now what about this code
import java.applet.*;
public class Main extends Applet {
//overrides Applet.init ()
public void init () {
//here I use super keyword without creating an istance of the class
super.init ();
//code here...
}
}

extends of the class with private constructor

Suppose we have the following code:
class Test {
private Test() {
System.out.println("test");
}
}
public class One extends Test {
One() {
System.out.println("One");
}
public static void main(String args[]) {
new One();
}
}
When we create an object One, that was originally called the parent class constructor Test(). but as Test() was private - we get an error.
How much is a good example and a way out of this situation?
There is no way out. You have to create an available (protected, public or default) super constructor to be able to extend test.
This kind of notation is usually used in utility classes or singletons, where you don't want the user to create himself an instance of your class, either by extending it and instanciating the subclass, or by simply calling a constructor of your class.
When you have a class with only private constructors, you can also change the class to final because it can't be extended at all.
Another solution would be having a method in test which create instances of test and delegate every method call from One to a test instance. This way you don't have to extend test.
class Test {
private Test() {
System.out.println("test");
}
public static Test getInstance(){
return new Test();
}
public void methodA(){
//Some kind of implementation
}
}
public class One {
private final Test test;
One() {
System.out.println("One");
test = Test.getInstance();
}
public void methodA(){
test.methodA();
}
public static void main(String args[]) {
new One();
}
}
Make the constructor of test non-private or move One into test.
BTW, your sample code contains a few issues:
classes should be named title case (Test instead of test)
I'd suggest to make the One's constructor private unless it is called from a different class in the same package
Actually, I found there is a way out. Like this:
class Base {
private Base() {
}
public void fn() {
System.out.println("Base");
}
public static class Child extends Base {
public void fn() {
System.out.println("Child");
}
}
public static Base getChild() {
return new Child();
}
}
Now, you can use getChild() to get instance of the extended class.

Categories

Resources