You are on page 1of 17

Object-Oriented

Programming
Lecture 2
Prepared by: Ms. Roda Flor Andrea B. Teodocio
Reference: www.udacity.com
Outline
Integrated Development Environment (IDE)
01

Self-Reference
02

Objects and Classes


03

Constructors
04
Integrated Development
Environment
What is an IDE?

To be able to create and run any code in Java (and pretty much any
programming language), you will need 2 main things:

• A helpful text editor that highlights keywords with different colors and autocompletes code.
• A compiler that converts your Java code into computer code (known as bytecode) that can be
understood by computers and hence run properly.

An IDE (which stands for Integrated Development Environment) combines


both of those amongst other features like highlighting errors and potential
bugs.

An IDE will help you power through creating any project in almost any
programming language.
Choosing an IDE?

There are plenty of options out there, and


choosing one is usually based the programming
language you're using as well as your personal
preference. Here's a list of the most commonly
used Java IDEs:
• IntelliJ
• NetBeans
• Eclipse
• Android Studio (based on IntelliJ)
Objects & Classes
Classes and Objects are two different terms and should not be used interchangeably.
String
Everything is an object in Java
The main method
Constructors
Constructors are special types of methods that are responsible for creating and initializing
an object of that class.
Self Reference

Sometimes you'll need to refer to an object within one


of its methods or constructors, to do so you can use the
keyword this.
this is a reference to the current object — the object
whose method or constructor is being called.

You can refer to any field of the current object from


within a method or a constructor by using this.
Self Reference (cont’d)
Using this with a Field
Access Modifiers
Public VS Private
Remember
Always try to declare all fields as private

Create a constructor that accepts those private fields as inputs

Create a public method that sets each private field, this way you
will know when you are changing a field. These methods are called setters

Create a public method that returns each private field, so you can read the
value without mistakingly changing it. These methods are called getters
Methods
Public VS Private

With methods, it's common to have a mix of private and public


methods.

The private methods are usually known as helper methods, since they
can only be seen and called by the same class, they are usually there
to organize your code and keep it simple and more readable.

The public methods are the actual actions that the class can perform
and are pretty much what the rest of the program can see and call.
Methods
Public VS Private

The class Person has both its fields set


to private because if they were public then any
other class will be able to change such sensitive
information.
The method getId() was also set to private so that
no other class can know the social security number
of any person.

However, we were still able to use that method


internally when comparing this person with another
person object in the isSamePerson(Person
p) method.
This means that any other class can only
call getUserName or isSamePerson and will seem
as if these are the only 2 methods provided by the
class Person
Remember
To summarize, it's recommended to:

Set all your classes to public

Set all your fields to private

Set any of your methods to public that are


considered actions
Set any of your methods to private that are
considered helper methods

You might also like