Thursday, December 28, 2023

METHOD OVERRIDING IN JAVA

   METHOD OVER RIDING IN JAVA WITH EXPLANATION


METHOD OVERRINDING :-
                                                         * Method overriding in java occurs when a sub class provides a specific implementation .For a method that is already defined in its super classes or other related sub classes.Thus if two different sub classes have the same method names ,its called method overriding.

                                                         *Although the names of the classes are different,but the names of the methods are same .This makes it difficult to understand by compiler in which method to use when the method calling time .

                                                         *  Here we can't call all the methods by using the least sub class's
objects like as usual.This can be overcome in many ways. Now let's see a one-of-a-kind logic in it.



PROGRAM  :

 package methodor;


public class Control {


public static void main(String[] args) {

// TODO Auto-generated method stub

Nature ob=new Nature ();

ob.action();

}


}

class Trees

{

void action()

{

System.out.println(" trees");

}

}

class Wind extends Trees

{

void action()

{

System.out.println("Wind");

}

}

class Nature extends Wind

{

void action()

{

System.out.println("Nature");

}

}

                                                 
I have created four classes in this program.It has one superclass and three sub-classes. According to the concept of an inheritance in java .we are using a keyword called extends ,to link each sub classes to each other.

class Trees

{

void action()

{

System.out.println(" trees");

}

}

class Wind extends Trees

{

void action()

{

System.out.println("Wind");

}

}

class Nature extends Wind

{

void action()

{

System.out.println("Nature");

}

}

Each sub-classes has different names.But each sub-classes has the same method names.Now i need to call each of the methods.We can't call the method names by using the least sub-class's object.

Nature ob=new Nature ();

ob.action();

Wind wi=new Wind();

wi.action();

Trees tr=new Trees();

tr.action();

Now over come it, Let's create an individual objects for each individual sub-classes.Now we can call the same named method by using an object created for that sub classes.    

full fine program;-    

package methodor;


public class Control {


public static void main(String[] args) {

// TODO Auto-generated method stub

Nature ob=new Nature ();

ob.action();

Wind wi=new Wind();

wi.action();

Trees tr=new Trees();

tr.action();

}


}

class Trees

{

void action()

{

System.out.println(" trees");

}

}

class Wind extends Trees

{

void action()

{

System.out.println("Wind");

}

}

class Nature extends Wind

{

void action()

{

System.out.println("Nature");

}

}

___________________________________________________________________________________
OUTPUT

Nature

Wind

trees

No comments:

Post a Comment

DDL command in sql

DATA DEFINITION LANGUAGE DBMS language database language are used to read,update and store data in a database.There are several suc...