Home > Java > javaTutorial > class and object in java:

class and object in java:

Mary-Kate Olsen
Release: 2024-12-30 15:28:14
Original
675 people have browsed it

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.
Copy after login

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
Copy after login

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.
Copy after login

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.
Copy after login

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: 
Copy after login

class and object in java:

    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, {} 
Copy after login

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
Copy after login

Program Task:

class and object in java:

The above is the detailed content of class and object in java:. For more information, please follow other related articles on the PHP Chinese website!

source:dev.to
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template