Java Object-Oriented Programming Guide
Java Object-Oriented Programming Guide
Object-Oriented
Booklet
Material developed by
COTI Informatics
Who We Are
The Coti Informática school is now a factory for inserting
students in the job market.
Coti Informatics.
Here the student is treated as a student.
Object Oriented
Lesson 1
Introduction
toJava
A lot is being said these days about Java and the whole revolution.
what he who provoking no way of life of
programmers, analysts and internet users. But what is
Java, after all? What are its origins and purposes?
Where does all this success come from?
Additional Comments
The reason to use Java? Although it was made for the web, it has
robustness to work with 3D, mathematical applications, networks, can create
compilers and even local applications like java swing.
Important Knowledge
To use Java, it is first necessary to install the JDK, and then the IDE that you will use.
for example Netbeans or Eclipse.
The moment I install the jdk (which is the compiler, it also installs the virtual machine
Java JVM, which is its interpreter, runs the .class files. The JVM is independent.
platform because it sits on a layer above the OS, as it does not affect the System
Any platform can run it (Windows, Linux, Mac).
The IDE is used to assist in programming, it has code compilation within it.
line when the data is entered showing the error or not, has the code test, and
it has a debugger to check where and how the error occurred.
3
COTI COMPUTER SCIENCE E-LEARNING
Java Object-Oriented Logic Workbook
• Java is very large, for learning due to its branches and frameworks.
constant developments.
• FrameWorks (These are programs that will help you put together a project using a)
pattern pronto como Struts que irá trabalhar bem com o MVC
• The most widely used IDE in the world is Eclipse, although Netbeans has thousands of.
followers just like me Professor Belém from Coti Informatics.
• The job market makes extensive use of Java, so a great programmer will not be left behind.
never unemployed (we are in the year 2010).
A JVM can be implemented for any platform, thus we have that the same program
Java can be run on any architecture that has a JVM.
4
COTI COMPUTER SCIENCE E-LEARNING
Workbook on Object-Oriented Logic in Java
Building your
first application
Java in the NetBeans IDE
First you should install the JDK (Java Development Kit) and then the IDE.
Integrated Development Environment you wish to use to develop your programs
Put the name of the project as Example, then select the location that
Do you want to save the project.
Also uncheck the option to create the main class, as this will be done in
followed in the next steps.
Your project will be displayed on the left side, the directory you will use to create your
source code files (Classes) are called 'Source Code Packages'.
COTI IT E-LEARNING 6
Java Object-Oriented Logic Notebook
Define two attributes in the Class (for testing only, as the concepts of Object Orientation
Objects will be exemplified further on.
package class;
7
COTI COMPUTER SCIENCE E-LEARNING
Java Object-Oriented Logic Workbook
Later, the use of this method will be discussed in more depth, for now,
just create it as follows:
To make the Student Class become an object, type the code below:
Through the created object 'a', we can use the attributes defined in the Student Class.
example assign value to these:
To print the values of the Class, the command [Link]() was used in
method printData(). To run the program, right-click and then
Run File
Ready! You are equipped to follow the rest of this material using the IDE
NetBeans
Lesson 2
Mainconceptsof
object-oriented
Java is an object-oriented language and, with that,
it is not possible to develop any program without following
such paradigm.
Classes are programs that have an uppercase name, which accompanies the name of the
file, when the class is public, the class represents the business rule that its name will
mention, for example: If the class is Person, you cannot do mathematical programs
inside the Person class, the name must have cohesion with the programming, within the class
person you must have programs that will represent the name
Attributes
It has two attributes, which are global 'variables' of
a class" all the subprograms (methods) will
visualize the name and the email, they are private,
In other words, other classes will not see them, they are
closed to other classes, but will always be visible to
own class
COTI IT E-LEARNING 9
Java Object-Oriented Logic Workbook
Methods
They are subprograms of the classes, setName(parameter), getName(), setEmail(parameter),
getEmail(). Every method has parentheses, always emphasize this.
Notice that in the set method it is always created with void, which means no return, and the
method getNome() it was created with the type of attribute the name was created which was String.
Attributes (Variables)
Qualifiers
Qualifiers can be (public, protected, private, default (blank))
Commentary is represented by //
Example:
char letter;
The letter is of the closed default type in the package.
10
COTI COMPUTER SCIENCE E-LEARNING
Workbook of Logic in Object-Oriented Java
Identifiers
First the primitives:
Integers
byte - from -2 raised to 7 to 2 raised to 7 - 1 (Example -128 to 127)
short - less than 2 raised to the power of 15 up to 2 raised to the power of 15 - 1
Reais or floating
float - minus 2 raised to 31 to 2 raised to 31
double -2 raised to 63 to 2 raised to 63
Text
String represents any type of character, it is not a primitive type because
it is a class that comes from the [Link] package; which is already internal to java
Boolean
boolean- represents true or false
Global Type
Object - Class represents any type of data, can be number, String,
data, any type of desired class.
Operators
COTI IT E-LEARNING 11
Java Object-Oriented Logic Workbook
Observation:
Note that...
The getNome() method has been declared as String because it will return the attribute
Name that was declared as String, the getter is of the same type as the Attribute, it
represents the attribute itself (It is the Output method, the return)
Example:
[Link] = parameter;
class attribute name receives the parameter.
It is the method that starts the execution of the Java program. It calls all other methods of
other classes. It is the method that is initially seen by Java Local and it is suggested that it be the
the first method to be looked for when reading a Java program, just like the 'Main' class
It should be the first class to be sought.
COTI IT E-LEARNING 12
Java Object-Oriented Logic Workbook
[Link]("Belem");
[Link]("belem@[Link]");
[Link]([Link]());
[Link]([Link]());
}
}
object-oriented model.
It's as if the 'main' were a boss. The boss can perform tasks, but it shouldn't be done by those who
You must make your subordinates, in this case, the methods of other classes.
Remember that Java is 'case sensitive', meaning that the distinction between uppercase and lowercase letters matters.
Difference. "Main" and "main" are distinct things, the first is the main class and the second is a method.
principal.
Iunderstandbetter.
osobjetos
An object is like a clone or a copy of the class. In the reference program, see the
Following instructions: Person p = new Person(). Onew(new, refers to a new
based on the Person class. All you need to understand is that it is like a copy
from the Person class and therefore have all the attributes and methods derived from the Person class.
Let's suppose a book that has blank spaces to be filled in. No one is going to use it.
the original book also because making an original book takes a lot of work, for this reason, it goes to print the
The original book written directly by the author is the class, the copy is the object.
If the book is a copy, as is the case, it has blank spaces to be filled in by the students,
each copy may take on a different form, as different students will fill in the
blank spaces (as if each copy assumes a new state) and each student will do what
You can read with your copy, as long as the original book offers this resource, you can read only
the chapters that interest you and do the exercises that are available that the teacher
sent. Likewise, the object has an original form coming from the class, but it can be
used as desired by the user.
The most accurate definition of an object is: the memory location where it is stored.
State of the Class. However, we understand that this definition is not very assimilable for a
beginner student in Java. If it becomes easier to understand by this definition or by another definition,
make yourself at home.
Lesson 3
Encapsulationand
inheritance
Creating your
first application
Java in IDE Eclipse
[Link]
o/SR2/[Link]
15
COTI COMPUTER SCIENCE E-LEARNING
Java Object-Oriented Logic Workbook
Upon opening the eclipse, you will need to specify which working directory to be used by the IDE.
This directory is called workspace and will store the projects and configurations made.
no Eclipse
To create a new project, select the option file / new / java project
Para criar um novo pacote, clique com o direito em src e depois em new / package coloque o
class name
16
COTI INFORMATION TECHNOLOGY E-LEARNING
Java Object-Oriented Logic Workbook
To encapsulate the attributes, right-click on the class then go to source / generate getters
and setters. Check all the attributes that will be encapsulated and click ok
packageclass;
public classPessoa {
privateString name;
private intidade;
To create the Main Class, give a right over the package class of after new / class
When creating the Class, you can already check the option to include the static void main method as
below:
18
COTI COMPUTER SCIENCE E-LEARNING
Java Object-Oriented Logic Handbook
packageclass;
[Link]([Link]());
[Link]([Link]());
}
}
The solution is to create methods that receive the values of the variables or send values to
variables and just work with them. In addition, methods can be handled and modified
without fundamentally changing the properties of the variable. The methods begin to represent
the attributes as if they had a "power of attorney" from them. Two types of methods are created to
the encapsulation one that starts with 'get' which would be responsible for displaying the values of
attribute and 'set' would be responsible for receiving new values and passing them to the attribute.
19
COTI COMPUTER SCIENCE E-LEARNING
Java Object-Oriented Logic Workbook
The main objective of encapsulation is to protect the attributes of a Class, that is, access.
the attributes should be done indirectly through methods that perform the input and
output of data for the attributes.
By turning the class into an object, its attributes (declared as public) become accessible and
therefore, without any type of protection against any type of invalid data. Note:
Note that there is no coherence in the data passed to the Class attribute.
Here is the example of the same Class in an encapsulated form:
Input methods
public void setName(String name){
[Link] = nome;
}
20
COTI COMPUTER SCIENCE E-LEARNING
Java Object-Oriented Logic Handbook
Important notes!
The methods declared with the set prefix are used to perform data input into the
attributes in such a way that their value is assigned indirectly.
The methods declared with the get prefix are used to return or output the
attribute values.
In this way, any type of data input or output validation rule could
can be easily implemented in the set and get methods, thereby making the attributes protected and
ensuring the integrity and consistency of the information stored by the object of the class.
[Link]([Link]());
[Link]([Link]());
21
COTI COMPUTER SCIENCE E-LEARNING
Java Object-Oriented Logic Workbook
Inheritance
Department has Employee (In this case, it does not constitute inheritance, the demonstration of
relationships that use the verb "to have" will be shown in another class.
Inheritance is the first and most common type of relationship existing between classes. We can
represent it as follows:
The representation of these classes in the form of a diagram is in accordance with a tree, where
We can assert that Employee is a superclass and Analyst and Programmer are
subclasses denote the concept of hierarchy (vertical relationship)
Note: In Java, multiple inheritance is not allowed, that is, a child class
can only have 1 single parent class.
[Link]("Pedro");
[Link](4000.0);
[Link]("Senior");
[Link]("Joao");
[Link](5000.0);
[Link]("Web");
Note that the previous program creates objects for Analyst and
Programmer. Through inheritance, both share the same data (name and
salary) as they are inherited from the same class: Employee.
24
COTI COMPUTER SCIENCE E-LEARNING
Java Object-Oriented Logic Handbook
[Link]("Sergio Mendes");
[Link]("[Link]@[Link]");
[Link]("1234567890");
Individual person......:
Email..............:
[Link]("CPF................: " + [Link]());
To practice ...
Develop the classes according to the diagram below and
demonstrate your execution in the Main
26
COTI COMPUTER SCIENCE E-LEARNING
Workbook of Logic in Object-Oriented Java
Lesson 4
Interfacesand
Polymorphism
The word Polymorphism comes from Greek and means many
forms. In programming, it is related to the technique of
put different methods with the same name for
to perform similar tasks. In this lesson we will learn
to effectively use this concept!
For example:
Note that methods declared in interfaces do not have a body, only their
declaration or signature.
27
COTI COMPUTER SCIENCE E-LEARNING
Java Object-Oriented Logic Workbook
Note that when the classes implement the interface, the method calculateTax becomes
if necessary. Thus, the interface has become a means of standardizing the use of
productAndServiceClasses
Through the use of interfaces, the execution of the program can be done in a way
more flexible, for example:
/*
All classes that implement this interface must provide
body for the method calculateFinancing
*/
}
The term polymorphism comes from Greek and means many forms.
programming is related to the technique of placing methods
different ones with the same name to perform similar tasks
in principle (if poorly programmed they can do things
completely different).
To practice...
Demonstrate the execution of the Rectangle and Triangle classes in the main.
Let's go
Practice
1. Crie um programa em Java que demonstre o uso de uma Classe Produto com
the attributes name, brand, and price. Use encapsulation for the attributes.
4. Explain the difference between the qualifiers private, protected, package, and
public.
COTI IT E-LEARNING 31