0% found this document useful (0 votes)
15 views4 pages

Class: Attributes Properties Methods Functions

OOP

Uploaded by

dennielbarc
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views4 pages

Class: Attributes Properties Methods Functions

OOP

Uploaded by

dennielbarc
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

What is Object-Oriented Programming?

Object-Oriented Programming (OOP) is a programming approach where you organize your


code around "objects", which represent real-world things.

These objects contain:

 Data (called attributes or properties)


 Actions (called methods or functions)

OOP is a way of writing instructions for the computer by using things called objects — like
toys, pets, or people.

Class

A class is like a blueprint or template.


It defines the structure and behavior (properties and methods) that the objects created from the
class will have.

Think of a class as a recipe for cooking a dish.

 The recipe tells you what ingredients you need (just like a class defines the variables).
 It also tells you how to prepare the dish (just like a class defines the methods or actions).

But the recipe itself is not the actual food — it's just instructions or a guide.

Object

An object is an actual instance of a class.


When you create an object from a class, you're making a real thing based on that blueprint.

If the class is the blueprint, then the object is the actual house built from it.
You can build many houses (objects) from one blueprint (class), and each house can be a bit
different (for example, painted different colors or decorated differently).

Encapsulation

- Means hiding the internal details of how something works, and only showing what’s
necessary.
It keeps the data (variables) and the methods (functions) that work on the data together
in one unit (the object), and it protects the data from being changed directly.

Why Encapsulation is Important:

 Keeps your data safe.


 Prevents accidental changes to data.
 Makes code easier to manage and more secure.
To create a class, use the keyword class: Create a class named “Main” with a
variable x:

Remember that our classname should always start with an uppercase.

In Java, an object is created from a class. We have already created the class
named Main, so now we can use this to create objects.

To create an object of Main, specify the class name, followed by the object name,
and use the keyword new:

Create an object called “myObj” and print the value of x:

You can also create two or more objects of Main.

You can also create an object of a class and access it in another class. This is
often used for better organization of classes (one class has all the attributes and
methods, while the other class holds the main() method (code to be executed)).

Remember that the name of the java file should match the class name. In this
example, we have created two files in the same directory/folder:

 Main.java
 Second.java
Java Class Attributes
In the previous chapter, we used the term "variable" for x in the example (as
shown below). It is actually an attribute of the class. Or you could say that
class attributes are variables within a class:

Cretae a class called “Main” with two attributes: x and y:

Accessing Attributes
You can access attributes by creating an object of the class, and by using the
dot syntax (.):

The following example will create an object of the Main class, with the
name myObj. We use the x attribute on the object to print its value:

Create an object called “”myObj” and print the value of x:

You can also modify attribute values:

Set the value of x to 40:

Or override existing values:


Change the value of x to 25:

If you don’t want the ability to override existing values, declare the attributes as final:

The final keyword is called modifier.

If you create multiple objects of one class, you can change the attribute values
in one object, without affecting the attribute values in the other:
Change the value of x to 25 in myObj2, and leave x in myObj1 unchanged:

You can also specify as many attributes as you want:

You might also like