July 20, 2022

Class in Java

A class is the foundation for object oriented programming in Java language. Any logic that has to be implemented in Java must be encapsulated with in the class.

Once a class is created in Java that defines a new data type which can be used for creating object of that data type. Thus, in the context of an object oriented language like Java, a class provides a template that is used to create objects.

Class declaration in Java

In general, class declarations in Java can include these components, in order-

  1. Modifiers- A class can have public or default (no modifier specified) access modifiers.
  2. Class name- The class name, with the initial letter capitalized by convention.
  3. Super class- If a class has a super class then the name of the class's parent (superclass) preceded by the keyword extends. A class can only extend (subclass) one parent.
  4. Interfaces- If class is implementing any interface(s) then specify the comma-separated list of interfaces preceded by the keyword implements. A class can implement more than one interface.
  5. The class body, surrounded by braces, {}.

The class body (the area between the braces) contains all the code that provides for the life cycle of the objects created from the class:

  1. Constructors- Constructors are used for initializing new objects,
  2. Field declarations- Declarations for the fields that provide the state of the class and its objects. Fields defined within a class are also known as instance variables because each instance of the class (object) gets its own copy of these variables.
  3. Methods- Any code is written with in the methods and that implementation defines the behavior of the class and its objects.

Generally the fields with in the class have restricted access, only the code written with in the methods of that class can access and manipulate the data that is why class is the foundation for encapsulation OOPS concept in Java.

Form of class in Java

On the basis of what we have read so far about the declaration of the class, general form of the class in Java is as follows-

class MyClass extends MySuperClass implements YourInterface {
  type instance_var 1;
  type instance_var 2;
  // Constructor
  MyClass(){
  }

  return_type method1(parameters){
    ..
  } 
  return_type method2(){
    ..
  }
}

Class example in Java

Here is a simple example of creating a class with fields, constructor and method.

public class MyClass {
  int num;
  String name;
  // Constructor
  MyClass(int num, String name){
    this.num = num;
    this.name = name;
  }

  public void displayValues() {
    System.out.println("Num- " + num + " Name- " + name);
  }
  public static void main(String[] args) {
    // Creating object of the class
    MyClass myObj = new MyClass(10, "knpCode");
    myObj.displayValues();
  }
}
Output
Num- 10 Name- knpCode

Class in Java- Access and non-access modifiers

Classes in Java can have public or default (package) access modifier.

  • A public class is visible to all classes everywhere.
  • A class with default access is visible within its own package.

A nested class (inner class) can have any access modifier private, protected, public or default.

Non-access modifiers that are permitted with a class in Java are-

  1. final
  2. abstract
  3. strictfp

A nested class can have static non-access modifier too.

Reference: https://docs.oracle.com/javase/tutorial/java/javaOO/classdecl.html

That's all for the topic Class in Java. If something is missing or you have something to share about the topic please write a comment.


You may also like

No comments:

Post a Comment