Java Classes
A class in Java is a set of objects which shares common characteristics/ behavior and common properties/ attributes. It is a user-defined blueprint or prototype from which objects are created. For example, Student is a class while a particular student named Ravi is an object.
Properties of Java Classes
- Class is not a real-world entity. It is just a template or blueprint or prototype from which objects are created.
- Class does not occupy memory.
- Class is a group of variables of different data types and a group of methods.
- A Class in Java can contain:
- Data member
- Method
- Constructor
- Nested Class
- Interface
Class Declaration in Java
access_modifier class <class_name> { data member; method; constructor; nested class; interface; }
Example of Java Class
Java
// Java Program for class example class Student { // data member (also instance variable) int id; // data member (also instance variable) String name; public static void main(String args[]) { // creating an object of // Student Student s1 = new Student(); System.out.println(s1.id); System.out.println(s1.name); } } |
Java
// Java program to Illustrate Creation of Object // Using new Instance // Main class class GFG { // Declaring and initializing string String name = "w3wiki" ; // Main driver method public static void main(String[] args) { // Try block to check for exceptions try { Class cls = Class.forName( "GFG" ); // Creating object of main class // using instance method GFG obj = (GFG)cls.newInstance(); // Print and display System.out.println(obj.name); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (InstantiationException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } } } |
Java
// Java program to Illustrate Creation of Object // Using clone() method // Main class // Implementing Cloneable interface class GFG implements Cloneable { // Method 1 @Override protected Object clone() throws CloneNotSupportedException { // Super() keyword refers to parent class return super .clone(); } String name = "w3wiki" ; // Method 2 // main driver method public static void main(String[] args) { GFG obj1 = new GFG(); // Try block to check for exceptions try { GFG obj2 = (GFG)obj1.clone(); System.out.println(obj2.name); } catch (CloneNotSupportedException e) { e.printStackTrace(); } } } |
Output 1
0
null
Output 2
w3wiki
Output 3
w3wiki
Components of Java Classes
In general, class declarations can include these components, in order:
- Modifiers: A class can be public or has default access (Refer this for details).
- Class keyword: class keyword is used to create a class.
- Class name: The name should begin with an initial letter (capitalized by convention).
- Superclass(if any): The name of the class’s parent (superclass), if any, preceded by the keyword extends. A class can only extend (subclass) one parent.
- Interfaces(if any): 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: The class body is surrounded by braces, { }.
Constructors are used for initializing new objects. Fields are variables that provide the state of the class and its objects, and methods are used to implement the behavior of the class and its objects.
There are various types of classes that are used in real-time applications such as nested classes, anonymous classes, and lambda expressions.
Classes and Objects in Java
In Java, classes and objects are basic concepts of Object Oriented Programming (OOPs) that are used to represent real-world concepts and entities. The class represents a group of objects having similar properties and behavior. For example, the animal type Dog is a class while a particular dog named Tommy is an object of the Dog class.
In this article, we will discuss Java objects and classes and how to implement them in our program.