0

Here is my code; apparently I am missing main. Please help

class A {

    A get(){return this;}

}


class B1 extends A{
    B1 get(){return this;}
    void message(){System.out.println("welcome to covariant return type");}

    public static void main(String args[]){
        new B1().get().message();
    }
}  
3
  • 2
    More likely you are not running the program correctly. e.g. are you running java -cp . B1 ? I suggest you try running (and formatting your code) in your IDE. Commented Dec 9, 2014 at 19:00
  • @MichalWilkowski, B1 must be declared public and you are right. Nothing has changed since you have learned java specs :).. Commented Dec 9, 2014 at 19:23
  • I have no idea why he deleted the post. Commented Dec 9, 2014 at 19:24

1 Answer 1

1

Class A

public class A
{
    public A get()
    {
        return this;
    }
}

Class B1

public class B1 extends A
{
    public B1 get()
    {
        return this;
    }

    public void message()
    {
        System.out.println("welcome to covariant return type");
    }
}

Main method

public static void main(String[] args)
{
     B1 b1 = new B1();
     B1 b2 = b1.get();

     b2.message();
}

If you write it out like this, it's a bit clearer for you and for others to read. The main method should be enclosed in a class, but I separated it out so you can see each component of your code.

Sign up to request clarification or add additional context in comments.

1 Comment

The main method has to be inside some class.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.