Lab No. 5:: Method Overloading

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 6

Faculty of Computing and Information Technology (FCIT)

Department of Computing Indus University, Karachi

NAME OF STUDENT: ___________________ID No: ____________

Lab No. 5: Method Overloading


Objective:
 Defining constructors
 Understanding method / constructor overloading.
 Creating and using packages
Scope:
Overloading means to have more than one constructors or methods having same name. There are more
than one way to discriminate between methods, including name and number and type of parameters. To
see how parameters can help in overloading, consider the following examples.
Packaging is a way to organize files when a project consists of multiple modules. It also helps resolve
naming conflicts when different packages have classes with the same names.
Packages access level also allows you to protect data from being used by the non-authorized classes.

Example 1: Write a Java Program to define a class, describe its constructor, overload the Constructors and
instantiate its object

Object Oriented Programming


Faculty of Computing and Information Technology (FCIT)
Department of Computing Indus University, Karachi

NAME OF STUDENT: ___________________ID No: ____________

Example 2: Write a Java Program to define a class, define instance methods and overload them and use
them for dynamic method invocation.

Object Oriented Programming using Lab Manual


JAVA
Faculty of Computing and Information Technology (FCIT)
Department of Computing Indus University, Karachi

NAME OF STUDENT: ___________________ID No: ____________

Example 3: Implementation of constructor overloading

Example 4: This code generates error message.

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

NAME OF STUDENT: ___________________ID No: ____________

 public void setBase(double x);//Sets base length


 public double getBase();//Gets base length
 public double getArea();//Returns the area of the triangle

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.

Creating and using Packages:

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.

Object Oriented Programming using Lab Manual


JAVA
Faculty of Computing and Information Technology (FCIT)
Department of Computing Indus University, Karachi

NAME OF STUDENT: ___________________ID No: ____________

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.

Object Oriented Programming using Lab Manual


JAVA
Faculty of Computing and Information Technology (FCIT)
Department of Computing Indus University, Karachi

NAME OF STUDENT: ___________________ID No: ____________

Object Oriented Programming using Lab Manual


JAVA

You might also like