0
class Vehicle{
    public void park(){
        //Do something
    }
}
class Example{
    public static void main(String args[]){
    Vehicle v1=new Vehicle();
    Vehicle v2=new Vehicle(){
        public void park(){
            //Do something
        }
        public void start(){
            //Do something
        }
        //start();
    }/*.start()*/;
    //v2.start();
    }
}

//All of those calling for start functions are illegal. Is there a possible way to access start function?

  • 2
    Only by calling it from within `park()` – Tim Moore Mar 01 '23 at 10:10
  • 1) You could call `start` using reflection. 2) You could call `start` in a context where the static type of the the subclass of `Vehicle` hasn't been obscured; e.g. by assigning to a variable of type `Vehicle`. 3) Just don't do it. 4) See the duplicate link for more information. – Stephen C Mar 01 '23 at 12:20
  • 2
    Just declare `v2` using `var`, i.e. `var v2 = new Vehicle() { …` – Holger Mar 02 '23 at 12:20

0 Answers0