Professional Documents
Culture Documents
IT103 Chapter 2 - Classes, Methods and Typecasting
IT103 Chapter 2 - Classes, Methods and Typecasting
CHAPTER 2
Why design?
A programmer who quickly throws together some code without considering design
criteria might justify his actions by saying that he knows what is going on in the code and so
there is no need to worry about a misinterpretation. Furthermore, he might say, the code is
never going to be used again or viewed by anyone else so why devote time needlessly to
design.
In the case of small “quick-and-dirty” programs written by one person and used only
once, such as short shell scripts, the programmer is correct. It is usually not productive to spend
a lot of time creating the most elegant possible version. All that is important is that the code
work correctly. However, you also need to be aware that the code that the programmer thought
of as “throwaway” is often not thrown away. It ends up copied and pasted into another program,
or it becomes the core of a more general, more complex program. In such cases, time devoted
to make the design elegant would be time productively spent.
3) Robustness – will it deal with unusual situations gracefully and avoid crashing?
5) Scalability – will it still perform correctly and efficiently when the problems grow in
size by several orders of magnitude?
9) Maintainability – can defects be found and fixed easily without adding new
defects?
Prepared by: AHSubang Saint Louis College- Information Technology Department Page 1
[COMPUTER PROGRAMMING 2] IT 103
it supports:
classes
objects
messages
methods and procedures
inheritance
polymorphism
For example, a car object has the properties, type of transmission, manufacturer and
color. Its behaviors are turning, braking and accelerating. Similarly, we can define different
properties and behavior of a lion. Please refer to the table below for the examples.
With these descriptions, the objects in the physical world can easily be modeled as
software objects using the properties as data and the behaviors as methods. These data and
methods could even be used in programming games or interactive software to simulate the real-
world objects! An example would be a car software object in a racing game or a lion software
object in an educational interactive software zoo for kids.
Prepared by: AHSubang Saint Louis College- Information Technology Department Page 2
[COMPUTER PROGRAMMING 2] IT 103
To differentiate between classes and objects, let us discuss an example. What we have
here is a Car Class which can be used to define several Car Objects. In the table shown below,
Car A and Car B are objects of the Car class. The class has fields plate number, color,
manufacturer, and current speed which are filled-up with corresponding values in objects Car A
and Car B. The Car has also some methods Accelerate, Turn and Brake.
Accelerate Method
Instance Methods Turn Method
Brake Method
When instantiated, each object gets a fresh set of state variables. However, the method
implementations are shared among objects of the same class.
Classes provide the benefit of reusability. Software programmers can use a class over
and over again to create many objects.
Prepared by: AHSubang Saint Louis College- Information Technology Department Page 3
[COMPUTER PROGRAMMING 2] IT 103
Classes
In the real world, you'll often find many individual objects all of the same kind. A class is
the blueprint from which individual objects are created. (Example: There may be thousands of
other bicycles in existence, all of the same make and model. Each bicycle was built from the
same set of blueprints and therefore contains the same components. In object-oriented terms,
we say that your bicycle is an instance of the class of objects known as bicycles.)
Encapsulation
Imagine how many parts exist in a car and how many functions those parts can perform.
Does the driver need to know about all of them? Of course not. The driver needs to know how to
start and stop the car, how to indicate turns, open the windows, turn on the wipers, and do a few
dozen other simple operations, which in programming jargon can be called the car's public
interface.
We can prevent access to our object's data by declaring them in a certain way such that
we can control access to them. We will learn more about how Java implements encapsulation
as we discuss more about classes.
Access Levels
Java classes, methods, and member variables can have public, private, protected and
package access levels.
The keyword public means that this element (a class, a method, or a variable) can be
accessed from any other Java class.
The keyword protected makes the element “visible” not only from the current class, but
from its subclasses too.
The keyword private is the most restrictive one, as it makes a member variable or a
method accessible only inside this class.
If you do not specify any access level, the default is package, which means that only
classes located in the same package will have access to this method or variable. Java classes
should expose only the methods that outsiders have to know.
Tip: If you are not sure which access level to give to methods or variables, just make them all
private; if later on during development some other class needs to access them, you can always
change the access level to be less restrictive. This will protect all the internals of your
application from misuse.
Prepared by: AHSubang Saint Louis College- Information Technology Department Page 4
[COMPUTER PROGRAMMING 2] IT 103
Class Instantiation
To create an object or an instance of a class, we use the new operator. For example, if
you want to create an instance of the class String, we write the following code,
The new operator allocates a memory for that object and returns a reference of that
memory location to you. When you create an object, you actually invoke the class' constructor.
The constructor is a method where you place all the initializations, it has the same name as the
class.
Methods
In the examples we discussed before, we only have one method, and that is the main()
method. In Java, we can define many methods which we can call from different methods.
A method is a separate piece of code that can be called by a main program or any other
method to perform some specific function.
After the method has finished execution, it goes back to the method that called it.
Now, why do we need to create methods? Why don't we just place all the code inside
one big method? The heart of effective problem solving is in problem decomposition. We can do
this in Java by creating methods to solve a specific part of the problem. Taking a problem and
breaking it into small, manageable pieces is critical to writing large programs.
nameOfObject.nameOfMethod( parameters );
Prepared by: AHSubang Saint Louis College- Information Technology Department Page 5
[COMPUTER PROGRAMMING 2] IT 103
There are two types of passing data to methods, one is Pass-by-value and the other is
pass-by-reference.
Pass-by-value
When a pass-by-value occurs, the method makes a copy of the value of the
variable passed to the method. The method cannot accidentally modify the original argument
even if it modifies the parameters during calculations.
In the given example, we called the method test and passed the value of i as parameter.
The value of i is copied to the variable of the method j. Since j is the variable changed in the test
method, it will not affect the variable value if i in main since it is a different copy of the variable.
By default, all primitive data types when passed to a method are pass-by-value.
Pass-by-reference
Typecasting
Casting between primitive types enables you to convert the value of one data from one
type to another primitive type. This commonly occurs between numeric types.
There is one primitive data type that we cannot do casting though, and that is the
boolean data type.
An example of typecasting is when you want to store an integer data to a variable of data
type double. For example,
In this example, since the destination variable (double) holds a larger value than what we
will place inside it, the data is implicitly casted to data type double.
Prepared by: AHSubang Saint Louis College- Information Technology Department Page 6
[COMPUTER PROGRAMMING 2] IT 103
Another example is when we want to typecast an int to a char value or vice versa. A
character can be used as an int because each character has a corresponding numeric code that
represents its position in the character set. If the variable i has the value 65, the cast (char)i
produces the character value 'A'. The numeric code associated with a capital A is 65, according
to the ASCII character set, and Java adopted this as part of its character support. For example,
When we convert a data that has a large type to a smaller type, we must use an explicit
cast. Explicit casts take the following form:
(dataType)value
where,
For example,
double x = 10.2;
int y = 2;
Prepared by: AHSubang Saint Louis College- Information Technology Department Page 7
[COMPUTER PROGRAMMING 2] IT 103
Prepared by: AHSubang Saint Louis College- Information Technology Department Page 8