Professional Documents
Culture Documents
Lab No. 5:: Method Overloading
Lab No. 5:: Method Overloading
Lab No. 5:: Method Overloading
Example 1: Write a Java Program to define a class, describe its constructor, overload the Constructors and
instantiate its object
Example 2: Write a Java Program to define a class, define instance methods and overload them and use
them for dynamic method invocation.
To better understand classes and objects, lets analyze a well-known statement we've been using repeated-
ly:
System.out.println("Hello World");
System is a name of a class, inside System class, there is a public and static member (object) of class
PrintWriter, the name of this object is out. Inside PrintWriter class, there is a public, non-static method
called println that has several overloads.
Exercises:
Exercise 1:
Write a class that represents triangle named Triangle, the class must have the following-
members:
private double height;//Height
private double base;//Base length
public Triangle(double h, double b);//Constructor
public void setHeight(double x);//Sets height
public double getHeight();//Gets height
Object Oriented Programming using Lab Manual
JAVA
Faculty of Computing and Information Technology (FCIT)
Department of Computing Indus University, Karachi
Exercise 2:
Write a class Employee that represents an employee of some organization, the class should con-
tain the following members:
private int id;//Employee id
private String name;//Employee name
private int type;//1 = employee, 2 = manager
private double baseSalary;//Base salary
public Employee(int _id, String _name);//Constructor
public void setID(int x);//id mutator
public void setName(int x);//name mutator
public intgetID();//id accessor
public String getName();//name accessor
public double getSalary();
//if manager, add 10% to base salary
public void setBaseSalary(double bs);//sets base salary.
A package is the collection of interfaces and classes. It is an important aspect one should be aware of,
packaging means putting two or more classes/interfaces in the same package by stating package name
before declaring class (and also exceptions). When two classes are in the same package, it is possible to
access any one of them from the other, for example, if we have a class named Student that represents a
student, another class named Main to run a program that manages student information, we can access
Student class from Main class and define several objects of it if we use the following statement before
each class declaration:
packagestudentinfo;
In NetBeans IDE, there is no need to manually write the above statement, by adding new class toany
package of your project, that class is automatically declared as a part of that package. Following Figure-
shows packages and classes in NetBeans IDE project explorer.
To access a class from its package we simply provide package name followed by dot '.' then
de- sired class name. For example, we can access Main class show in above Figure by calling it
in the following statement:
linkedlist.Main
Example 5: Write a Java program to implement the concept of importing classes from user defined pack-
age.