Mrec Oops Module-2
Mrec Oops Module-2
Syllabus:
Classes and Objects - Concepts of Classes, Objects, Constructors, Methods, This Keyword,
Garbage Collection, Overloading Methods and Constructors.
Inheritance - Base Class Object, Subclass, Member Access Rules, Super keyword, final
keyword, Method Overriding, Abstract Classes.
Example:
Box mybox; // declare reference to object
mybox = new Box(); // allocate a Box object
The first line declares mybox as a reference to an object of type Box. After this line
executes, mybox contains the value null, which indicates that it does not yet point to an actual
object. Any attempt to use mybox at this point will result in a compile-time error.
The next line allocates an actual object and assigns a reference to it to mybox. After the
second line executes, you can use mybox as if it were a Box object. But in reality, mybox simply
holds the memory address of the actual Box object.
The effect of these two lines of code is depicted in the figure as follows
The above two statements can also be combines into a single statement as follows
Box mybox = new Box();
The new operator:
The new operator dynamically allocates memory for an object. It has the general form:
class-var = new classname( );
Here, class-var is a variable of the class type being created. The classname is the name of
the class that is being instantiated. The class name followed by parentheses specifies the
constructor for the class. A constructor defines what occurs when an object of a class is created.
It is important to understand that new allocates memory for an object during run time. The
advantage of this approach (using new) is that the program can create as many or as few objects
as it needs during the execution of the program.
The distinction between a class and an object:
• A class creates a new data type that can be used to create objects. That is, a class creates a
logical framework that defines the relationship between its members.
• When we declare an object of a class, we are creating an instance of that class. Thus, a class
is a logical construct. An object has physical reality. (That is, an object occupies space in
memory.)
For example:
Box b1 = new Box();
Box b2 = b1;
// ...
b1 = null;
Here, b1 has been set to null, but b2 still points to the original object.
Introducing Methods
Classes usually consist of two things: instance variables and methods. In most classes, the
instance variables are acted upon and accessed by the methods defined for that class. Thus the
methods that determine how a class’ data can be used.
The general form of a method is:
type name(parameter-list) {
// body of method
}
• Here, type specifies the type of data returned by the method. If the method does not return a
value, its return type must be void.
• The name of the method is specified by name.
• The parameter-list is a sequence of type and identifier pairs separated by commas.
Methods that have a return type other than void return a value to the calling routine using the
following form of the return statement:
return value;
Here, value is the value returned.
Adding a Method to the Box Class:
Methods provide access to data. In addition to defining methods that provide access to
data, we can also define methods that are used internally by the class itself.
// This program includes a method inside the box class. Output:
class Box { Volume is 3000.0
let’s re-examine the new operator. As you know, when you allocate an object, you use the
following general form:
class-var = new classname( );
Now you can understand why the parentheses are needed after the class name. What is actually
happening is that the constructor for the class is being called. Thus, in the line
Box b1 = new Box();
new Box( ) is calling the Box( ) constructor. When you do not explicitly define a constructor for
a class, then Java creates a default constructor for the class.
The default constructor automatically initializes all instance variables to zero. The default
constructor is often sufficient for simple classes, but it usually won’t do for more sophisticated
ones. Once you define your own constructor, the default constructor is no longer used.
Parameterized Constructors:
The default constructor (Box()) in the preceding example does initialize a Box object. But
it is not very useful because all boxes have the same dimensions. What is needed is a way to
construct Box objects of various dimensions. The easy solution is to add parameters to the
constructor.
A constructor which takes parameters is called a parameterized constructor.
Garbage Collection
• Since objects are dynamically allocated by using the new operator, you might be wondering
how such objects are destroyed and their memory released for later reallocation.
• In some languages, such as C++, dynamically allocated objects must be manually released by
use of a delete operator.
• Java takes a different approach which handles deallocation automatically. The technique that
accomplishes this is called garbage collection.
• Garbage collection works like this: when no references to an object exist, that object is
assumed to be no longer needed, and the memory occupied by the object can be reclaimed.
• Garbage collection only occurs sporadically (if at all) during the execution of your program.
It will not occur simply because one or more objects exist that are no longer used.
• Different Java run-time implementations will take varying approaches to garbage collection,
but for the most part, you should not have to think about it while writing your programs.
Inheritance
Inheritance is an important feature of object oriented programming. Inheritance is the
property of acquiring the properties of other class. A class that is inherited is called a superclass.
The class that does the inheriting is called a subclass. A subclass inherits members of superclass
and adds its own, unique elements.
The general form of a class declaration that inherits a superclass is shown here:
class subclass-name extends superclass-name{
// body of class
}
In java extends is the keyword used to inherit the other class. The class which inherits the
properties is called sub class and the class from which the properties are inherited is called the
super class.
Types of inheritance
• Single inheritance
• Multi-level inheritance
• Multiple inheritance
• Hybrid inheritance
• Cyclic Inheritance
Single inheritance:
When a class inherits another class, it is known as a single inheritance. The following program
creates a superclass called A and a subclass called B.