Overloading
All the method will share the same name but it differes based on the parameter, type of parameter and number of parameter
overloading does not take return type to differentiate overloaded method.
ex: int add(int a, int b) int add(float a , float b) are overloaded methods
Overriding
The method in the derived class the has the same name in the base class and it changes the behaviour or functionality of the method in the base class.
overriding comes with inheritance concept. here parent and child both contains the same method. and when execution takes place; child's method is called.
also
overriding-> 1) method should be public. 2)it need inheritance. 3)it need virtual keyword before it declartion. 4)it have same name with same parameter in diffrent class. 5)it require non-static method. 6)method should have same datatype. Overloading-> 1)method can be different access speicifier. 2)it doesn't need inheritacne. 3)all method should be in same class. 4)method can have diffrent datatypes
also
OVERLOADING:- 1) overloaded methods have the same name but different parameter list. 2)a subclass method can overload a superclass method eg:-int add(int a, int b) int add(float a , float b) are overloaded methods OVERRIDING:- it just in inheritance and the overriding method must hold the same name and the same signatures . the change maybe just in behavior . The Cat class in the following example is the subclass and the Animal class is the superclass. The Cat class overrides eat() method inherited from Animal class. public class Animal { public void eat() { System.out.println("Eat for Animal"); } } public class Cat extends Animal { public void eat() { System.out.println("Eat for Cat"); } }
No comments:
Post a Comment