i dont understand the below code | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
2nd Jun 2020, 10:35 PM
Karthik Singuru
Karthik Singuru - avatar
9 Answers
+ 2
This given code cannot work at all, it violates many rules and the syntax. What did you want to do? It looks like a try to implement two classes a (as parent) and b (as child).
2nd Jun 2020, 10:42 PM
Sandra Meyer
Sandra Meyer - avatar
2nd Jun 2020, 11:22 PM
Sandra Meyer
Sandra Meyer - avatar
+ 1
Click on run and the code will be explained in output... The basics about classes and constructors are explained in the course.
3rd Jun 2020, 1:43 AM
Sandra Meyer
Sandra Meyer - avatar
+ 1
Did you read the code?
3rd Jun 2020, 1:50 AM
Sandra Meyer
Sandra Meyer - avatar
+ 1
Did you read the code, which I posted for you? I'm asking because there is no '2'. So if course it cannot be printed.
3rd Jun 2020, 1:53 AM
Sandra Meyer
Sandra Meyer - avatar
0
last bracket has wrong place, I modified it, then it is executed as: B.main() new B(); A.A() // run super class constructor B.m() // overriding ---------- public class A { public A() { System.out.println(1); } void m() { System.out.println(2); } } public class B extends A { public B() { System.out.println(3); this.m(); } public static void main(String[] args) { B b = new B(); } void m() { System.out.println(4); } }
3rd Jun 2020, 12:30 AM
zemiak
0
What will be the output for this? Can you please explain me the process
3rd Jun 2020, 1:41 AM
Karthik Singuru
Karthik Singuru - avatar
0
Why 2 is not displayed?
3rd Jun 2020, 1:47 AM
Karthik Singuru
Karthik Singuru - avatar
0
because nobody calls A.m(). you can add it to B() public B() { super.m(); // for 2 System.out.println(3); this.m(); }
3rd Jun 2020, 6:06 PM
zemiak