OOP_Q3_Reviewer

Methods

Object Oriented Programming 1

Objectives

  • METHODS
  • CREATING Methods
  • CALLING Methods
  • VARIABLE Scoping
  • PARAMETERS / ARGUMENTS

Objectives (continued)

  • RETURN Keyword
  • RETURN Type
  • OVERLOADING Methods
  • Example (Summation of Numbers)

CREATING Methods

modifiers returntype methodName(){

//Do Anything here

CREATING Methods

static void sayHello(){

System.out.println(“Hello”);

CALLING Methods

public static void main(String[] args){ sayHello();

CALLING Methods

public static void main(String[] args){ methodName();

VARIABLE Scoping

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 / PARAMETERS

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.

Methods w/ Arguments

modifiers returntype methodName(arguments){

// Do anything here

}

Methods w/ Arguments

static void say(String word){

System.out.println(word);

}

RETURN Keyword

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.

RETURN Type

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…..

Methods w/ RETURN

modifiers returntype methodName(arguments){

//Do anything here return value;

}

Methods w/ RETURN

static int add(int num1, int num2){

return num1+num2;

}

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;

}

SUMMATION of NUMBERS

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

Objectives

  • OBJECT-ORIENTED PROGRAMMING
  • CLASSES
  • OBJECTS
  • ATTRIBUTES
  • CLASS Creation
  • CLASS Instantiation
  • ACCESSING Attributes

OBJECT – ORIENTED PROGRAMMING

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.

CLASSES

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.

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 Creation

Personpublic Person class {
-First Name//Attributes
-Last NameString firstName;
-SexString lastName;
-Agechar sex;

int age;

//Method or Purpose

}

CLASS Instantiation

The process of creating an Object using a class so we can use it on our program.

CLASS Instantiation

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 & Modifiers

Object Oriented Programming 1

Objectives

  • PACKAGES • CREATING Packages
  • CREATING Classes
  • IMPORTING Packages
  • CLASS Instantiation
  • ACCESS Modifiers
  • NON-ACCESS Modifiers

PACKAGES

This is like a folder in our project where we can throw all our related codes in the same directory.

CREATING Packages

We can create multiple packages inside our java project, we can also reuse it in other projects if we choose to do so.

CREATING Classes

We can create classes inside a package and import it in our main class so that we can use that class in our program.

IMPORTING Packages

IMPORT SPECIFIC CLASS

import packagename.class

IMPORT WHOLE PACKAGE

import packagename.*

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.

CLASS Instantiation

ClassName identifier = new ClassName();

Arithmetic a = new Arithmetic();

ACCESS Modifiers

Used to modify where classes, variables and methods are accessible.

ACCESS Modifiers

Classes

ModifierDescription
defaultThe class can only be accessed by classes in the same package.
publicThe class can be accessed anywhere as long as it is imported.

ACCESS Modifiers

Methods and Variables

ModifierDescription
defaultIt can only accessed by classes in the same package.
publicIt can be accessed anywhere as long as it is imported.
privateIt can only be accessed inside its own class.
protectedIt can only be accessed by classes in the same package and subclass.

NON – ACCESS Modifiers

Used to add other functionalities for classes, variables and methods, it is basically used for specific situation.

Classes

ModifierDescription
finalThe class cannot be inherited by other classes
abstractThe class cannot be used to instantiate objects. We can only access an abstract class, by inheriting it from another class.

Methods and Variables

ModifierDescription
transientVariables and Methods are skipped when serializing the object containing them.
synchronizedMethods can only be accessed by one thread at a time
volatileThe value of an attribute is not cached thread-locally, and is always read from the“main memory”

Methods and Variables

ModifierDescription
finalVariables cannot be reassigned. Methods cannot be overridden.
staticThe variables/methods belong to the class itself rather than its instance
abstractIt 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.