Chapter Two The Inside of Objects and Classes More OOP Concepts
Chapter Two The Inside of Objects and Classes More OOP Concepts
Chapter Two
2. The Inside of Objects and Classes: More OOP Concepts.
Class Declaration
- Class is a java keyword and all the normal keyword restrictions apply. One
way of thinking about a java class is to note that they resemble structures. We
simply use the key word class instead of struct, and the member of the
structure become private by default.
- the class declaration syntax is:
- class class_name{
data_members;
...
class_methods();
...;
}
Object Creation
- No object is actually created by the declaration process. An object
declaration simply declares the name (identifier), which we use to refer to an
object. eg:
- Account account; designate that the name account is used to refer to an
account object, but the actual Account object is not yet created. We create
an object by invoking the new command. the syntax is:
- <object name> = new <class name>(<arguments>);
- Where <object name> is the name of a declared object, <class name> is the
name of the class to which the object belongs, and <argument> is a
sequence of values passed to the method.
- let us see the distinction between object declaration and object
creation/instantiation
1
Chapter Two Object Oriented Programming
A. Account account;
Since there is no reference to the first customer object anymore, it will eventually be
erased and returned to the system. Remember that when an object is created, a certain
amount of memory space is allocated for storing objects. If this allocated but unused
space is not returned to the system for other use, the space gets wasted. This returning of
space to the system is called deallocation, and the mechanism to deallocate unused
space is called garbage collection
Constructors
- A constructor is a special initialization method that is called automatically
whenever an instance of a class is created. The constructor member method
has, by definition, the same name as the corresponding class. The constructor
has no return value specification, not even void. The Java run-time system
makes sure that the constructor of a class is called when instantiating an
object.
- Constructors are used to initialize the data members of the class. If we have
data members that must be initialized, we must have a constructor. In our
constructor, we should not do something that might fail because; the
constructor does not return a value.
public Employee()
{
empName = “”;
address = “”;
}
}//end class
3
Chapter Two Object Oriented Programming
Parameterized constructors
- The parameterized constructors can accept values into the constructor that
has different parameters in the constructor. The value would be transferred
from the main() method to the constructor either with direct values or through
variables.
}
}//end of class
4
Chapter Two Object Oriented Programming
- The above class contains five overloaded constructors that provide convenient
ways to initialize objects of class person. Each constructor guarantees that
every object the constructor initializes begins in a consistent state.
- The above class uses the keyword this to call / invoke a specific constructor
with the specified number and type of argument. It is a syntax error, when the
‘this’ reference is used in a constructor’s body to call another constructor of
the same class and that statement is not the first statement in the constructor. It
is also a syntax error when a non-constructor method attempts to invoke a
constructor directly via the ‘this’ reference.
Methods
- Methods (sometimes called functions or procedures) allow the programmer to
modularize a program by separating its tasks into self-contained units. These units are
- A method is invoked or called by a method call. The method call specifies the
name of the method and provides information (arguments) that the called
method requires to perform its task. When the method call completes, the
method either returns a result to the calling method (or caller) or simply
returns the control to the caller.
- The first line of the method declaration is called the method header.
Following the method header, declaration and statements in brace form the
method body, which is a block. Variables can be declared in any block, and
blocks can be nested. A method can’t be declared inside another method.
- the basic format of a method declaration is :
- return-value-type method-name(para1,para2,...,paraN)
{
declarations and statements;
}
- The method-name is any valid identifier. The return-type is the type of the
result returned by the method to the caller. The return-type void indicates that
a method does not return a value. Methods can return at most one value.
- eg. public double maximum(double x,double y,double z)
{
return Math.max(x,Math.max(y,z));
}
5
Chapter Two Object Oriented Programming
with the default properties. By default, all classes are assigned to package
level visibility. This means that while the class may be extended and
employed by other classes, only those objects within the same package
may make use of this class.
- private: private is the highest degree of protection that can be applied to a
method. A private method is only accessible by those methods in the same
class. Even classes that extend from the current class do not have access to a
private member.
Other important concepts :
- final: final classes may not have any subclasses and are created by placing the
modifier final in front of he class declaration. A variable can be decaled as
final. Doing so prevents its contents from being modified. This means that you
must initialize a final variable when it is declared. (In this usage, final is
similar to const in C++).
- abstract: an abstract class is a class that is incomplete, or to be considered
incomplete. Only abstract classes may have an abstract method. That is,
methods that are declared but not yet implemented. If a class that is not
abstract contains an abstract method, then a compile-time error occurs. A
class has abstract methods if any of the following is true; if: it explicitly
contains a declaration of an abstract method. it inherits an abstract method
from its direct superclass.
- An abstract class is defined with the modifier abstract. No instance can be
created from an abstract class. consider the following example:
6
Chapter Two Object Oriented Programming
- An abstract method is a method with the keyword abstract and it ends with a
semicolon instead of a method body. We say a method is implemented if it
has a method body.
or written at will by any method that has reference to an object of the class
in which the instance variable is declared.
- Using a public set and get method ensures that the new value is appropriate
for that data item. For example, an attempt to set the day of the month for a
data to 37 would be rejected; an attempt to set a person’s weight to a
negative value would be rejected, and so on. So, although set and get
methods could provide access to private data, the access is restricted by the
programmer’s implementation.
8
Chapter Two Object Oriented Programming
Passing by value
- Here, the actual variable will not be passed; rather, the value of the variable is
passed to the receiving method. Because the value/copy of the variable is
passed to the receiving method, the receiving method can’t modify the calling
method’s variable.
- Consider the following program that calculates the factorial of a give
number. The example illustrates passing by value.
import java.io.*;
public class Factorial{
public void calcFact(int n)
{
int i, fact=1;
for(int i=1;i<=n;i++)
fact*=i;
System.out.println(“the factorial of ”+n+ “is ”+fact);
}
}//end class
9
Chapter Two Object Oriented Programming
Passing by address
- When we pass an argument (local variable) by address, the variable’s address is
assigned to the receiving method’s parameter. The address of the variablenot its
value is copied to the receiving method’s parameter when we pass a variable
address. In Java, all the arrays are passed by address.
11
Chapter Two Object Oriented Programming
12
Chapter Two Object Oriented Programming
System.out.println(“employee1:”e1.getFirstName()+“”+e1.getSecondName());
System.out.println(“employee2:”e2.getFirstName()+ “”+e2.getSecondName());
}
}//end class
NB: A method declared static can’t access a non-static class member. Unlike non static
methods, a static method has no “this” reference because static variables and static
methods exist independent of any object of a class whether or not any objects of the
class have been instantiated.
13