My code is like this...
but there seems to be a problem when I call the overridden method createHome(). Here is a sample code:
public class Test extends SweetHome3D {
public static void main(String [] args) {
new Test().init(args);
***createHome();***
}
#Override
public Home createHome() {
Home home = super.createHome();
// Modify home as you wish here
return home;
}
}
I take it that code didn't compile? You are calling createHome() as if it's a static method.
public static void main(String [] args) {
Test test = new Test();
test.init(args);
test.createHome();
}
Related
package jav;
class PackageDemo
{
public void display()
{
System.out.println("PackageDemo executed");
}
}
public class PackageDemoDriver
{
public static void main(String[] args) {
PackageDemo boy = new PackageDemo();
boy.display();
}
}
This is the code for a package.
I will be importing this package into a different file.
The code for that is:
package exercise;
import jav.PackageDemoDriver;
class Exe
{
public static void main(String[] args) {
}
}
What should I fill in the main method to run display(), if it is possible to do so?
You can run static methods as needed
public static void main(String[] args) {
PackageDemoDriver.main(args);
}
Based off your question I assume you want the main function in Exe to essentially run the main function in PackageDemoDriver:
public class Exe {
public static void main(String args[]) {
PackageDemoDriver.main(null);
}
}
I think that'll provide the functionality you're after, if your PackageDemo and PackageDemoDriver are in different classes
Is this what you're looking for?
package exercise;
import jav.PackageDemoDriver;
class Exe {
public static void main(String[] args) {
PackageDemoDrive.main(args);
}
}
I want to make a test program that instead of specifying main method explicitly, extends a class/abstract class and overrides a method that gets called by that superclass eg init.
My attempt:
JavaApplication.java
public class JavaApplication {
public JavaApplication(){
this.init(null);
}
public JavaApplication(String[] args) {
this.init(args);
}
public void init(String[] args) {
/* override me */
}
public static void main(String[] args) {
new JavaApplication(args);
}
}
MyApp.java:
public class MyApp extends JavaApplication {
#Override
public void init(String[] args) {
System.out.println("Hello, World!");
}
}
The code compiles but my init method is not called(The string does not appear).
What is the proper way of formulating this behavior in Java?
Related Questions:
can-i-access-a-subclass-method-through-a-base-class-typed-reference
calling-a-method-of-subclass-through-reflection
whats-wrong-with-overridable-method-calls-in-constructors
calling-an-overridden-method-from-a-parent-class-ctor
You need to add a main method in MyApp that creates an instance of MyApp:
public static void main(String[] args) {
new MyApp(args);
}
Then, run MyApp instead of JavaApplication.
I have the following classic java script.
public class HelloWorld3 {
public static void main(String[] args) {
System.out.println("Hello");
}
}
I just want to run this script from another script so that it just prints out "Hello"
this was my attempt to do just that.
public class Test {
public static void main(String[] args) {
HelloWorld3 obj = new HelloWorld3();
System.out.println(obj);
}
}
which failed and I get why it failed.
Not sure how to do it right though.
The main method is used to start a program, there is no need to have it in two different classes and to call one from the other.
To construct an instance of a class you need a constructor. Here's one:
public class HelloWorld3 {
public HelloWorld3(){
}
}
Now you can call new HelloWorld3 from another class (such as Test) to create a HelloWorld3 type object. If you want this object to print a message, lets add a method to it:
public class HelloWorld3 {
public HelloWorld3(){
}
public void printHello(){
System.out.println("Hello");
}
}
You may now use this main method inside your Test class:
public static void main(String[] args) {
HelloWorld3 obj = new HelloWorld3();
obj.printHello();
}
Modify the HelloWorld3
public class HelloWorld3 {
public static void main(String[] args) {
System.out.println("Hello");
}
public void printMeStatic(String msg) {
System.out.println(msg);
}
public void printMeInstace(String msg) {
System.out.println(msg);
}
}
Use it in other class
public static void main(String[] args) {
HelloWorld3 obj = new HelloWorld3();
System.out.println(obj.printMeInstace("a msg 1")); //use this if you need an instance/ object of the class HelloWorld3
System.out.println(HelloWorld3.printMeStatic("a msg 2")); //use this for static methods (you dont need an object to use them)
}
I was wondering if it was a bad way of programming to move the main reference in the subclass of the software.
It will probably be hard to understand what i really mean so i will do an example.
public class Main{
public static void main(String[] args) {
Main app = new Main();
Toto myToto = new Toto();
myToto.something(app);
}
}
public class Toto{
public void something(Main app){
}
}
"this" is a non-static variable, so it can not be referenced from a static method.
so we can not call something() by using "this" keyword.
Try the below code, it is working correctly
class Main{
public static void main(String[] args) {
Toto myToto = new Toto();
Main m= new Main();
myToto.something(m);
}
}
class Toto{
public void something(Main app){
}
}
wondering how it is possible to call public m method?
public class Test1 {
public static void main(String[] args) {
Test1 test = new Test1() {
public void m() {
System.out.println("m");
}
};
}
}
I don't believe you can. You'd have to create an interface or subclass. (Well, okay, that's probably not true. You could probably do it with reflection.)
E.g., like this (where you call it via test.m() after construction):
public class Test1 {
public static void main(String[] args) {
SubTest1 test = new SubTest1() {
public void m() {
System.out.println("m");
}
};
test.m();
}
private static abstract class SubTest1 extends Test1 {
public abstract void m();
}
}
Or like this, where it happens during construction:
public class Test1 {
public static void main(String[] args) {
SubTest1 test = new SubTest1() {
public void m() {
System.out.println("m");
}
};
}
private static abstract class SubTest1 extends Test1 {
public SubTest1() {
this.m();
}
public abstract void m();
}
}
You can't define an anonymous class constructor, so that last uses the constructor of the SubTest1 class and the abstract method.
You cannot directly invoke m since test is of type Test1 which does not contain a method called m, but you should never find yourself in a situation like this. The whole point of anonymous classes is to alter some already-existent aspect of the base class's functionality, so adding new methods makes no sense. Consider rethinking your design or using a named class instead.
Of course, if you won't care about test in the future you could do this:
new Test1() {
public void m() {
System.out.println("m");
}
}.m();
Although you would rarely want to do something like this, it could be useful if you're working with Thread or Runnable and need to invoke the run method.
If Test1 had a method called "m" you could just call test.m() after you instantiated the inner class:
public class Test1 {
public static void main(String[] args) {
Test1 test = new Test1() {
public void m() {
System.out.println("New Behavior");
}
};
test.m();
}
public void m() {
System.out.println ("Default Behavior");
}
}
Running this would output:
New Behavior