Object-Oriented Programming
• Object
• Class
• Inheritance
• Polymorphism
• Abstraction
• Encapsulation
1-1
Objects
• Any entity that has state and behavior is known
as an object. For example, a chair, pen, table,
keyboard, bike, etc. It can be physical or logical.
• An Object can be defined as an instance of a
class. An object contains an address and takes
up some space in memory.
• Objects can communicate without knowing the
details of each other's data or code. The only
necessary thing is the type of message accepted
and the type of response returned by the
objects.
• Example: A dog is an object because it has
states like color, name, breed, etc. as well as
behaviors like wagging the tail, barking, eating,
etc.
1-2
Classes
• Collection of objects is called class. It is a logical entity.
• A class can also be defined as a blueprint from which
you can create an individual object. Class doesn't
consume any space.
1-3
Inheritance
• When one object acquires all the properties and
behaviors of a parent object, it is known as inheritance.
• It provides code reusability. It is used to achieve
runtime polymorphism
1-4
Polymorphism
• If one task is performed in different ways, it is
known as polymorphism. For example: to convince
the customer differently, to draw something, for
example, shape, triangle, rectangle, etc.
• In Java, we use method overloading and method
overriding to achieve polymorphism.
• Another example can be to speak something; for
example, a cat speaks meow, dog barks woof, etc
1-5
Abstraction
• Hiding internal details and showing
functionality is known as abstraction. For
example phone call, we don't know the
internal processing.
• In Java, we use abstract class and interface
to achieve abstraction.
1-6
Encapsulation
• Binding (or wrapping) code and data
together into a single unit are known as
encapsulation. For example, a capsule, it is
wrapped with different medicines.
• A java class is the example of
encapsulation. Java bean is the fully
encapsulated class because all the data
members are private here.
1-7
Character Strings
• A string of characters can be represented as a
string literal by putting double quotes around
the text:
• Examples:
"This is a string literal."
"123 Main Street"
"X"
• Every character string is an object in Java,
defined by the String class 1-8
The println Method
• In the sample program from Chapter 1, we
invoked the println method to print a
character string
• The [Link] object represents a
destination (the monitor screen) to which
[Link] ("Whatever you are, be a good one.");
we can send output
object method
information provided to the method
name
(parameters)
1-9
The print Method
• The [Link] object provides another
service as well
• The print method is similar to the
println method, except that it does not
advance to the next line
• Therefore anything printed after a print
statement will appear on the same line
1-10
[Link]
public class Countdown
{
//-----------------------------------------------------------------
// Prints two lines of output representing a rocket countdown.
//-----------------------------------------------------------------
public static void main (String[] args)
{
[Link] ("Three... ");
[Link] ("Two... ");
[Link] ("One... ");
[Link] ("Zero... ");
[Link] ("Liftoff!"); // appears on first output line
[Link] ("Houston, we have a problem.");
}
}
1-11
String Concatenation
• The string concatenation operator (+) is used
to append one string to the end of another
"Peanut butter " + "and jelly"
• It can also be used to append a number to a
string
• A string literal cannot be broken across two
lines in a program
1-12
[Link]
public class Facts
{
public static void main (String[ ] args)
{
// Strings can be concatenated into one long string
[Link] ("We present the following facts for your "
+ "extracurricular edification:");
[Link] ();
// A string can contain numeric digits
[Link] ("Letters in the Hawaiian alphabet: 12");
// A numeric value can be concatenated to a string
[Link] ("Dialing code for Antarctica: " + 672);
[Link] ("Year in which Leonardo da Vinci invented "
+ "the parachute: " + 1515);
[Link] ("Speed of ketchup: " + 40 + " km per year");
}
}
1-13
String Concatenation
• The + operator is also used for arithmetic
addition
• The function that it performs depends on the
type of the information on which it operates
• If both operands are strings, or if one is a
string and one is a number, it performs string
concatenation
• If both operands are numeric, it adds them
1-14
[Link]
public class Addition
{
//-----------------------------------------------------------------
// Concatenates and adds two numbers and prints the results.
//-----------------------------------------------------------------
public static void main (String[] args)
{
[Link] ("24 and 45 concatenated: " + 24 + 45);
[Link] ("24 and 45 added: " + (24 + 45));
}
}
1-15
Variables
• A variable is a name for a location in
memory
• A variable must be declared by specifying
the variable's
data type name andname
variable the type of
information that it will hold
int total;
int count, temp, result;
Multiple variables can be created in one declaration
1-16
Variable Initialization
• A variable can be given an initial value in the
declaration
int sum = 0;
int base = 32, max = 149;
• When a variable is referenced in a program, its
current value is used
1-17
[Link]
public class PianoKeys
{
//-----------------------------------------------------------------
// Prints the number of keys on a piano.
//-----------------------------------------------------------------
public static void main (String[] args)
{
int keys = 88;
[Link] ("A piano has " + keys + " keys.");
}
}
1-18
Assignment
• An assignment statement changes the value
of a variable
• The assignmenttotal
operator
= 55;
is the = sign
• The expression on the right is evaluated and the
result is stored in the variable on the left
• The value that was in total is overwritten
• You can only assign a value to a variable that is
consistent with the variable's declared type
1-19
[Link]
public class Geometry
{
public static void main (String[] args)
{
int sides = 7; // declaration with initialization
[Link] ("A heptagon has " + sides + " sides.");
sides = 10; // assignment statement
[Link] ("A decagon has " + sides + " sides.");
sides = 12;
[Link] ("A dodecagon has " + sides + " sides.");
}
}
1-20