How its output is starting... | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

How its output is starting...

class Machine { public void start() { System.out.println("Starting..."); } } public static void main(String[ ] args) { Machine m1 = new Machine() { @Override public void start() { System.out.println("Wooooo"); } }; Machine m2 = new Machine(); m2.start(); }

21st May 2019, 3:17 AM
Abhishek
Abhishek - avatar
2 Answers
+ 7
Yes you're right Faisal The override property is used to override members of parent class. It's like re-writing them for child class. I prepared a example for you Abhishek https://code.sololearn.com/cYh0lW55Hs6a/?ref=app
21st May 2019, 3:40 AM
Raj Chhatrala
Raj Chhatrala - avatar
+ 4
I may definitely be wrong in this, but from what it seems because you are only overriding the start function in m1, start() in m2 is unaffected and continues to use the default code for the class.
21st May 2019, 3:38 AM
Faisal
Faisal - avatar