Inheritance
Overriding Access Modifier:
In overriding, the access modifier can allow more. i.e. If the access modifier is declared as protected in the parent, the child can only change it to public.
Hiding member method:
In case of class methods, if the child class declares the same class method with the same signature (that is the combination of method name, parameter number) then it is considered as the child hides the parent's method. In that scenario, the method actually invoked depends on from where the class method is being called.
Cat cat = new Cat();
Animal animal = cat;
The Animal.classMethod() will invoke the Parent's (Animal) and the Cat.classMethod() will invoke the Child's (Cat) implementation.
This is somewhat opposite in case of instance method overriding; the method gets invoked depends on the instance of the class.
Hiding member variable:
If the child uses any member variable of the name same as in parent class, even if the types are different, the field variable is considered to be hidden by the child. In this case super keyword needs to be used to access the parent 's member variable. This is known as field hiding.
No comments:
Post a Comment