In programming, method overriding means that whatever the parent class is doing, it is inherited by the child class. Given the code below:
class CodeGalaxy
{
public void planet()
{
System.out.print("Pluto is not considered a planet");
}
}
class MilyWay extends CodeGalaxy
{
public void planet()
{
System.out.print("How many planets do we have now?");
}
}
public class RunningCode
{
public static void main(String args[])
{
CodeGalaxy output1 = new CodeGalaxy();
CodeGalaxy output2 = new Milkyway();
output1.planet();
output2.planet()
}
}
Class MilkyWay extends CodeGalaxy is better understood as MilkyWay overrides CodeGalaxy. The output will be:
Pluto is not considered a planet.How many planets do we have now?
For more on Overriding, refer to next post about the extend keyword.
No comments:
Post a Comment