How will you call parameterized constructor and override method from parent class in sub-class ?

Ram Pothuraju
 Definition of Constructor:

A constructor is a special member function whose have the same name as the class name, but the function (constructor) never returns any value. A constructor initializes an objects immediately upon creation. Once defined, the constructor is automatically called immediately after the object is created.
Parameterized Constructor:

            Passing arguments to the function is called parameterized function. Similarly passing arguments to the constructor is called parameterized constructor.
Method overriding:

            When a method in a subclass has the same name and type signature as a method in its super class , then the method in the subclass is said to be override the method in the super class.
 
Describe all above feature with the following example:
class A
{
            int x,y;
            A()
            {          // zero argument constructor
            }
            A(int i,int j) // parameterized constructor
            {
                        x=i;y=j;
            }
            void show() // override methods by subclass version
            {
                        System.out.println("x="+x+" y="+y);
            }
}
class B extends A
{
            int z;
            B(int i, int j, int k) // parameterized constructor
            {
                        super(i,j);
                        z=k;
            }
            void show()
            {
                        System.out.println("z="+z);
            }
}
class override
{
            public static void main(String args[])
            {
                        B subob=new B(1,2,3); // call constructor by creating objects
                        subob.show(); // this calls subclass version
            }


 }

Tags

Post a Comment

0Comments

Post a Comment (0)