Showing posts with label Method. Show all posts
Showing posts with label Method. Show all posts

The 'extend' keyword


class first

{
int number;


void Add()
{
number=number+1;
}


void Subtract()
{
number=number-1;
}
}
class second extends first
{
void Add()
{
number=number*1;
}
void Divide()
{
number=number/1;
}
}
public class MClass
{
public static void main (String args[])
{
first one = new first();
one.Add();
one.Subtract();


second two = new second();
two.Add();
two.Divide()
}
}

Given the code above, second extends first where second inherits first's methods and variable. As you can see the variable in class first  and class second is the same, which is number, though number is only declared in class first. Class Second overrides the Add method and added a Divide method.

When class second extends to first, we call second as a subclass of first and first is the superclass of second. In java, two or more classes can inherit from the same parent class but a class can only have one parent.

Method Overriding

Let's start with an example. If Aphrodite Keys is a famous celebrity, her mother, Hera Keys is also a famous celebrity. Whatever the parent is doing is inherited by the child.

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.

Method Overloading


Whenever a two or more methods have the same name but different behaviors or parameters, it is called method overloading.

A good example of method overloading is a set (or a pair) of people with the same name. A very good example to understand this are social networking sites. Let's say that there are two people with the name Aphrodite Keys on Facebook. The first Aphrodite Keys is a famous celebrity, while the second is just a poser.

Between the two Aphrodite Keys, it is obvious that both have the same name. It is also obvious that both have different personalities and behaviors.  The original Aphrodite Keys posts updates regarding her work and insights of life honestly, while the poser Aphrodite Keys posts different things that may help gain her social network fame.

Given the codes below....

class CodeGalaxy
{
public int sum;

public CodeGalaxy()
{
sum = 0;
}

public void Add(int num1, int num2)
{
sum = num1 + num2;
}

public void Add(int num1, int num2, int num3)
{
sum=int1+num2+num3;
}

public void Add(int num1)
{
sum = num1;
}
}

The code of the main class is the following:

public class UseGalaxy
{
public static void mai (String args[])
{
CodeGalaxy planet = new CodeGalaxy();
planet.Add(2,3);
planet.Add(1,2,3);
planet.Add(100);
}
}

Using the first planet.Add with parameters 2 and 3, the values are passed on the first (public void) Add in your object. The values of the second planet.Add with parameters 1,2 and 3, are passed on the second (public void) Add in your object. The last planet.Add with 1 as its parameter is passed on the last method in your object.

The main idea is: You cannot pass planet.Add(2,3)  to the second method in your object with public void Add(int1, int2, int3) since planet.Add has only two values and the method requires three values. Therefore, in order to use planet.Add(2,3), the values are passed on the first method with public void Add(int1,int2)

Extended Search

Custom Search
Hello World