Object Model in Java
The object model is a system or interface which is basically used to visualize elements in terms of objects in a software application. It is modeled using object-oriented techniques and before any programming or development is done, the object model is used to create a system model or an architecture. It defines object-oriented features of a system like inheritance, encapsulation, and many other object-oriented interfaces. Let us learn some of those object-oriented terminologies in-depth:
Objects and Classes
These form the basis of the Object-Oriented Paradigm in Java or any other Object-Oriented Programming Languages. They are explained in detail here:
In an object-oriented environment, an object is a physical or conceptual real-world element.
- Unique and distinct from other objects in the system.
- State that indicates certain properties and values belonging to the particular object.
- Behavior that indicates its interaction with other objects or its externally visible activities.
An example of a physical object is a dog or a person whereas an example of a conceptual one is a process or a product.
A class is a blueprint or prototype for an object and represents a set of objects that are created from the same. Objects are basically instances of these classes. A class consists of –
- Objects of the same class can be different from each other in terms of values in their attributes. These attributes are known as class data.
- The operations which identify and display the behavior of these objects are known as functions and methods.
Suppose, there is a class called Student. The attributes of this class can be –
- Marks of the student
- Department in which the student studies
- An academic year of study
- Personal Identity i.e name, roll number, date of birth, etc.
Some of the operations to be performed can be indicated using the following functions –
- averageMarks() – calculates the average marks of the student.
- totalMarks() – calculates the total marks of the student.
- libraryFine() – calculates the fine that the student needs to pay for returning books late to the library.
This is demonstrated in the code below:
The total marks is = 192. Th average is = 96.0.
Encapsulation and Data Hiding
To protect our data from being accessed and exploited by outside usage, we need to perform encapsulation. This is explained in detail below –
The process of binding methods and attributes together in a class is called encapsulation. If an interface is provided by a class, only then encapsulation allows external access to internal details or class attributes.
The process by which an object is protected from direct access by external methods is called data hiding.
- setStudentValues() – assigns values to department, academic, and all personal identities of the student.
- getStudentValues() – to get these values stored in the respective attributes.
A number of objects are required to make an application interactive. The message is passed between objects using the following features –
- In message passing, objects from different processes can be involved.
- Class methods need to be invoked in message passing.
- Between two objects, message passing is usually unidirectional.
- Interaction between two objects is enabled in message passing.
The above example is demonstrated in the code below –
Roll number of student : 23. The name of student : Deepthi. Department of the student : CSE. The total marks is = 192. The average is = 96.0.
The process by which new classes are generated from existing classes by maintaining some or all of its properties is called inheritance. The original classes through which other classes can be generated are classed parent class or superclass or base class whereas the generated classes are known as derived classes or subclasses.
For a class Vehicle, derived classes can be a Car, Bike, Bus, etc. In this example, these derived classes are passed down the properties of their parent class Vehicle along with their own properties like the number of wheels, seats, etc. The above example is demonstrated in the following code –
Car belongs to- automobiles Car has 4 wheels.
Types of inheritance
- Single inheritance – One derived class generated out of a single base class.
- Multiple inheritance – One derived class generated out of two or more base classes.
- Multilevel inheritance – One derived class is generated out of a base class which is also generated out of another base class.
- Hierarchical inheritance – A group of derived classes generated out of a base class which in turn might have derived classes of their own.
- Hybrid inheritance – A lattice structure out of a combination of multilevel and multiple inheritances.
The ability in which objects can have a common external interface with different internal structures. While inheritance is implemented, Polymorphism is particularly effective. In polymorphism, functions can have the same names but different parameter lists.
Example: A Car and a Bus class both can have wheels() method but the number of wheels for both is different so since the internal implementation is different, no conflict arises.
Generalization and Specialization
The representation of the hierarchy of different classes where derived classes are generated out of base classes –
The combination of common characteristics from derived classes in order to form a generalized base class. Example – “A cow is a land animal”.
The distinction of objects from existing classes into specialized groups is specialization. This is almost like a reverse process of generalization.
The following diagram demonstrates generalization vs specialization –
Links and Association
Link: The representation of a connection in which an object collaborates with other objects i.e the relationship between objects is called a link.
Association: A set of links that identify and demonstrated the behavior between objects is called association. The instances of associations are called links.
Degree of Association
There are three types of Association:
- Unary relationship: Objects of the same class get connected.
- Binary relationship: Objects of two classes are connected.
- Ternary relationship: Objects of three or more classes are connected.
Cardinality of Binary Association
- One-to-One: One object of class A associated with an object of B.
- One-to-Many: One object of class A associated with more than one object of class B.
- Many-to-Many: More than one object of class A associated with more than one object from class B.
Composition or Aggregation
A class can generally be made using a combination of other classes and objects. This is class composition or aggregation. The “has-a” or “part-of” of a relationship is generally the aggregate. If an object consists of other objects then this object is called an aggregate object.
In a student-books relationship, the student “has-a” book and the book is a “part-of” the student’s curriculum. Here the student is the complete object. An aggregate includes –
- Physical containment – For example, a bag consists of zips and a bottle holder.
- Conceptual containment – For example, a student has marks.
Advantages of the Object Model
- Enable DRY (Don’t repeat yourself) way of writing code.
- While integrating complex systems, reduces development risks.
- Enables quick software development.
- Enables upgrades quite easily.
- Makes software easier to maintain.