You are on page 1of 14

OOPR FINALS REVIEWER

ARRAY BASIC CONCEPTS


ORDINARY VARIABLE vs. ARRAY

THE ORDINARY VARIABLE

VARIABLE
 Is a location in the computer’s memory where a value can be stored for use by a program.
SYNTAX:

VARIABLE DECLARATION

ORDINARY VARIABLE IS LIKE A BOX storing ONE VALUE


ARRAY IS LIKE CABINET containing many drawers

WHAT IS AN ARRAY?
 A special kind of object used to store a collection of variables, all of the same type.
 An ARRAY is something like a list of variables but it handles the naming of the variables in a nice and compact way.
 A single name for a collection of data values.

IMPLEMENTATION AND OPERATION

Creating an Arrays
3 ways to Use [] (Brackets) with an Array Name

1. Declaring an array: int[] pressure


 Creates a name of type “int array”
 Types int and int[] are different
 Int[] : type of the array
 Int : type of the individual values

2. To create a new array,


 e.g. pressure = new int[100];

3. To refer to a specific element in the array


 Also called an indexed variable, e.g.
 Pressure [3] = n keyboard.nec=xtInt();
 System.out.println(“You entered” + pressure[3]);

ARRAY TERMINOLOGY

ARRAY LENGTH
 Specified by the number in brackets when created with new
 maximum number of elements the array can hold
 storage is allocated whether or not the elements are assigned values

 the attribute length


 Specied[] entry = new Species[20];
 System.out.println(entry.length(;

 The length attribute is established in the declaration and cannot be changed unless the array is redeclared.

SUBSCRIPT RANGE
 Array subscripts use zero-numbering
 The first element has subscript 0
 The second elemnt has subscript 1
 Etc. –the nth element has subscript n-1
 The last element has subscript length -1
 For example: an int array with 4 elements

ACCESING ARRAY
MULTIDIMENSIONAL ARRAY

MULTIDIMENSIONAL ARRAYS

 Multi-dimensional array is similar with an array however, the difference from single dimension is it can store more
values utilizing more index reference from the created array.
 This is very helpful in storing multiple related data in a single array.
 This application allows you to have more than 1 value per index however, the data can be accessed by a more
complex reference or we can call it as sub-index of an array.
 This can be interpreted in a tabular manner.

DECLARING MULTI-DIMENSIONAL ARRAY


 An array of more than one dimension.
 Declaring a multidimensional array

ACCESSING VALUES FROM THE MULTI-DIMENSIONAL ARRAY


Entries can still be accessed using index number of a data within the array. Assume we’ll be using the array created.
USES & APPLICATION
Problem One
Getting the sum of Array contents
Problem Two
Searching the occurrence of a value within an Array

Problem One
PROBLEM TWO

ENCAPSULATION INHERITANCE AND POLYMORPHISM


ENCAPSULATION
 Encapsulation is the practice of keeping fields within a class private, then providing access to them via public
methods. It’s a protective barrier that keeps the data and code safe within the class itself. This way, we can re-use
objects like code components or variables without allowing open access to the data system-wide.

How Encapsulation Works


 Encapsulation lets us re-use functionality without jeopardizing security. It’s a powerful OOP concept in Java because
it helps us save a lot of time. For example, we may create a piece of code that calls specific data from a database. It
may be useful to reuse that code with other databases or processes. Encapsulation lets us do that while keeping our
original data private. It also lets us alter our original code without breaking it for others who have adopted it in the
meantime.

public class Professor{ private String name; public String getName() { return name;
}
public void setName(String name)
{
this.name = name
}
}

//save as Test.java package com.javatpoint; class Test {


public static void main(String[]
args) {
Profesor p = new Professor();
p.setName(“johani”);

System.out.println(p.getName()
);
}

INHERITANCE
 Inheritance is a special feature of Object Oriented Programming in Java. It lets programmers create new classes that
share some of the attributes of existing classes. This lets us build on previous work without reinventing the wheel.

How Inheritance Work


 Inheritance is another labor-saving Java OOP concept. It works by letting a new class adopt the properties of
another. We call the inheriting class a subclass or a child class. The original class is often called the parent.
We use the keyword extends to define a new class that inherits properties from an old class.

class Mammal {

}
class Panda extendsMammal {

Polymorphism
 Polymorphism is Java OOP concept lets programmers use the same word to mean different things in different
contexts. One form of polymorphism in Java is method overloading. That’s when different meanings are implied by
the code itself. The other form is method overriding. That’s when the different meanings are implied by the values
of the supplied variables.
How Polymorphism Work
 Polymorphism is derived from 2 Greek words: poly and morphs. The word "poly" means many and "morphs" means
forms. So polymorphism means many forms.
⊳ Polymorphism in Java works by using a reference to a parent class to affect an object in the child class. We might
create a class called “horse” by extending the “animal” class. That class might also implement the “professional racing”
class. The “horse” class is “polymorphic,” since it inherits attributes of both the “animal” and “professional racing” class.

class Person {
void walk()
{
System.out.println(“CanRun….”);
}
}
class Employee extendsPerson {
void walk()
{
System.out.println(“Running Fast…”);
}
public static void main(String arg[]) {
Person p = new Employee(); //upcasting p.walk();
}

OBJECT
 An entity that has state and behavior is known as an object e.g., chair, bike, marker, pen, table, car, doggo etc. It can
be physical or logical (tangible and intangible).
 Real-world objects share two characteristics: They all have state and behavior. Dogs have state (name,
color, breed, hungry) and behavior (barking, fetching, wagging tail). Bicycles also have state (current gear, current
pedal cadence, current speed) and behavior (changing gear, changing pedal cadence, applying brakes). Identifying
the state and behavior for real-world objects is a great way to begin thinking in terms of object-oriented
programming.

CLASS
 In the real world, you'll often find many individual objects all of the same kind. There may be thousands of other
bicycles in existence, all of the same make and model. Each bicycle was built from the same set of blueprints and
therefore contains the same components. In object-oriented terms, we say that your bicycle is an instance of the
class of objects known as bicycles. A classis the blueprint from which individual objects are created.

class Bicycle {

int cadence = 0; int speed = 0; int gear = 1;

void changeCadence(int newValue) {


cadence = newValue;
}

void changeGear(int newValue) { gear = newValue;


}

void speedUp(int increment) { speed = speed + increment;


}

void applyBrakes(int decrement) { speed = speed - decrement;


}

void printStates() { System.out.println("cadence:" +


cadence + " speed:" +
speed + " gear:" + gear);
}
}

class BicycleDemo {
public static void main(String[] args) {

// Create two different


// Bicycle objects
Bicycle bike1 = new Bicycle(); Bicycle bike2 = new Bicycle();

// Invoke methods on
// those objects bike1.changeCadence(50); bike1.speedUp(10); bike1.changeGear(2); bike1.printStates();

bike2.changeCadence(50); bike2.speedUp(10); bike2.changeGear(2); bike2.changeCadence(40); bike2.speedUp(10);


bike2.changeGear(3); bike2.printStates();
}
}

ABSTRACTION
 Abstraction means using simple things to represent complexity. We all know how to turn the TV on, but we don’t
need to know how it works in order to enjoy it. In Java, abstraction means simple things like objects, classes, and
variables represent more complex underlying code and data. This is important because it lets avoid repeating the
same work multiple times.

How Abstraction Works


 Abstraction as an OOP concept in Java works by letting programmers create useful, reusable tools.
For example, a programmer can create several different types of objects. These can be variables, functions, or data
structures. Programmers can also create different classes of objects. These are ways to define the objects.
 For instance, a class of variable might be an address. The class might specify that each address object shall have a
name, street, city, and zip code. The objects, in this case, might be employee addresses, customer addresses, or
supplier addresses.

Module CLASSES AND 9 OBJECTS

Java Classes
CLASS
class is a user defined blueprint or prototype from which objects are created. It represents the set of properties or
methods that are common to all objects of one type.

Modifiers : A class can be public or has default access.


⊳ Class name: The name should begin with a initial letter.
⊳ Superclass: The name of the class’s parent (superclass), if any, preceded by the keyword extends. A class can only
extend (subclass) one parent.
⊳ Interfaces (if any): A comma-separated list of interfaces implemented by the class, if any, preceded by the keyword
implements. A class can implement more than one interface.
⊳ Body: The class body surrounded by braces, {}.

CLASS CONTAINS
Instance of data- Instance variable in Java is used by Objects to store their states. Variables which are defined without
the STATIC keyword and are Outside any method declaration are Object specific and are known as instance variables.
⊳ Constructor(s)- Constructors are used to initialize the object’s state. Like methods, a constructor also contains
collection of statements(i.e. instructions) that are executed at time of Object creation.
⊳ Method(s)- A method is a collection of statements that perform some specific tasks and return the result to the caller.
⊳ A method can perform some specific tasks without returning anything.
⊳ Methods allow us to reuse the code without retyping the code. In Java, every method must be part of some class
which is different from languages like C, C++, and Python.
OBJECT
It is a basic unit of Object-Oriented Programming and represents the real-life entities.
⊳ A typical Java program creates many objects, which as you know, interact by invoking methods.
⊳ OBJECTS CLASSIFICATION
▸ PHYSICAL-OBJECT THAT WE CAN TOUCH (STUDENT, ROOM)
▸ CONCEPTUAL- OBJECTS WE CANNOT TOUCH BUT EXIST(STUDENT NUMBER, COURSE)

State: It is represented by attributes of an object. It also reflects the properties of an object.


⊳ Behavior: It is represented by methods of an object. It also reflects the response of an object with other objects.
⊳ Identity: It gives a unique name to an object and enables one object to interact with other objects.
State: It is represented by attributes of an object. It also reflects the properties of an object.
⊳ Behavior: It is represented by methods of an object.

OBJECT INSTANTIATION
When an object of a class is created, the class is said to be instantiated. All the instances share the attributes and the
behavior of the class. But the values of those attributes, i.e. the state are unique for each object. A single class may have
any number of instances.

CLASS DECLARATION
The syntax for declaring a class is:
<modifier> class ClassName {
//field, constructor, and method declarations
}
Generally, declaring a class can include the following components, in order:

1. Modifiers such as public or private.


2. The keyword class used to create a class in Java.
3. The class name, with initial letter capitalized by convention. Think of an appropriate name for your class. Don’t just
call your class ABC or any random names you can think of. Be descriptive.
4. The class body surrounded by braces {}.
public class OLFU{
//Data Members; Constructors; Methods
}

Declaring Member Variables


There are several kinds of variables and these are the following:
⊳ Fields – member variables of a class
⊳ Local Variables – variables declared in a method or block of code
⊳ Parameters – variables used in method declarations.
⊳ Field declarations are composed of three components:
▸ Zero or more modifier, such as public or private
▸ The field’s (data) type
▸ The field’s name
<modifier> <type> <name> [= <default value>];

There are several kinds of variables and these are the following:
⊳ Fields – member variables of a class
⊳ Local Variables – variables declared in a method or block of code
⊳ Parameters – variables used in method declarations.
⊳ Field declarations are composed of three components:
▸ Zero or more modifier, such as public or private
▸ The field’s (data) type
▸ The field’s name
<modifier> <type> <name> [= <default value>];
Declaring Member Variables

Access Modifier
The access modifiers in Java specifies the accessibility or scope of a field, method, constructor, or class. We can change
the access level of fields, constructors, methods, and class by applying the access modifier on it. Access modifiers are
categorise into four;
⊳ Private: The access level of a private modifier is only within the class. It cannot be accessed from outside the class.
⊳ Default: The access level of a default modifier is only within the package. It cannot be accessed from outside the
package. If you do not specify any access level, it will be the default.
⊳ Protected: The access level of a protected modifier is within the package and outside the package through child class.
If you do not make the child class, it cannot be accessed from outside the package.
⊳ Public: The access level of a public modifier is everywhere. It can be accessed from within the class, outside the class,
within the package and outside the package.

Private Access Modifier


⊳ The private access modifier is accessible only within the class.
class A{
private int data=40;
private void msg(){
System.out.println("Hello java");}
}

public class Simple{


public static void main(String args[]){
A obj=new A();
System.out.println(obj.data);//Compile Time Error
obj.msg();//Compile Time Error
}
}

⊳ If you make any class constructor private,


you cannot create the instance of that class from outside the class.

class A{
private A(){}//private constructor
void msg()
{System.out.println("Hello java");}
}
public class Simple{
public static void main(String args[]){
A obj=new A();//Compile Time Error
}}

Default Access Modifier


⊳ If you don't use any modifier, it is treated as default by default.
⊳ The default modifier is accessible only within package.
⊳ It cannot be accessed from outside the package. It provides more accessibility than private. But, it is more restrictive
than protected, and public.
package sample1;
class A{
void msg(){System.out.println("Hello");}
}

package sample2;
import sample1.*;
class B{
public static void main(String args[]){
A obj = new A();//Compile Time Error
obj.msg();//Compile Time Error
}
}

Protected Access Modifier


⊳ The protected access modifier is accessible within package and outside the package but through inheritance only.
⊳ The protected access modifier can be applied on the data member, method and constructor. It can't be applied on the
class.
⊳ It provides more accessibility than the default modifier.

package sample1;
public class A{
protected void msg(){
System.out.println("Hello");

package sample2;
import sample1.*;

class B extends A{
public static void main(String args[]){
B obj = new B();
obj.msg();
}
}

⊳ The public access modifier is accessible everywhere. It


has the widest scope among all other modifiers.

package sample1;
public class A{
public void msg(){
System.out.println("Hello");

package sample2;
import sample1.*;

class B{
public static void main(String args[]){
A obj = new A();
obj.msg();
}
}

Overriding
Method overriding is one of the way by which java achieve Run Time Polymorphism.

class A{
protected void msg(){
System.out.println("Hello java");

public class Simple extends A{


void msg(){
System.out.println("Hello java");
}
public static void main(String args[]){
Simple obj=new Simple();
obj.msg();
}
}

Final keyword
final keyword is used in different contexts. First of all, final is a non-access modifier applicable only to a variable, a
method or a class.
⊳ When a variable is declared with final keyword, its value can’t be modified, essentially, a constant.
⊳ This also means that you must initialize a final variable.
Final keyword
class Car{
final int speedlimit=90;
void run(){
speedlimit=400;
}
public static void main(String args[]){
Car Subaru =new Car();
Subaru.run();
}
}

You might also like