class A { int a = 2, b = 3; public void display() { int c = a + b; System.out.println(c); } } class B extends A { int a = 5, b = 6; } class Tester { public static void main(String arr[]) { A x = new A(); B y = new B(); x.display(); y.display(); } }
Why does the output come out as 5,5? And not 5,11?.How would the y.display()
method work?
Inheritance allows programmers to create classes that are built upon existing classes, to specify a new implementation while maintaining the same behaviors (realizing an interface), to reuse code and to independently extend original software via public classes and interfaces.
Inheritance in Java is a mechanism in which one object acquires all the properties and behaviors of a parent object. It is an important part of OOPs (Object Oriented programming system). The idea behind inheritance in Java is that you can create new classes that are built upon existing classes.
Inheritance is a mechanism that allows one class to inherit properties or behaviors from another class. Multiple classes can inherit from the same parent class, forming a tree-like hierarchy structure. Inheriting classes can add features beyond those inherited from the parent class to allow for unique behavior.
Java. 2. Second Method: Without using the keyword super keyword after inheritance all the methods and instance variables of the parent class is inherited by the child class.
why does the output comes 5,5?
Because A.display()
only knows about the fields A.a
and A.b
. Those are the only fields that any code in A
knows about. It looks like you expect the declarations in B
to "override" the existing field declarations. They don't. They declare new fields which hide the existing fields. Variables don't behave virtually in the way that methods do - the concept of overriding a variable simply doesn't exist. From the JLS section 8.3:
If the class declares a field with a certain name, then the declaration of that field is said to hide any and all accessible declarations of fields with the same name in superclasses, and superinterfaces of the class.
You can get the effect you want by changing B
so that its constructor changes the values of the existing fields that it inherits from A
instead:
class B extends A { B() { a = 5; b = 6; } }
Note that these are not variable declarations. They're just assignments. Of course in most code (well, most code I've seen anyway) the fields in A
would be private, so couldn't be accessed from B
, but this is just example for the purpose of explaining the language behaviour.
In class A
you declare fields a
and b
. The method display
uses these fields. In class B
you declare NEW fields of the same name. You're actually hiding the old fields not "overriding" them. To assign different values to the same fields use a constructor:
class A { A(int a, int b) { this.a = a; this.b = b; } A() { this(2, 3); } int a,b; public void display() { int c=a+b; System.out.println(c); } } class B extends A { B() { super(5, 6); } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With