DEV Community

Kaviya Kannan
Kaviya Kannan

Posted on

class and object in java:

classes and object:
The classes and object are the basic concepts of object oriented programming (oops) that are used to represent real world concept and entities.[]
The class represents a group of object having similar properties and behavior.

for example:
The animal type dog:
DOG-is a class while a particular dog named as TOMMY is an object of the dog.

class:
A class in java is a set of objects which shares common characteristics/behavior and common properties/attributes.
It is a user defined blueprint prototype from which objects are created.
class doesn't allocated memory when it is created.
class is declared using class keyword
class Employee{}
here is only one way to define a class is that by using class keyword.

for example:
STUDENT is a class while a particular student named RAVI is an object.
object:
object is said to be an physical entity and it represent a class.
object represent an real life entities and memory reference of class.
object are the instances of a class that are created to use the attributes and methods of a class.java program create many object.
an object consists of states,behavior and identity.

  • for example:
  • the example of object :DOG
  • identity--name of dog
  • state/attributes--age,color,breed
  • behaviors--bark,sleep,eat

creation of object:
object is created through new keyword.
employee ob=new employee();

1.How to Create Object in Java

  The object is a basic building block of an object oriented       language(oops). In Java, we cannot execute any program without creating an object. There is various way to create an object in Java that we will discuss in this section, and also learn how to create an object in Java.
Enter fullscreen mode Exit fullscreen mode

Java provides five ways to create an object.

Using new Keyword
Using clone() method
Using new Instance() method of the Class class
Using new Instance() method of the Constructor class
Using deserialization
Enter fullscreen mode Exit fullscreen mode

2.How method is called in java:

  A method in Java is a group of statements that carry out a certain action or function. It is commonly used because it enables code reuse, which refers to the ability to write code once and use it multiple times.
Enter fullscreen mode Exit fullscreen mode

To call a method in Java, you need to follow these basic steps:

  Define the method: This includes giving the method a name, specifying any arguments that it takes, and defining what the method should do when it is called.
  Create an instance of the class: If the method you want to call is part of a class, you need to create an instance of that class.
  Call the method: Once you have an instance of the class (if necessary), you can use the name of the method, followed by any arguments that the method takes (if any) to call the method.
Enter fullscreen mode Exit fullscreen mode

How method is defined in java:

 To define a method in Java, you need to declare it within a class.

 The following elements in the method declaration: 
Enter fullscreen mode Exit fullscreen mode

Image description

    Return type: The data type of the value the method returns,        or void if it doesn't return a value
    Method name: The name of the method
    Parentheses: A pair of parentheses, ()
    Method body: The code for the method, enclosed in braces, {} 
Enter fullscreen mode Exit fullscreen mode

Other elements that can be included in a method declaration are:

   Modifiers: Such as public or private
   Parameter list: A comma-delimited list of input parameters, preceded by their data types
   Exception list: An exception list
Enter fullscreen mode Exit fullscreen mode

Program Task:

Image description

Top comments (0)