Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
43 views

W9-Presentation-Basic Java Object Oriented Programming

This document provides an introduction to basic object-oriented programming concepts in Java including classes, objects, encapsulation, inheritance, polymorphism, and casting. It defines classes as templates for objects, and objects as instances of classes with state (attributes) and behavior (methods). It discusses encapsulation as a way to hide implementation details. It also covers creating objects from classes using the new operator, casting between types, and comparing objects using equals().
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
43 views

W9-Presentation-Basic Java Object Oriented Programming

This document provides an introduction to basic object-oriented programming concepts in Java including classes, objects, encapsulation, inheritance, polymorphism, and casting. It defines classes as templates for objects, and objects as instances of classes with state (attributes) and behavior (methods). It discusses encapsulation as a way to hide implementation details. It also covers creating objects from classes using the new operator, casting between types, and comparing objects using equals().
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 24

Basic Java Object Oriented

Programming
Introduction to Object Oriented
Programming
- it revolves around the concept of objects as the basic
elements of your programs.

Please refer to the table below for the examples.


Object State Behavior

Dog  Name  Barking


 Color  Fetching
 Hungry  Wagging Tail

Bicycle  Current Gear  Changing Gear


 Current Pedal Cadence  Changing Pedal Cadence
 Current Speed  Accelerating
Classes and Objects

Object is a software component whose structure is similar to


objects in the real world.
- is composed of a set of data (state/attributes) which are
variables describing the essential characteristics of the object, and it also consists
of a set of methods (behavior) that describes how an object behaves.

Class is the fundamental structure in object-oriented programming.


- It can be thought of as a template, a prototype or a
blueprint of an object.
To differentiate between classes and objects, let us discuss an example.

Car Class Object Car A Object Car B


Plate Number AAA 123 BBB 456
Instance Color Red Black
Variables Manufacturer Honda Toyota
Current Speed 60 km/hr 55 km/hr
Accelerate Method
Instance
Turn Method
Methods
Brake Method
Encapsulation

- is the method of hiding certain elements of the


implementation of a certain class.

- By placing a boundary around the properties and methods of


our objects, we can prevent our programs from having side effects wherein
programs have their variables changed in unexpected ways.
Class Variables and Methods
In addition to the instance variables, it is also possible to define class
variables, which are variables that belong to the whole class. This means that it
has the same value for all the objects in the same class. They are also called
static member variables
Car Class Object Car A Object Car B
Plate Number AAA 123 BBB 456
Instance Color Red Black
Variables Manufacturer Honda Toyota
Current Speed 60 km/hr 55 km/hr
Class
Count = 2
Variable
Accelerate Method
Instance
Turn Method
Methods
Brake Method
Class Instantiation
To create an object or an instance of a class, we use the new
operator. There are three steps when creating an object from a class:

• Declaration − A variable declaration with a variable name with an object


type.

• Instantiation − The 'new' keyword is used to create the object.

• Initialization − The 'new' keyword is followed by a call to a constructor.


This call initializes the new object.
For example:
public class Puppy {
public Puppy(String name) {
// This constructor has one parameter, name.
System.out.println("Passed Name is :" + name );
}

public static void main(String []args) {


// Following statement would create an object myPuppy
Puppy myPuppy = new Puppy( "tommy" );
}
}
Casting, Converting and Comparing
Objects
- In this section, we are going to learn how to do typecasting.

Typecasting or casting is the process of converting a data of a certain


data type to another data type.
Casting Primitive Types

- it enables you to convert the value of one data from one type
to another primitive type.

- This commonly occurs between numeric types.


An example of typecasting is when you want to store an integer data to a variable
of data type double. For example,

int varInt = 10;


double varDouble = varInt; //implicit cast
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. For
example,

char varChar = A;
int varInt = varInt; //implicit cast
System.out.println(varInt); //print 65
In case that we want to convert a data that has a large type to a
smaller type, we must use an explicit cast. Explicit casts take the following form:
(Data_Type)value;
Where,
Data_Type, is the output data type that you’re converting.
value, is an expression that results in the value of the source type.
For example,
double varDouble = 10.12;
Int varInt = (int)varDouble; //convert varDouble to int type

double x = 10.2;
Int y = 2;
int result = (int)(x/y); //typecast result of operation to int
Casting Primitive Types

Sample Class Hierarchy


To cast an object to another class, you use the same operation as for
primitive types:

To cast,
(Class_Name)object;
Where,
Class_Name, is the name of the destination class.
value, is a reference to the source object.
For example,

Class Hierarchy for superclass Employee


Converting Primitive Types to Objects and Vice Versa

One thing you can't do under any circumstance is cast from an


object to a primitive data type, or vice versa. Primitive types and objects are
very different things in Java, and you can't automatically cast between the
two or use them interchangeably.

Two classes have names that differ from the corresponding data type:
Character is used for char variables.

Integer for int variables.


Java treats the data types and their class versions very differently,
and a program won't compile successfully if you use one when the other is
expected.
For example,
//The following statement creates an instance of the Integer
// class with the integer value 7801 (primitive -> Object)
Integer dataCount = new Integer(7801);

//The following statement converts an Integer object to


// its primitive data type int. The result is an int with
//value 7801
Int newCount = dataCount.intValue();
// A common translation you need in programs
// is converting a String to a numeric type, such as an int
// Object->primitive
String pennsylvania = "65000";
int penn = Integer.parseInt(pennsylvania);

CAUTION: The void class represents nothing in Java, so there's no reason it would
be used when translating between primitive values and objects. It's a placeholder
for the void keyword, which is used in method definitions to indicate that the method
does not return a value.
Comparing Objects

- The exceptions to this rule are the operators for equality: == (equal)
and != (not equal).

- To compare instances of a class and have meaningful results, you


must implement special methods in your class and call those methods. A good
example of this is the String class.
The following code illustrates this,
class EqualsTest {
public static void main(String[] arguments) {
String str1, str2;
str1 = "Free the bound periodicals.";
str2 = str1;
System.out.println("String1: " + str1);
System.out.println("String2: " + str2);
System.out.println("Same object? " + (str1 == str2));
str2 = new String(str1);
System.out.println("String1: " + str1);
System.out.println("String2: " + str2);
System.out.println("Same object? " + (str1 == str2));
System.out.println("Same value? " + str1.equals(str2));
}
}
This program's output is as follows,

String1: Free the bound periodicals.


String2: Free the bound periodicals.
Same object? true
String1: Free the bound periodicals.
String2: Free the bound periodicals.
Same object? false
Same value? True
This program's output is as follows,

String str1, str2;


str1 = "Free the bound periodicals.";

NOTE: Why can't you just use another literal when you change str2, rather than
using new? String literals are optimized in Java; if you create a string using a
literal and then use another literal with the same characters, Java knows enough
to give you the first String object back. Both strings are the same objects; you
have to go out of your way to create two separate objects.
Determining the Class of an Object
Want to find out what an object's class is? Here's the way to do it for an
object assigned to the variable key:

1. The getClass() method returns a Class object (where Class is itself a class)
that has a method called getName(). In turn, getName() returns a string
representing the name of the class.

For Example,
String name = key.getClass().getName();
2. The instanceOf operator. The instanceOf has two operands: a reference to an
object on the left and a class name on the right. The expression returns true or
false based on whether the object is an instance of the named class or any of that
class's subclasses.

For Example,
boolean ex1 = "Texas" instanceof String; // true
Object pt = new Point(10, 10);
boolean ex2 = pt instanceof String; // false

You might also like