Monday 5 September 2016

Method Overloading in Java

My Placements Total entires: 2 Vertical Integration D
Method Overriding : -

If a child class has the same method as declared in the parent class is known as method overriding



Rules for Method overriding

1)                  For Overriding method must have same name as in the parent class

2)                  For Overriding method must have same parameter as in the parent class.

3)                  For Overriding must be IS-A relationship(IS-A relationship mean Inheritance).

4)                  When any method declared final cannot be overridden.

5)                  When any method declared static cannot be overridden but can be re-declared.

6)                  When any method cannot be inherited, then it cannot be overridden.

7)                  Instance methods can be overridden only if they are inherited by the subclass.



W.A.P for Method Overriding using super keyword

class school

{

public void move()

            {

                        System.out.println("Student moving in the class");

            }

}

class student extends school

{

public void move()

            {

                        System.out.println("Student are walking and runing");

                        super.move(); // invokes the super class method

            }         

}

public class te

{

public static void main(String args[])

            {

                        student b = new student();

                        b.move(); //Runs the method in studentclass

            }

}



W.A.P for Method Overriding

class school

{

void around()

            {

                        System.out.println("Student around the class");

            }

}

class student extends school

{

void around()

            {

                        System.out.println("Student are walking and runing");

            }

void read()

            {

                        System.out.println("Student are reading");

            }

}

class te2

{

public static void main(String args[])

{

            school a = new school(); // school reference and object

            student b = new student(); // school reference but student object

            a.around();// Run the method in school class

            b.around();//Run the method in student class

            b.read();

}

}



W.A.P for Method Overriding show compile time error

class school

{

void around()

            {

                        System.out.println("Student around the class");

            }

}

class student extends school

{

void around()

            {

                        System.out.println("Student are walking and runing");

            }

void read()

            {

                        System.out.println("Student are reading");

            }

}

class te1

{

public static void main(String args[])

{

            school a = new school(); // school reference and object

            school b = new student(); // school reference but student object

            a.around();// Run the method in school class

            b.around();//Run the method in student class

            b.read();

}

}

No comments:

Post a Comment