OOP - Chapter 2
OOP - Chapter 2
OOP - Chapter 2
Chapter Two
These tasks are not listed in any particular order. Most likely, you will perform these tasks
iteratively throughout the design process.
In an object-oriented design:
- You identify the fundamental objects of the problem domain, the "things" involved.
- You then classify the objects into types by identifying groups of objects that have common
characteristics and behaviors.
The types of objects you identify in the problem domain become "types" in your solution. The
program you write will create and manipulate objects of these types.
The fundamental task of abstraction in an object-oriented design is to identify classes and objects
in the problem domain and then to identify attributes and methods.
Everything (data and methods) in Java is contained in classes. So far you've been using classes
to hold methods (main and perhaps a few other static methods). These methods use local
variables, which are completely private to the method, and which disappear when the method
returns.
The main task of OOP is to modeling the real world problem using classes and objects. Classes
and objects are models that used to represent the real world problem that supposed to be solved.
A model is a representation of simple important aspects of the real world. Model used in software
development like, representation of input, objects, object interaction and classes. Abstraction is
the main source for modeling. These models represent classes and objects. Thus, classes and
objects are a representation of reality, abstracted from the complex real world.
The first step in finding classes and objects is studying the problem domain.
Classes and objects should initially emerge from the problem domain itself.
Construct a prototype – simple skeleton of real system, using classes, objects and
including expected interactions between.
Class name
List of Attributes
List of Methods
Example 1: Consider Cost Sharing System, use abstraction feature and list possible
classes, attributes, and methods
Example 2: Consider Dormitory System, use abstraction technique and list possible
classes, attributes, and methods. Do the same for Car Registration System.
Class is similar to database table. If you are familiar with databases, a class is very similar to a
table definition.
- A private part can only be accessible by member methods (by all methods defined inside
the class).
- A public part can be used by other methods and classes outside the given class.
Example 2: Here is the class again that represents information about a student.
A class defines what fields an object will have when it's created. When a new Student object is
created, a block of memory is allocated, which is big enough to hold these three fields -- as well
as some extra overhead that all objects have.
Student stud;
stud = new Student(); // Create Student object with new operator.
Or we can write in one line in the following way:
Student stud=new Student ( );
To create a new object, write new followed by the name of the class, followed by parentheses.
Later we'll see that we can specify arguments in the parentheses when creating new objects.
All public fields can be referenced using dot notation (later we'll see better ways to access and
set fields). To reference a field, write the object name, then a dot, then the field name or method
name.
stud = new Student(); // Create a new Student object with default values.
stud.firstName = "Rishan"; // Set values of the fields.
stud.lastName = "Mezegeb";
stud.id = 1234;
A class definition is a template for creating objects. A class defines which fields an object has in
it. You need to use new to create a new object from the class by allocating memory and assigning
a default value to each field. A little later you will learn how to write a constructor to control the
initialization.
2.5 Constructor
- Constructor can be Default and Parameterized
Constructor Method
Automatically invoked when a class instance is created using the new operator
o Has same name as the class
o Has no return type
o Java creates a default constructor if no constructor has been explicitly written for a class
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
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.
Parameterized Constructor has parameter list to receive arguments for populating the instance
attributes.
When you create a new instance (a new object) of a class using the new keyword, a constructor
for that class is called. Constructors are used to initialize the instance variables (fields) of an
object. Constructors are similar to methods, but with some important differences.
Default constructor. If you don't define a constructor for a class, a default (parameter less)
constructor is automatically created by the compiler. The default constructor calls the default
parent constructor (super ( )) and initializes all instance variables to default value (zero for
numeric types, null for object references, and false for Booleans).
There is no return type given in a constructor signature (header). The value is this object
itself so there is no need to indicate a return value.
There is no return statement in the body of the constructor.
There can be more than one constructor in a class, distinguished by their parameters. When the
class is initialized, Java will call the right constructor with matching arguments and type.
If such a class requires a default constructor, its implementation must be provided. Any attempt
to call the default constructor will be a compile time error if an explicit default constructor is not
provided in such a case.
Summary
• Constructor is a special method that gets invoked “automatically” at the time of object
creation.
• Constructor is normally used for initializing objects with default values unless different
values are supplied.
• Constructor has the same name as the class name.
• Constructor cannot return values.
• A class can have more than one constructor as long as they have different signature (i.e.,
different input arguments syntax).