View RSS Feed


Java Overriding

  1. Rules for method overriding

    by , 04-26-2012 at 06:56 PM
    • Overridden method and argument list shall exactly be the same
    • Return type shall be the subtype that is being declared in original method that is overridden in superclass, or it shall be the same.
    • Restriction is not more in case of access level as compared to access level of the overridden method. E.g, when superclass is considerd to be public, in that case overriding method present in the subclass would not be public or private. But access level might not be much restrictive ...
  2. What is overriding

    by , 04-26-2012 at 06:54 PM
    A method is sometimes inherited by a subclass from superclass which is not according the requirements. Perhaps, twenty methods are inherited by subclass & one among them is not right.
    Subclass in such case will be overriding those methods, by method redefinition.
    Method that is defined in superclass is not affected by this override.

    In following example, Cat class is the subclass & superclass includes Animal. Eat() method that has been inherited by the Animal ...
  3. Overriding with constructors

    by , 04-26-2012 at 06:53 PM
    • A constructor can’t be overridden in a superclass, because they are non-inherited.
    • A constructor can’t be overridden in similar class because both of them would have similar signatures and compiler error would be there.
    • When a subclass object is instantiated & a method is called by superclass constructor which in superclass gets overridden, superclass constructor will be calling the subclass methods.

    Java Code:
    class Super {
  4. Java method Overriding

    by , 04-26-2012 at 06:42 PM
    Example that has been given below shows the method overriding, in Java. In java, method overriding is a subclass method that overrides the super class method. Extend keywords are used by the subclass for extension of the super class. In the example, Class B is present in the subclass. Class A is present in super class. Overriding method of superclass & subclass has similar signatures. Methods of the superclass are being modified by using overriding.
    Java Code:
    class A {
    int i;