Classes and object program in java

What is Class and Object in Java OOPS? Learn with Example

Classes and Objects in Java are the fundamental components of OOP’s. Often there is a confusion between classes and objects. In this tutorial, we try to tell you the difference between Class and Object in Java.

First, let’s understand what they are,

What is Class in Java?

Class are a blueprint or a set of instructions to build a specific type of object. It is a basic concept of Object-Oriented Programming which revolve around the real-life entities. Class in Java determines how an object will behave and what the object will contain.

Syntax of Class in Java

What is Object in Java?

Object is an instance of a class. An object in OOPS is nothing but a self-contained component which consists of methods and properties to make a particular type of data useful. For example color name, table, bag, barking. When you send a message to an object, you are asking the object to invoke or execute one of its methods as defined in the class.

From a programming point of view, an object in OOPS can include a data structure, a variable, or a function. It has a memory location allocated. Java Objects are designed as class hierarchies.

Object Syntax in Java

ClassName ReferenceVariable = new ClassName();

What is the Difference Between Object and Class in Java?

A Class in object oriented programming is a blueprint or prototype that defines the variables and the methods (functions) common to all Java Objects of a certain kind.

An object in OOPS is a specimen of a class. Software objects are often used to model real-world objects you find in everyday life.

Click here if the video is not accessible

Читайте также:  Java создать массив символов

Understand the concept of Java Classes and Objects with an example.

Let’s take an example of developing a pet management system, specially meant for dogs. You will need various information about the dogs like different breeds of the dogs, the age, size, etc.

You need to model real-life beings, i.e., dogs into software entities.

Java Classes and Objects

Moreover, the million dollar question is, how you design such software?

Here is the solution-

First, let’s do an exercise.

You can see the picture of three different breeds of dogs below.

Java Classes and Objects

Stop here right now! List down the differences between them.

Some of the differences you might have listed out maybe breed, age, size, color, etc. If you think for a minute, these differences are also some common characteristics shared by these dogs. These characteristics (breed, age, size, color) can form a data members for your object.

Java Classes and Objects

Next, list out the common behaviors of these dogs like sleep, sit, eat, etc. So these will be the actions of our software objects.

Java Classes and Objects

So far we have defined following things,

  • Class – Dogs
  • Data members or objects– size, age, color, breed, etc.
  • Methods– eat, sleep, sit and run.

Java Classes and Objects

Now, for different values of data members (breed size, age, and color) in Java class, you will get different dog objects.

Java Classes and Objects

You can design any program using this OOPs approach.

While creating a class, one must follow the following principles.

  • Single Responsibility Principle (SRP)- A class should have only one reason to change
  • Open Closed Responsibility (OCP)- It should be able to extend any classes without modifying it
  • Liskov Substitution Responsibility (LSR)- Derived classes must be substitutable for their base classes
  • Dependency Inversion Principle (DIP)- Depend on abstraction and not on concretions
  • Interface Segregation Principle (ISP)- Prepare fine grained interfaces that are client specific.

Classes and Objects in Java Example Programs

// Class Declaration public class Dog < // Instance Variables String breed; String size; int age; String color; // method 1 public String getInfo() < return ("Breed is: "+breed+" Size is:"+size+" Age is:"+age+" color is: "+color); >public static void main(String[] args) < Dog maltese = new Dog(); maltese.breed="Maltese"; maltese.size="Small"; maltese.age=2; maltese.color="white"; System.out.println(maltese.getInfo()); >>
Breed is: Maltese Size is:Small Age is:2 color is: white

Java Object and Class Example: main outside class

In previous program, we are creating main() method inside the class. Now, we create classes and define main() method in another class. This is a better way than previous one.

// Class Declaration class Dog < // Instance Variables String breed; String size; int age; String color; // method 1 public String getInfo() < return ("Breed is: "+breed+" Size is:"+size+" Age is:"+age+" color is: "+color); >> public class Execute < public static void main(String[] args) < Dog maltese = new Dog(); maltese.breed="Maltese"; maltese.size="Small"; maltese.age=2; maltese.color="white"; System.out.println(maltese.getInfo()); >>
Breed is: Maltese Size is:Small Age is:2 color is: white

Summary:

  • Java Class is an entity that determines how Java Objects will behave and what objects will contain
  • A Java object is a self-contained component which consists of methods and properties to make certain type of data useful
  • A class system allows the program to define a new class (derived class) in terms of an existing class (superclass) by using a technique like inheritance, overriding and augmenting.
Читайте также:  Serialize или json php

Источник

Java Classes and Objects

Everything in Java is associated with classes and objects, along with its attributes and methods. For example: in real life, a car is an object. The car has attributes, such as weight and color, and methods, such as drive and brake.

A Class is like an object constructor, or a «blueprint» for creating objects.

Create a Class

To create a class, use the keyword class :

Main.java

Create a class named » Main » with a variable x:

Remember from the Java Syntax chapter that a class should always start with an uppercase first letter, and that the name of the java file should match the class name.

Create an Object

In Java, an object is created from a class. We have already created the class named Main , so now we can use this to create objects.

To create an object of Main , specify the class name, followed by the object name, and use the keyword new :

Example

Create an object called » myObj » and print the value of x:

Multiple Objects

You can create multiple objects of one class:

Example

Create two objects of Main :

Using Multiple Classes

You can also create an object of a class and access it in another class. This is often used for better organization of classes (one class has all the attributes and methods, while the other class holds the main() method (code to be executed)).

Remember that the name of the java file should match the class name. In this example, we have created two files in the same directory/folder:

Читайте также:  Python line count text file

Main.java

Second.java

When both files have been compiled:

You will learn much more about classes and objects in the next chapters.

Источник

Classes and object program in java

  • Introduction to Java
  • The complete History of Java Programming Language
  • C++ vs Java vs Python
  • How to Download and Install Java for 64 bit machine?
  • Setting up the environment in Java
  • How to Download and Install Eclipse on Windows?
  • JDK in Java
  • How JVM Works – JVM Architecture?
  • Differences between JDK, JRE and JVM
  • Just In Time Compiler
  • Difference between JIT and JVM in Java
  • Difference between Byte Code and Machine Code
  • How is Java platform independent?
  • Decision Making in Java (if, if-else, switch, break, continue, jump)
  • Java if statement with Examples
  • Java if-else
  • Java if-else-if ladder with Examples
  • Loops in Java
  • For Loop in Java
  • Java while loop with Examples
  • Java do-while loop with Examples
  • For-each loop in Java
  • Continue Statement in Java
  • Break statement in Java
  • Usage of Break keyword in Java
  • return keyword in Java
  • Object Oriented Programming (OOPs) Concept in Java
  • Why Java is not a purely Object-Oriented Language?
  • Classes and Objects in Java
  • Naming Conventions in Java
  • Java Methods
  • Access Modifiers in Java
  • Java Constructors
  • Four Main Object Oriented Programming Concepts of Java
  • Inheritance in Java
  • Abstraction in Java
  • Encapsulation in Java
  • Polymorphism in Java
  • Interfaces in Java
  • ‘this’ reference in Java

Источник

Оцените статью