0
i dont understand the below code
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).
+ 2
https://code.sololearn.com/cbU8MthvqeL0/?ref=app
Readability rules!
+ 1
Click on run and the code will be explained in output... The basics about classes and constructors are explained in the course.
+ 1
Did you read the code?
+ 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.
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);
}
}
0
What will be the output for this? Can you please explain me the process
0
Why 2 is not displayed?
0
because nobody calls A.m().
you can add it to B()
public B() {
super.m(); // for 2
System.out.println(3);
this.m();
}