Chapter Two: 2. The Inside of Objects and Classes: More OOP Concepts
Chapter Two: 2. 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 Compiled By Tewodors G.
Chapter Two Object Oriented Programming
account
2 Compiled By Tewodors G.
Chapter Two Object Oriented Programming
3 Compiled By Tewodors G.
Chapter Two Object Oriented Programming
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
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.
4 Compiled By Tewodors G.
Chapter Two Object Oriented Programming
5 Compiled By Tewodors G.
Chapter Two Object Oriented Programming
}
}//end class
- 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 (some times called functions or procedures) allow the programmer to
modularize a program by separating its tasks into self-contained units. These
units are sometimes called programmer defined methods.
6 Compiled By Tewodors G.
Chapter Two Object Oriented Programming
- 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));
}
7 Compiled By Tewodors G.
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:
8 Compiled By Tewodors G.
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.
9 Compiled By Tewodors G.
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.
10 Compiled By Tewodors G.
Chapter Two Object Oriented Programming
{
Factorial fact = new Factorial();
InputStreamReader reader = new InputStreamReader(System.in);
BufferedReader input = new BufferedReader(reader);
String num =
input.readLine(); int n =
Integer.parseInt(num);
fact.calcFact(n); }
}//end class
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 Compiled By Tewodors G.
Chapter Two Object Oriented Programming
12 Compiled By Tewodors G.
Chapter Two Object Oriented Programming
secondName=sName;
count1++;
count2++; }
13 Compiled By Tewodors G.
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 nonstatic
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.
14 Compiled By Tewodors G.