0% found this document useful (0 votes)
26 views12 pages

OOP Concepts

The document discusses object-oriented programming concepts in Java including classes, objects, attributes, and methods. It provides examples of creating classes and objects, accessing and modifying attributes, creating static and public methods, and calling methods on objects.

Uploaded by

Rodolfo III
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)
26 views12 pages

OOP Concepts

The document discusses object-oriented programming concepts in Java including classes, objects, attributes, and methods. It provides examples of creating classes and objects, accessing and modifying attributes, creating static and public methods, and calling methods on objects.

Uploaded by

Rodolfo III
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/ 12

ASIAN DEVELOPMENT FOUNDATION COLLEGE

262 P. Burgos St. Tacloban City


Information Technology Department

Object-oriented programming Concepts


Object-oriented programming or OOPs refers to languages that use objects in programming,
they use objects as a primary source to implement what is to happen in the code. Objects
are seen by the viewer or user, performing tasks assigned by you. Object-oriented
programming aims to implement real-world entities like inheritance, hiding, polymorphism
etc. in programming. The main aim of OOP is to bind together the data and the functions that
operate on them so that no other part of the code can access this data except that function. 
Let us discuss prerequisites by polishing concepts of method declaration and message
passing. Starting off with the method declaration, it consists of six components: 

Access Modifiers: Defines the access type of the method i.e. from where it can be
accessed in your application. In Java, there are 4 types of access specifiers: 
 public: Accessible in all classes in your application.
 protected: Accessible within the package in which it is defined and in
its subclass(es) (including subclasses declared outside the
package).
 private: Accessible only within the class in which it is defined.
 default (declared/defined without using any modifier): Accessible
within the same class and package within which its class is defined.

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.

Create a Class

To create a class, use the keyword class:

Main.java

1
Create a class named "Main" with a variable x:

public class Main {

int x = 5;

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:

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

public class Main {

int x = 5;

public static void main(String[] args) {

Main myObj = new Main();

System.out.println(myObj.x);

Multiple Objects

You can create multiple objects of one class:

Example
Create two objects of Main:

public class Main {

int x = 5;

2
public static void main(String[] args) {

Main myObj1 = new Main(); // Object 1

Main myObj2 = new Main(); // Object 2

System.out.println(myObj1.x);

System.out.println(myObj2.x);

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

Main.java

 public class Main {

 int x = 5;

 }

Second.java

class Second {

public static void main(String[] args) {

Main myObj = new Main();

System.out.println(myObj.x);

3
Java Class Attributes

In the previous example, 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:

Create a class called "Main" with two attributes: x and y:

public class Main {

int x = 5;

int y = 3;

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:

public class Main {

int x = 5;

public static void main(String[] args) {

Main myObj = new Main();

System.out.println(myObj.x);

Modify Attributes

You can also modify attribute values:

4
Set the value of x to 40:

public class Main {

int x;

public static void main(String[] args) {

Main myObj = new Main();

myObj.x = 40;

System.out.println(myObj.x);

Or override existing values:

Change the value of x to 25:

public class Main {

int x = 10;

public static void main(String[] args) {

Main myObj = new Main();

myObj.x = 25; // x is now 25

System.out.println(myObj.x);

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

public class Main {

final int x = 10;

5
public static void main(String[] args) {

Main myObj = new Main();

myObj.x = 25; // will generate an error: cannot assign a value to a final variable

System.out.println(myObj.x);

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:

Change the value of x to 25 in myObj2, and leave x in myObj1 unchanged:

public class Main {

int x = 5;

public static void main(String[] args) {

Main myObj1 = new Main(); // Object 1

Main myObj2 = new Main(); // Object 2

myObj2.x = 25;

System.out.println(myObj1.x); // Outputs 5

System.out.println(myObj2.x); // Outputs 25

Multiple Attributes

You can specify as many attributes as you want:

public class Main {

6
String fname = "John";

String lname = "Doe";

int age = 24;

public static void main(String[] args) {

Main myObj = new Main();

System.out.println("Name: " + myObj.fname + " " + myObj.lname);

System.out.println("Age: " + myObj.age);

Java Class Methods

EXAMPLE:

Create a method named myMethod() in Main:

public class Main {

static void myMethod() {

System.out.println("Hello World!");

myMethod() prints a text (the action), when it is called. To call a method, write the method's
name followed by two parentheses () and a semicolon;

Inside main, call myMethod():

public class Main {

static void myMethod() {

System.out.println("Hello World!");

7
public static void main(String[] args) {

myMethod();

// Outputs "Hello World!"

Static vs. Public

You will often see Java programs that have either static or public attributes and methods.

In the example above, 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:

An example to demonstrate the differences between static and public methods:

public class Main {

// Static method

static void myStaticMethod() {

System.out.println("Static methods can be called without creating objects");

// Public method

public void myPublicMethod() {

System.out.println("Public methods must be called by creating objects");

// Main method

public static void main(String[] args) {

8
myStaticMethod(); // Call the static method

// myPublicMethod(); This would compile an error

Main myObj = new Main(); // Create an object of Main

myObj.myPublicMethod(); // Call the public method on the object

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:

// Create a Main class

public class Main {

// Create a fullThrottle() method

public void fullThrottle() {

System.out.println("The car is going as fast as it can!");

// Create a speed() method and add a parameter

public void speed(int maxSpeed) {

System.out.println("Max speed is: " + maxSpeed);

// Inside main, call the methods on the myCar object

9
public static void main(String[] args) {

Main myCar = new Main(); // Create a myCar object

myCar.fullThrottle(); // Call the fullThrottle() method

myCar.speed(200); // Call the speed() method

// The car is going as fast as it can!

// Max speed is: 200

Example explained

1) We created a custom Main class with the class keyword.

2) We created the fullThrottle() and speed() methods in the Main class.

3) The fullThrottle() method and the speed() method will print out some text, when they are
called.

4) The speed() method accepts an int parameter called maxSpeed - we will use this in 8).

5) In order to use the Main class and its methods, we need to create an object of


the Main Class.

6) Then, go to the main() method, which you know by now is a built-in Java method that runs
your program (any code inside main is executed).

7) By using the new keyword we created an object with the name myCar.

8) Then, we call the fullThrottle() and speed() methods on the myCar object, and run the


program using the name of the object (myCar), followed by a dot (.), followed by the name of
the method (fullThrottle(); and speed(200);). Notice that we add an int parameter of 200 inside
the speed() method.

Remember that..

The dot (.) is used to access the object's attributes and methods.

10
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).

Using Multiple Classes

It is a good practice to create an object of a class and access it in another class.

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

Main.java

public class Main {

public void fullThrottle() {

System.out.println("The car is going as fast as it can!");

public void speed(int maxSpeed) {

System.out.println("Max speed is: " + maxSpeed);

Second.java

class Second {

public static void main(String[] args) {

Main myCar = new Main(); // Create a myCar object

myCar.fullThrottle(); // Call the fullThrottle() method

myCar.speed(200); // Call the speed() method

11
References:

https://www.w3schools.com/java/java_oop.asp

https://www.geeksforgeeks.org/classes-objects-java/

12

You might also like