Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Java Arrays
  • Java Strings
  • Java OOPs
  • Java Collection
  • Java 8 Tutorial
  • Java Multithreading
  • Java Exception Handling
  • Java Programs
  • Java Project
  • Java Collections Interview
  • Java Interview Questions
  • Java MCQs
  • Spring
  • Spring MVC
  • Spring Boot
  • Hibernate
Open In App
Next Article:
Object Model in Java
Next article icon

Object Model in Java

Last Updated : 28 Jan, 2022
Comments
Improve
Suggest changes
Like Article
Like
Report

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:

Object

In an object-oriented environment, an object is a physical or conceptual real-world element.

Features:

  1. Unique and distinct from other objects in the system.
  2. State that indicates certain properties and values belonging to the particular object.
  3. 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.

Class

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.

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 -

Encapsulation

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. 

Data Hiding

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.

Inheritance 

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.

Types of inheritance 

  1. Single inheritance - One derived class generated out of a single base class.
  2. Multiple inheritance - One derived class generated out of two or more base classes.
  3. Multilevel inheritance - One derived class is generated out of a base class which is also generated out of another base class.
  4. Hierarchical inheritance - A group of derived classes generated out of a base class which in turn might have derived classes of their own.
  5. Hybrid inheritance - A lattice structure out of a combination of multilevel and multiple inheritances.

Polymorphism

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 -

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 -

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.

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.

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.

Next Article
Object Model in Java

T

tejsidda34
Improve
Article Tags :
  • Java
  • Java-Object Oriented
Practice Tags :
  • Java

Similar Reads

    Object toString() Method in Java
    Object class is present in java.lang package. Every class in Java is directly or indirectly derived from the Object class, henceforth, it is a child of the Object class. If a class does not extend any other class then it is a direct child class of Object, and if it extends another class, then it is
    3 min read
    Object Class in Java
    Object class in Java is present in java.lang package. Every class in Java is directly or indirectly derived from the Object class. If a class does not extend any other class then it is a direct child class of the Java Object class and if it extends another class then it is indirectly derived. The Ob
    7 min read
    Reflection in Java
    Reflection is an API that is used to examine or modify the behavior of methods, classes, and interfaces at runtime. The required classes for reflection are provided under java.lang.reflect package which is essential in order to understand reflection. So we are illustrating the package with visual ai
    5 min read
    new operator in Java
    When you are declaring a class in java, you are just creating a new data type. A class provides the blueprint for objects. You can create an object from a class. However obtaining objects of a class is a two-step process : Declaration : First, you must declare a variable of the class type. This vari
    5 min read
    Anonymous Object in Java
    In Java, an anonymous object is an object that is created without giving it a name. Anonymous objects are often used to create objects on the fly and pass them as arguments to methods. Here is an example of how to create and use an anonymous object in Java. Example:Java import java.io.*; class Perso
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences