Why is there a semicolon and the braces after " Machine m1 = new Machine()" ? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Why is there a semicolon and the braces after " Machine m1 = new Machine()" ?

Machine m1 = new Machine() { @Override public void start() { System.out.println("Wooooo"); } };

27th Jan 2016, 3:04 PM
Dalia Ahmed
Dalia Ahmed - avatar
2 Answers
+ 1
about that semicolon you can understand this way if it's not clear!!! for a moment let us just ignore the overriding part.. so now the statement will look like this: machine m1=new machine(); //eq1 so here we have used semicolon to terminate the statement as this is how we are suppose to create an object of a class. now keeping above statement(eq1) in mind ,apply it to the real problem which involves the overriding part. overriding part begins just before the semicolon in the above statement(eq1).
3rd Apr 2016, 8:24 AM
johnson shijin
johnson shijin - avatar
0
Because you wonna override the method start(). And this is the way to tell The compiler what you want to do. Remember if you don't use brace after start() method, the compiler thinks you want to create a new method with the same name,and this cause trouble. And finally the }; sign tells the compiler that overriding ends here.
3rd Feb 2016, 4:10 PM
Patriot Samurai
Patriot Samurai - avatar