OOP Concepts
OOP Concepts
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
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.
Create a Class
Main.java
1
Create a class named "Main" with a variable x:
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:
int x = 5;
System.out.println(myObj.x);
Multiple Objects
Example
Create two objects of Main:
int x = 5;
2
public static void main(String[] args) {
System.out.println(myObj1.x);
System.out.println(myObj2.x);
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
int x = 5;
}
Second.java
class Second {
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:
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:
int x = 5;
System.out.println(myObj.x);
Modify Attributes
4
Set the value of x to 40:
int x;
myObj.x = 40;
System.out.println(myObj.x);
int x = 10;
System.out.println(myObj.x);
If you don't want the ability to override existing values, declare the attribute as final:
5
public static void main(String[] args) {
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:
int x = 5;
myObj2.x = 25;
System.out.println(myObj1.x); // Outputs 5
System.out.println(myObj2.x); // Outputs 25
Multiple Attributes
6
String fname = "John";
Java Class Methods
EXAMPLE:
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():
System.out.println("Hello World!");
7
public static void main(String[] args) {
myMethod();
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:
// Static method
// Public method
// Main method
8
myStaticMethod(); // Call the static method
9
public static void main(String[] args) {
Example explained
3) The fullThrottle() method and the speed() method will print out some text, when they are
called.
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).
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 (;).
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
Second.java
class Second {
11
References:
https://www.w3schools.com/java/java_oop.asp
https://www.geeksforgeeks.org/classes-objects-java/
12