Saturday, December 28, 2019

Classes and Objects in java

Classes and Objects in java

Class


Class is a set of objects. Class is used to classify, things having common attributes and behaviors. For example, the Student is a class in which all people studying can be kept. The class provides the structure for the Object. The class can be considered as a Blueprint, Template, or Factory to create identically structured and behavioral objects.

Class Dog with multiple objects
Class Dog with multiple objects

Object


The object is an abstract data type. The object has attributes, behavior, identity, or the state. An object is an instance of the class. A programming language is said to object-oriented programming language if it is object-based and has the power of polymorphism and inheritance. For example class Dog with the attribute, color can have many dog objects with different colors


The relation between Class and Object 


  • Class is a logical construct
  • An object is a physical reality
  • An object is an instance of a class


Classes and Objects in Java
Classes and Objects in Java


A Java class can contain the declarations of variables and methods.

java class variables and methods
Java class

The class can have three kinds of members in Java.

  • Fields
  • Methods
  • Nested Classes and Interfaces
We can also nest other classes and interfaces in a class.

Example of Class members

public class Employee { //class declaration

int emp_id;            //Attributes/fields(static or non static)
String emp_name;
int basic_sal;

public void displaySalary() //method(static or non static)
{
      System.out.println("salary:"+basic_sal);
}

class Address                 //nested class
{
      String city;
      String state;
      String pin;
}
}



Class modifiers

Java also has four class modifiers

public


"public" means the class is accessible its own package and other than this package. Any class with no modifier is only accessible in its own package. 


abstract


"abstract" modifier means that the class is Abstract Class, having one or more abstract methods. The abstract methods are incomplete methods or methods without implementation.


final


A "final" declared class can not be inherited by the subclasses. The "final " keyword with a class is used to restrict the inheritance(read Inheritance in Java).


strictfp


"strictfp" means that the strict floating-point evaluation will be used with this class for arithmetic evaluations.


Creating a class in Java

In the example given below, the class Employee has attributes and methods.



Objects can be declared using object reference like


Employee employee; 
Student student;

An object is created by using the "new" keyword followed by the constructor of the class.


Employee emp=new Employee(); 
Student std=new Student();

The "new " keyword 

The "new" keyword,
  • Dynamically allocates memory for the object on heap space.
  • Creates object reference on the heap stack.
  • Returns a reference to it.
  • Reference is stored in a variable.
Object references are used to store objects. The reference to the object can be created for any type of class(Abstract or concrete class) or interface(see Interfaces in Java).

Video Tutorial