Classes and Objects in Java

Classes and Objects in Java

In today’s topic, we discuss Classes and Objects in Java and how they perform a vital rule in the programming field.

Classes and Objects in Java are basic concepts of Object Oriented Programming that revolve around real-life entities.

Classes and Objects in Java
Classes and Objects in Java

Class

  1. A class is a collection of objects that have similar traits, behaviors, and attributes.
  1. Class does not exist in the real world. It merely serves as a model, blueprint, or prototype from which objects can be made.
  2. Memory is not taken up by class.
  3. A class is a collection of variables with various data types and a collection of methods.
  • In Java, a class could include:
  •  data member
  • method
  • constructor
  • nested class and 
  • interface

Syntax to declare a class

Syntax to declare a class:
 class<class_name>
{  
    data member;  
    method;  
    constructor;
    nested class;
    interface;
}

Example

class Employes
{
 int id;//data member (always instance variable)
 String name; //data member (always instance variable)

 public static void main(String args[])
 {
  Employes e1=new Student();//creating an object of Student
  System.out.println(e1.id);
  System.out.println(e1.name);
 }
}

Output

o
null

A user-defined class serves as a template or prototype from which objects are made. It stands for the collection of attributes or operations that are shared by all objects of a particular type. The following elements can typically be found in class declarations, in that order:

Modifiers: A class may have default access or be made public (see this for more information).
Class keyword: a class is created using a class keyword.
class name: The name must start with a letter (capitalized by convention).
superclass: The keyword extends is followed by the name of the class’s parent (superclass). Only one parent can be extended (subclassed) by a class.
Interfaces: A comma-separated list of interfaces implemented by the class, if any, preceded by the keyword implements. A class can implement more than one interface Body: Braces, or surround the class body.

New objects are initialized using constructors. While methods are used to implement the behavior of the class and its objects, fields are variables that provide the state of the class and its objects.

Object

This fundamental building block of Object-Oriented Programming represents actual people and things. Many objects are created by a typical Java programme, and as you are aware, these objects interact by calling methods. An object includes:

State: An object’s attributes serve as a representation of it. Additionally, it reflects an object’s characteristics.
Behavior: It is represented by an object’s methods. Additionally, it depicts how an object interacts with other objects.
Identity: It gives an object a special name and makes it possible for one object to communicate with another.

Declaring Objects (Also called instantiating a class)

A class is said to be instantiated when an object belonging to it is produced. The class’s characteristics and behavior are shared by all instances. However, the states of those attributes, or their values, are particular to each object. Any number of instances may exist for a single class.

When declaring variables, for example (type name;). By doing this, we are telling the compiler that we’ll be using the name to refer to data of type type. This declaration also makes the appropriate amount of memory available for a primitive variable. Therefore, the type of a reference variable must strictly be a concrete class name. In general, we are unable to produce objects that belong to an abstract class or interface.

Syntax to declare a class

class Student{  
 int id;  
 String name;  
}  
//Creating another class TestStudent1 which contains the main method  
class TestStudent1{  
 public static void main(String args[]){  
  Student s1=new Student();  
  System.out.println(s1.id);  
  System.out.println(s1.name);  
 }  
}  

Example

class Student{  
 int id;  
 String name;  
}  
class Student2{  
 public static void main(String args[]){  
  Student s1=new Student();  
  s1.id=100;  
  s1.name="sinphun";  
  System.out.println(s1.id+" "+s1.name);//printing members with a white space  
 }  
}  

Output

100 sinphun

It is all about the explanation of Classes and Objects in Java

For more read with us Oraltadalafil

Read our previous post

1 thought on “Classes and Objects in Java”

Leave a Comment

%d bloggers like this: