Object-Oriented programming or OOP
– Revolves around the concept of objects as the basic elements of
your programs.
– These objects are characterized by their properties and behaviors.
Encapsulation
– the method of hiding certain elements of the implementation of a
certain class.
– By placing a boundary around the properties and methods of our
objects, we can prevent our programs from having side effects
wherein programs have their variables changed in unexpected ways.
Class & Objects
Class
– can be thought of as a template, a prototype or a blueprint of an object
– is the fundamental structure in object-oriented programming
Two types of class members:
– Fields (properties or attributes)
● specify the data types defined by the class
– Methods.
● specify the operations
Object
– is composed of a set of data (properties) which are variables describing
the essential characteristics of the object, and consists of a set of
methods (behavior) that describes how an object behaves.
– An object is an instance of a class.
Class Variables
-● Classes consist of
– Instance variables
– Instance methods
– Class Variables (static member variables)
● variables that belong to the whole class.
● This means that they have the same value for all the objects in the same class.
Class Instantiation
● To create an object or an instance of a class, we use the
new operator.
● For example, if you want to create an instance of the class
String, we write the following code,
String str2 = new String(“Hello world!”);
or also equivalent to,
String str2 = "Hello";
The new operator
– allocates a memory for that object and returns a reference of that
memory location to you.
– When you create an object, you actually invoke the class'
constructor.
The constructor
– is a method where you place all the initializations, it has the same
name as the class.
Method
– is a separate piece of code that can be called by a main program or
any other method to perform some specific function.
The following are characteristics of methods:
– It can return one or no values
– It may accept as many parameters it needs or no parameter at all.
Parameters are also called arguments.
– After the method has finished execution, it goes back to the method
that called it.
*Method Declaration
A method's declaration provides a lot of information about the method to the compiler, to the runtime system, and to other classes and objects.Included is not only the name of the method,but also such infomation as the return type of the method,the number and type of the arguments required by the method,and which other classes and object can call the method
*Static Methods
– methods that can be invoked without instantiating a class (means
without invoking the new keyword).
– Static methods belong to the class as a whole and not to a certain
instance (or object) of a class.
– Static methods are distinguished from instance methods in a class
definition by the keyword static.
Parameter Passing
*Pass-by-Value
– when a pass-by-value occurs, the method makes a copy of the value
of the variable passed to the method. The method cannot
accidentally modify the original argument even if it modifies the
parameters during calculations.
– all primitive data types when passed to a method are pass-by-value.
*Pass-by-Reference
– When a pass-by-reference occurs, the reference to an object is
passed to the calling method. This means that, the method makes a
copy of the reference of the variable passed to the method.
– However, unlike in pass-by-value, the method can modify the actual
object that the reference is pointing to, since, although different
references are used in the methods, the location of the data they are
pointing to is the same.
– Revolves around the concept of objects as the basic elements of
your programs.
– These objects are characterized by their properties and behaviors.
Encapsulation
– the method of hiding certain elements of the implementation of a
certain class.
– By placing a boundary around the properties and methods of our
objects, we can prevent our programs from having side effects
wherein programs have their variables changed in unexpected ways.
Class & Objects
Class
– can be thought of as a template, a prototype or a blueprint of an object
– is the fundamental structure in object-oriented programming
Two types of class members:
– Fields (properties or attributes)
● specify the data types defined by the class
– Methods.
● specify the operations
Object
– is composed of a set of data (properties) which are variables describing
the essential characteristics of the object, and consists of a set of
methods (behavior) that describes how an object behaves.
– An object is an instance of a class.
Class Variables
-● Classes consist of
– Instance variables
– Instance methods
– Class Variables (static member variables)
● variables that belong to the whole class.
● This means that they have the same value for all the objects in the same class.
Class Instantiation
● To create an object or an instance of a class, we use the
new operator.
● For example, if you want to create an instance of the class
String, we write the following code,
String str2 = new String(“Hello world!”);
or also equivalent to,
String str2 = "Hello";
The new operator
– allocates a memory for that object and returns a reference of that
memory location to you.
– When you create an object, you actually invoke the class'
constructor.
The constructor
– is a method where you place all the initializations, it has the same
name as the class.
Method
– is a separate piece of code that can be called by a main program or
any other method to perform some specific function.
The following are characteristics of methods:
– It can return one or no values
– It may accept as many parameters it needs or no parameter at all.
Parameters are also called arguments.
– After the method has finished execution, it goes back to the method
that called it.
*Method Declaration
A method's declaration provides a lot of information about the method to the compiler, to the runtime system, and to other classes and objects.Included is not only the name of the method,but also such infomation as the return type of the method,the number and type of the arguments required by the method,and which other classes and object can call the method
While this may sound like writing a novel rather than simply declaring a method, most method attributs can be declared implicity.The only required elements of a method declaration are the method names, it return type,and a pair of parentheses().This figure shows the elements of a method declaration.
*Static Methods
– methods that can be invoked without instantiating a class (means
without invoking the new keyword).
– Static methods belong to the class as a whole and not to a certain
instance (or object) of a class.
– Static methods are distinguished from instance methods in a class
definition by the keyword static.
Parameter Passing
*Pass-by-Value
– when a pass-by-value occurs, the method makes a copy of the value
of the variable passed to the method. The method cannot
accidentally modify the original argument even if it modifies the
parameters during calculations.
– all primitive data types when passed to a method are pass-by-value.
*Pass-by-Reference
– When a pass-by-reference occurs, the reference to an object is
passed to the calling method. This means that, the method makes a
copy of the reference of the variable passed to the method.
– However, unlike in pass-by-value, the method can modify the actual
object that the reference is pointing to, since, although different
references are used in the methods, the location of the data they are
pointing to is the same.
