Professional Documents
Culture Documents
Method Overloading
What is method overloading?
What is method overloading?
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?
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);
}
}
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{
c.radius=3.5;
c.arear= 3.14*c.radius*c.radius;
system.out.println(c.area);
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{
}
}
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)
{
}
Create Class car
class Car{
double mileage;
double amountOfFuel;// 10
double fuelCapacity;// 15
}
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