0% found this document useful (0 votes)
6 views27 pages

Oop Week 6789

OOP topics, classes, constructor, encapsulation, inheritance. Etc...

Uploaded by

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

Oop Week 6789

OOP topics, classes, constructor, encapsulation, inheritance. Etc...

Uploaded by

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

JAVA OOP

PF1 - OBJECT ORIENTED


PROGRAMMING
WHAT IS OBJECT ORIENTED
PROGRAMMING?
Procedural programming is about writing procedures or methods that
perform operations on the data, while object-oriented programming is
about creating objects that contain both data and methods.

Object-oriented programming has several advantages over procedural


programming:
OOP is faster and easier to execute
OOP provides a clear structure for the programs
OOP helps to keep the Java code DRY "Don't Repeat Yourself", and
makes the code easier to maintain, modify and debug
OOP makes it possible to create full reusable applications with less
code and shorter development time
WHAT ARE CLASSES AND
OBJECTS?
Classes and objects are the two main aspects of object-
oriented programming.
Look at the following illustration to see the difference
between class and objects:

So, a class is a template for objects, and an object is an instance of a class.


When the individual objects are created, they inherit all the variables and
methods from the class.
JAVA CLASSES/OBJECTS
Java is an object-oriented programming language.

Everything in Java is associated with classes and objects, along with its attributes
and methods. For example: in real life, a car is an object. The car has attributes,
such as weight and color, and methods, such as drive and brake.

A Class is like an object constructor, or a "blueprint" for creating objects.

Remember that a class should always


start with an uppercase first letter, and
that the name of the java file should
match the class name.
JAVA CLASSES/OBJECTS
CREATE AN OBJECT
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:
JAVA CLASSES/OBJECTS
MULTIPLE OBJECTS
You can create multiple objects of one class:
JAVA CLASSES/OBJECTS
USING MULTIPLE CLASSES
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 CLASSES/OBJECTS
JAVA CLASSES/OBJECTS
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:

Another term for class


attributes is fields.
JAVA CLASSES/OBJECTS
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:
JAVA CLASSES/OBJECTS
MODIFY ATTRIBUTES
You can also modify attribute values: Or override existing values:
JAVA CLASSES/OBJECTS
MODIFY ATTRIBUTES
If you don't want the ability to override existing values, declare the attribute as final:

The final keyword is useful when you want a variable to always store the
same value, like PI (3.14159...).
JAVA CLASSES/OBJECTS
MULTIPLE OBJECTS
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:
JAVA CLASSES/OBJECTS
MULTIPLE ATTRIBUTES
You can specify as many attributes as you want:
JAVA CLASS METHODS
STATIC VS. PUBLIC
You will often see Java programs that
have either static or public attributes
and methods.

In the example, we created a static


method, which means that it can be
accessed without creating an object of
the class, unlike public, which can only
be accessed by objects:
JAVA CLASSES/OBJECTS
ACCESS METHODS WITH AN OBJECT
Create a Car object named myCar. Call the fullThrottle() and speed() methods on the myCar object, and run the
program:

The dot (.) is used to access


the object's attributes and
methods.
To call a method in Java,
write the method name
followed by a set of
parentheses (), followed by a
semicolon (;).
A class must have a
matching filename (Main
and Main.java).
JAVA CLASSES/OBJECTS
USING MULTIPLE CLASSES
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:
Main.java
Second.java
JAVA CONSTRUCTORS
A constructor in Java is a special method that is used to initialize objects. The constructor is
called when an object of a class is created. It can be used to set initial values for object
attributes:

Note that the constructor


name must match the
class name, and it cannot
have a return type (like
void).
Also note that the
constructor is called when
the object is created.
All classes have
constructors by default: if
you do not create a class
constructor yourself, Java
creates one for you.
However, then you are not
able to set initial values
for object attributes.
JAVA CONSTRUCTORS
CONSTRUCTOR PARAMETERS
Constructors can also take parameters, which is used to initialize attributes.
The following example adds an int y parameter to the constructor. Inside the constructor we set x to y (x=y).
When we call the constructor, we pass a parameter to the constructor (5), which will set the value of x to 5:

You can have as many parameters as you want:


JAVA MODIFIERS
By now, you are quite familiar with the public keyword that appears in almost all of our examples:

The public keyword is an access modifier, meaning that it is used to set the access level for classes,
attributes, methods and constructors.
We divide modifiers into two groups:
Access Modifiers - controls the access level
Non-Access Modifiers - do not control access level, but provides other functionality

ACCESS MODIFIERS
JAVA MODIFIERS
ACCESS MODIFIERS

NON-ACCESS MODIFIERS
JAVA MODIFIERS
NON-ACCESS MODIFIERS
JAVA ENCAPSULATION
The meaning of Encapsulation, is to make sure that "sensitive" data is hidden from users. To
achieve this, you must:
declare class variables/attributes as private
provide public get and set methods to access and update the value of a private variable

GET AND SET


You learned from the previous chapter that private variables can only be accessed within the
same class (an outside class has no access to it). However, it is possible to access them if we
provide public get and set methods.

The get method returns the variable value, and the set method sets the value.
JAVA ENCAPSULATION
GET AND SET
Example explained
The get method returns the value of the
variable name.
The set method takes a parameter
(newName) and assigns it to the name
variable. The this keyword is used to refer to
the current object.
However, as the name variable is declared as
private, we cannot access it from outside this
class:
JAVA ENCAPSULATION
GET AND SET
JAVA ENCAPSULATION
GET AND SET

Why Encapsulation?
Better control of class attributes and methods
Class attributes can be made read-only (if you only use the get method), or write-only (if you
only use the set method)
Flexible: the programmer can change one part of the code without affecting other parts
Increased security of data
JAVA INHERITANCE
SUBCLASS AND SUPERCLASS
In Java, it is possible to inherit attributes and methods from one class to
another. We group the "inheritance concept" into two categories:
subclass (child) - the class that inherits from another class
superclass (parent) - the class being inherited from
To inherit from a class, use the extends keyword.
Did you notice the protected modifier in
Vehicle?
We set the brand attribute in Vehicle to a
protected access modifier. If it was set to
private, the Car class would not be able to
access it.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy