Object Model in Java

28 Jan, 2022
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:
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.
Features:
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.
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.
Example:
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:
Java
// package whatever import java.io.*; public class Student {       public static void main (String[] args) {        // passing parameters to functions        int tot = total_marks(93,99);        double avg = avg_marks(tot, 2);             // printing the output        System.out.println("The total marks is = "+tot+". Th average is = "+avg+".");    }     // function to calculate total  public static int total_marks(int math, int science) {    int total = math + science;    return total;  }   // function to calculate average marks  public static double avg_marks(int total, int num_subs) {   double avg = total/num_subs;   return avg;  }}
Output:
The total marks is = 192. Th average is = 96.0.
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. 
Example:
setStudentValues() – assigns values to department, academic, and all personal identities of the student.
getStudentValues() – to get these values stored in the respective attributes.
Message Passing 
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 –
Java
// package whatever import java.io.*; public class Student {    private int rollNo;   private String name;   private String dep;     // default constructor    public Student() {}     public Student(int rollNo, String name, String dep)    {        this.rollNo = rollNo;        this.name = name;        this.dep = dep;    }     // Methods to get and set the student properties    public int getRollNo() { return rollNo; }     public void setRollNo(int rollNo)    {        this.rollNo = rollNo;    }     public String getName() { return name; }     public void setName(String name) { this.name = name; }     public String getDep() { return dep; }     public void setDep(String dep) { this.dep = dep; }     // function to calculate total    public static int totalMarks(int math, int science)    {        int total = math + science;        return total;    }     // function to calculate average marks    public static double avgMarks(int total, int num_subs)    {        double avg = total / num_subs;        return avg;    }     public static void main(String[] args)    {        // setting student attributes        Student s1 = new Student(23, "Deepthi", "CSE");        // setRollNo(23);        // setName("Deepthi");        // setDep("CSE");                 // printing student attributes        System.out.println(            "Roll number of student : " + s1.getRollNo()            + ". The name of student : " + s1.getName()            + ". Department of the student : " + s1.getDep() + ".");               // passing parameters to functions        int tot = totalMarks(93, 99);        double avg = avgMarks(tot, 2);               // printing the output        System.out.println("The total marks is = " + tot                           + ". The average is = " + avg                           + ".");    }}
Output:
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.
Example:
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 –
Java
class Vehicle {    String belongsTo = "automobiles";}public class Car extends Vehicle {    int wheels = 4;    public static void main(String args[])    {        Car c = new Car();        System.out.println("Car belongs to- "                           + c.belongsTo);        System.out.println("Car has " + c.wheels                           + " wheels.");    }}
Output:
Car belongs to- automobiles
Car has 4 wheels.
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.
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.
The representation of the hierarchy of different classes where derived classes are generated out of base classes –
Generalization
The combination of common characteristics from derived classes in order to form a generalized base class. Example – “A cow is a land animal”.
Specialization
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 –
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.
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.
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.
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.
Example:
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.
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.
germanshephered48
Java-Object Oriented
Picked
Java
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Stream In Java
Introduction to Java
Constructors in Java
Exceptions in Java
Generics in Java
Functional Interfaces in Java
Java Programming Examples
Strings in Java
Differences between JDK, JRE and JVM
Abstraction in Java