You are on page 1of 44

Lecture 5

Method Overloading
What is method overloading?
What is method overloading?

Methods within the same class that have the same


name but slightly different characteristics(signature)
like different number of parameters, different type of
parameters that perform related operations are called
overloaded methods
Example…
Is called from
When the following function called from main…
Careful with type Casting in Java…

The rule of Automatic type casting in Java says that you


can easily convert something small to something
bigger, but when you want to convert something big to
something smaller there is a chances that you might
lose some data.
Method overloading
Ambiguous Overloading
Example of Method Overloaded

public class Mainjava{


public static void main(String[] args)
{
Maths maths=new Maths();
int no=maths.max(5,6);
system.out.println(no)
}
}
class Maths
{
public int max(int x, int y)
{
int max=0;
if(x>y)
{
max=x;
}
else
{
max=y;
}
system.out.println("Integer Version is called")
return max;
}
public double max(double x, double y)
{
double max=0;
if(x>y)
{
max=x;
}
else
{
max=y;
}
system.out.println("double version is called")
return max;
}
Lecture 6

Object and Class in Java


What is an Object?

Concept:
An object is a software component that exists in
memory and serves a specific purpose in a
program. An object is created from a class
that contains code describing the object.
Details…
If you have ever driven a car, you know that a car consists of a
lot of components. It has a steering wheel, an accelerator
pedal, a brake pedal, a gear shifter, a speedometer, and
numerous other devices that the driver interacts with. There
are also a lot of components under the hood, such as the
engine, the battery, the radiator, and so forth. So, a car is not
just one single object, but rather a collection of objects that
work together.
This same notion applies to computer programming as well.
Most programming languages in use today are
object-oriented. With an object oriented language, such as
Java, you create programs that are made of objects. In
programming, however, an object isn’t a physical device, like a
steering wheel or a brake pedal; it’s a software component
that exists in the computer’s memory and performs a specific
task.
In software, an object has two general capabilities:
1. An object can store data. The data stored in an
object are commonly called fields.
2. An object can perform operations. The operations
that an object can perform are called methods.
What is an Object?
An object is any real world thing that you want
to represent in a program with 2 parts:
1. Something it has properties
2. Something it does actions
Example: you an object
Note: An object is combination of your properties and
your action, when we are talking about you as a
person.
An Object
More on Object…
We have a Mobile and we want to represent it
in Java how can we represent it?

Very simple just ask 2 questions:

1. What it has(Properties)?
2. what it does(Action)?
How we represent this phone in java?
Example..
What is a Class?
What is a class
A class is the representation of common properties and
functions of an object.
Ex: int x= 25,
25 is the value, x is the name of the variable and int is
the type.
Person you, you is the name of the object and
person(class) is the type.
Class is the type of an object
More on Class
Class implementation…
Find an Area of Circle using object and class
Class vs Object
Important…
Creating an Object Circle
Example…
public class Cube{
public static void main(String [] args){
Circle c = new Circle(); ---> creating the object
system.out.println(c.radius);
}
}

Class Circle Class Circle has been define here…


{
double radius;
double area;
}
Java code to compute the area of Circle

public class Circle{

public static void main(String [] args){


Circle c = new Circle();

c.radius=3.5;
c.area= 3.14*c.radius*c.radius;

system.out.println(c.area);
}
}
Class Circle ---> We define the class here…
{
double radius;
double area;
}
More Example…
public class Cube{

public static void main(String [] args){


Circle c = new Circle();

c.radius=3.5;
c.arear= 3.14*c.radius*c.radius;
system.out.println(c.area);

Circle c2 = new Circle();

c2.radius=4.5;
c2.arear= 3.14*c2.radius*c2.radius;
system.out.println(c.2area);

}
}
Class Circle
{
double radius;
double area;
}
Better Approach…
public class Cube{

public static void main(String [] args){

Circle c = new Circle(); creating the Object c


c.findArea(3.1) calling the Method findArea()

Circle c2 = new Circle(); creating the object c2


c2.findArea(3.1) calling the method findArea()

}
}
Class Circle
{
double radius;
double area;
public void findArea(double r)
{
radius=r;
area= radius*radius*3.14;
system.out.println(area);
}}
Note:
The findArea is defined inside Class Circle. It means it
does the same operation of finding the area but
different Objects use it by giving different values and
getting different results.
Questions?
Problem
What we need to do…
Create the class
Public calss DrivingTest{
public static void main(String [] args)
{

Car myCar=new Car(); object Car


myCar.fuelCapacity=20;
myCar.amountOfFuel=12;
myCar.addGas(2);

}
Create Class car
class Car{

double mileage;
double amountOfFuel;// 10
double fuelCapacity;// 15

public void drive()


{

}
public void addGas()
{

}
public void addGas(double amount)
{
double emptySpace=fuelCapacity-amountOfFuel;//5
if(amount<emptySpace)
{
amountOfFuel=amountOfFuel+amount;
System.out.println(amount +"liters was added successfully" );
}
else
{
amountOfFuel=fuelCapacity;// 15

System.out.println("Now your Tank is full" );


}
}

You might also like