SimpleQuestionR

SimpleQuestionR

//why is the output of the program exp1 and 3?whts this concept about ?? class Exp { public int x=3; public void abc() { x+=5; System.out.println("the method of exp"); }

}

public class Exp1 extends Exp { public int x=8;

public Exp1(int y)
{
    x=y;
}

public void abc()
{
    x+=5;
    System.out.println("the method of exp1");
}

public static void main(String[] rr)
{


    Exp b=new Exp1(10);
    b.abc();
    System.out.println("the value of="+b.x);
}

}

View Answers

May 25, 2012 at 12:59 AM

In the previous chapter we talked about super classes and sub classes. If a class inherits a method from its super class, then there is a chance to override the method provided that it is not marked final.

The benefit of overriding is: ability to define a behavior that's specific to the sub class type. Which means a subclass can implement a parent calss method based on its requirement.

In object oriented terms, overriding means to override the functionality of any existing method.

class Exp {<br><br>
    public int x = 3;

    public void abc() {
        x += 5;
        System.out.println("the method of exp");
    }

}

public class Exp1 extends Exp {
    public int x = 8;

    public Exp1(int y) {
        x = y;
    }

    public void abc() {
        x += 5;
        System.out.println("the method of exp1");
    }

    public static void main(String[] rr) {

        Exp b = new Exp1(10);
        b.abc();
        System.out.println("the value of=" + b.x);
    }
}

/*Out put of the program:<br>
 * the method of exp1<br>
 * the value of=3<br>
 * <br>
 * <br>
 * */









Related Tutorials/Questions & Answers:
SimpleQuestionR

Ads