Object Oriented Programming 1
modifiers returntype methodName(){
//Do Anything here
static void sayHello(){
System.out.println(“Hello”);
public static void main(String[] args){ sayHello();
public static void main(String[] args){ methodName();
Global Variables
Are variables declared within a class, it can be accessed within the whole class.
Local Variables
Are variables declared inside a method, condition, loops and any other block of code, it can only be accessible within that block of code.
A value that needs to be passed on a Method so that the method can use that value and perform various operations on it.
PS: You can have as may
Arguments/Parameters as you want, They act as a Local Variable inside a method/function.
modifiers returntype methodName(arguments){
// Do anything here
}
static void say(String word){
System.out.println(word);
}
return keyword is used to return a value from the method. It is used when a method has a result.
Example:
A Method that performs Math Equations.
A Method that Concatenates Strings.
The type of the value that will be returned, return type are same as the datatypes.
void return nothing. int returns integers. string return strings.
And so on…..
modifiers returntype methodName(arguments){
//Do anything here return value;
}
static int add(int num1, int num2){
return num1+num2;
}
You can use the same method name but different parameters so that you will cater every possibility of a method.
static int add(int num1, int num2){ return num1+num2;
}
static int add(int num1, int num2, int num3){ return num1+num2+num3;
}
Create a program that has method named summation it needs to accept an array of integers then return the sum of the given array.
PS: Display the SUM outside of the method.
Classes & Objects
Object Oriented Programming 1
It focuses on implementing read world objects using Classes to create variations of Objects, that has attributes and purpose.
It helps us create much flexible and efficient code than procedural programming.
It is created by the programmer, It will act as a blueprint of an object that you want to implement in your program.
They contain all the attributes and methods that your desired object should have.
It is created by instantiating a Class.
It is anything that has an attribute and a purpose.
Example: Person, Furniture and Food.
These are the global variables declared inside the class of our object.
It is used to create variations of an object using only one class.
modifiers className class {
//Attributes
//Method or Purpose }
Person | public Person class { |
---|---|
-First Name | //Attributes |
-Last Name | String firstName; |
-Sex | String lastName; |
-Age | char sex; |
int age;
//Method or Purpose
}
The process of creating an Object using a class so we can use it on our program.
ClassName identifier = new ClassName();
Person p = new Person();
ClassName identifier = new ClassName();
WRITING Attributes
identifier**.attribute =** value**;**
READING Attributes
System.out.println(identifier.attribute);
Packages & Modifiers
Object Oriented Programming 1
This is like a folder in our project where we can throw all our related codes in the same directory.
We can create multiple packages inside our java project, we can also reuse it in other projects if we choose to do so.
We can create classes inside a package and import it in our main class so that we can use that class in our program.
IMPORT SPECIFIC CLASS
import packagename.class
IMPORT WHOLE PACKAGE
import packagename.*
The process of creating an Instance of
a class so we can use it on our program.
PS: These can also be called objects later on.
ClassName identifier = new ClassName();
Arithmetic a = new Arithmetic();
Used to modify where classes, variables and methods are accessible.
Classes
Modifier | Description |
---|---|
default | The class can only be accessed by classes in the same package. |
public | The class can be accessed anywhere as long as it is imported. |
Methods and Variables
Modifier | Description |
---|---|
default | It can only accessed by classes in the same package. |
public | It can be accessed anywhere as long as it is imported. |
private | It can only be accessed inside its own class. |
protected | It can only be accessed by classes in the same package and subclass. |
Used to add other functionalities for classes, variables and methods, it is basically used for specific situation.
Classes
Modifier | Description |
---|---|
final | The class cannot be inherited by other classes |
abstract | The class cannot be used to instantiate objects. We can only access an abstract class, by inheriting it from another class. |
Methods and Variables
Modifier | Description |
---|---|
transient | Variables and Methods are skipped when serializing the object containing them. |
synchronized | Methods can only be accessed by one thread at a time |
volatile | The value of an attribute is not cached thread-locally, and is always read from the“main memory” |
Methods and Variables
Modifier | Description |
---|---|
final | Variables cannot be reassigned. Methods cannot be overridden. |
static | The variables/methods belong to the class itself rather than its instance |
abstract | It can only be used in an abstract classmethods. These methods does not have a body |
Methods or Functions are used to divide and sort Functionalities within a class so that the code will be readable even if it is long.
CREATING Methods - static void sayHello(){ System.out.println(“Hello”); }
CALLING Methods - public static void main(String[] args){ sayHello(); }
Global Variables are variables declared within a class, it can be accessed within the whole class.
Local Variables are variables declared inside a method, condition, loops and any other block of code, it can only be accessible within that block of code.
ARGUMENTS or PARAMETERS are a value that needs to be passed on a Method so that the method can use that value and perform various operations on it.
TRUE - You can have as may Arguments/Parameters as you want. They act as a Local Variable inside a method/function.
Methods w/ Arguments - static void say(String word){ System.out.println(word); }
return keyword is used to return a value from the method. It is used when a method has a result.
void - a return type that returns nothing.
int - a return type that returns integers.
string - a return type that return strings.
Methods w/ RETURN - static int add(int num1, int num2){ return num1+num2; }
TRUE - In Overloading Methods, you can use the same method name but different parameters so that you will cater every possibility of a method
Overloading Methods - static int add(int num1, int num2){ return num1+num2; } static int add(int num1, int num2, int num3){ return num1+num2+num3; }
OBJECT ORIENTED PROGRAMMING - focuses on implementing read world objects using Classes to create variations of Objects, that has attributes and purpose. It helps us create much flexible and efficient code than procedural programming.
CLASSES - created by the programmer, It will act as a blueprint of an object that you want to implement in your program. They contain all the attributes and methods that your desired object should have.
OBJECTS - It is created by instantiating a Class. It is anything that has an attribute and a purpose. Example: Person, Furniture and Food.
ATTRIBUTES - These are the global variables declared inside the class of our object. It is used to create variations of an object using only one class.
CLASS Creation - modifiers className class { //Attributes //Method or Purpose }
CLASS Instantiation - The process of creating an Object using a class so we can use it on our program. ClassName identifier = new ClassName(); Person p = new Person();
ACCESSING Attributes - ClassName identifier = new ClassName(); WRITING Attributes identifier.attribute = value; READING Attributes System.out.println(identifier.attribute);
PACKAGES - This is like a folder in our project where we can throw all our related codes in the same directory.
TRUE - We can create multiple packages inside our java project, we can also reuse it in other projects if we choose to do so.
TRUE - We can create classes inside a package and import it in our main class so that we can use that class in our program.
CLASS Instantiation - The process of creating an Instance of a class so we can use it on our program. PS: These can also be called objects later on
ACCESS Modifiers - Used to modify where classes, variables and methods are accessible.