Parameters and Polymorphism

Parameters
A parameter is a special variable that are used as a subroutine to connect to another piece of an inputted data in that subroutine. Some programming languages refer parameters as arguments. There are two kinds of parameters: formal parameter and actual parameter.


Formal Parameter
Formal parameters are actually any kinds of variables placed inside a parenthesis. In the example below, the formal parameters are num1 and num2

class CodeGalaxy
{
public int sum;

public CodeGalaxy()
{
sum = 0;
}

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


Actual Parameters
An actual parameter is the value of the formal parameter. In the example below, the actual parameters are 2 and 3. The values of the actual parameter are passed to the formal parameter. If you refer to the codes above (formal) and the codes below (actual), 2 is passed to the formal parameter num1, while 3 is passed to the formal parameter num2.


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



Polymorphism
Polymorphism originated from the Greek word poly which means many, and morph which means form. Therefore, polymorphism means many forms. In programming, polymorphism is a variable, function, or (commonly) an object that has many forms. The main purpose of polymorphism in programming is for creating one name that can be used for a general class. Method Overriding and Method Overloading is a type of Polymorphism.

For more polymorphism, see next post (Method Overloading and Method Overriding)

Packages (Java)

What is a Package?


Java allows us to group classes in a collection, known as the Package.


A package is a helpful tool in organizing our work as well as separating our work from the libraries of codes made by others.

The standard Java library is known to have distributed over a number of packages, including the following:


  • java.util
  • java.lang
  • java.net
Reason for Using Packages


The purpose of using packages is to guarantee the uniqueness of class names. Two junior programmers, for example, come up with a good idea of supplying a price class. As long as the two programmers place their class in different packages, there is nothing wrong with it.

The sole purpose of package nesting is to manage their unique names. From the point of view of the complier, there is absolutely no relationship between nested packages. Similar to the packages java.util and java.util.jar both have nothing to do with each other since each has its own independent collection of classes.


Using Packages


A class can use all classes from its own package, including all public classes from other packages. There are indeed two ways to access public class, and those are the following:

1. Add the full package name in front of every class name

java.util.Date today = new java.util.Date();


2. The import keyword

import java.io.*;



The IMPORT keyword


The import statement gives you a shorthand to refer classes in the package. Once you use it, yo no longer have to give the classes their full names.

The * symbol is only used to import a single package. It makes you import the whole package itself.

Extended Search

Custom Search