Problem statement:
You are given a code for constructor overriding. What do you think, can we override the constructor in java?
Ans: No.
You are given a code for constructor overriding. What do you think, can we override the constructor in java?
Ans: No.
- class A {
- A() {
- System.out.println("A of A");
- }
- }
- class B extends A {
- A() {
- System.out.println("A of B");
- }
- }
- public class Sample {
- public static void main(String[] args) {
- B b = new B();
- }
- }
Output:
Return type of method is missing, meaning compiler is treating A() as a method not as a constructor and also compiler is expecting return type for the method A(), because in code there is no return type.
No comments:
Post a Comment