Method Overriding


Method Overriding

Changing the body that inherits from a super class to a sub class happens in Method Overriding. Only the method related to the created object is always executed. When the object is created, the object inherits the same method from the superclass but gives a different output than the output given by inheritance.


Figure 1: Method Overriding Example

As shown in Figure 1, object "M" is created for class "Man". Therefore, when the method called "climb" is called, only the method of the "Man" class will be executed. Therefore, the output is "using ladder".

Method Overloading vs Method Overriding

High school students are taught by teachers in a professional manner, but Kindergarten students are taught by teachers in a different way. Therefore, the same action "teach" will have different behaviors in different situations with different inputs. The same will happen in "Method Overloading". Simply put, a class in java can have multiple methods with the same name but different parameters.

There may be behaviors such as dancing, talking, teaching, where the parent and the child may have a different approach to performing the same behavior. For example, a parent and a child may have a common singing behavior, where the child's singing behavior may differ from that of its parent. The same happens in "Method Overriding". Simply put, a Java sub class can define methods with the same signature as the java super class.


Comments

Popular posts from this blog

Polymorphism

Java Standards, Class & Object

Super Keyword