Objects and Classes in JAVA

Objects and Classes in JAVA

Photo by Luca Bravo on Unsplash

Java is an object-oriented programming language. This means that it provides a way of modularizing programs by creating self-contained objects that can be used as building blocks. Once an object is created, it can be easily reused in other programs.

Java classes are like templates that you can use to create objects. A class definition starts with the keyword class followed by the class name; and the class body, enclosed by a pair of curly braces.

class MyClass {
 // class body }

To create an object of MyClass, we would use the following:

MyClass myObj = new MyClass();

Java objects are created from classes using the new keyword. A class can have more than one constructor.

A constructor is a block of code that is called when an instance of an object is created.

If a class does not have a constructor, Java will insert a default constructor into the compiled code. This default constructor will call the no-argument constructor of the superclass. If the superclass does not have a no-argument constructor, a compile-time error will occur.

In the example below, we create a class called MyClass with a constructor that takes a single argument, an int. We also create a second constructor that does not take any arguments.

class MyClass {
 int x;

 MyClass(int i) {
 x = i;
 }

 MyClass() {
 this(0);
 }
}

Java also supports something called inner classes. These are classes that are defined inside another class.

Inner classes have access to the members of the class in which they are defined. They can also be declared static, in which case they can only access static members of the outer class.

Here is an example of an inner class:

class MyClass {
 static class NestedClass {
 void myMethod() {
 // Code goes here
 }
 }
}

In Java, there are four access levels for classes and their members:

Public: The class and all its members are visible to all other classes.

Private: The class and all its members are visible only within the same class.

Protected: The class and all its members are visible only within the same package.

Default (no modifier): The class and all its members are visible only within the same package.

The following example shows how to use the different access levels:

public class myClass {
 private int x; // Only visible within myClass

 protected int y; // Visible within myClass and subclasses

 public int z; // Visible everywhere

 void myMethod() {
 // Code goes here
 }
}

Java also supports something called packages. A package is a way of grouping related classes and interfaces.

Packages are typically used to group related classes that work together, such as the java.util package which contains many useful classes for working with data.

Packages can be defined using the package keyword.

package myPackage;

Classes and interfaces can then be added to the package using the import keyword.

import myPackage.MyClass;

Packages can also be imported using the * wildcard character, which will import all classes in the package.

import myPackage.*;

Packages can be nested within other packages. In this case, the outer package is called the parent package and the inner package is called the subpackage.

Parent packages can access the classes and interfaces in their subpackages without importing them. However, subpackages cannot access the classes and interfaces in their parent packages.

The following example shows how to create and use packages:

// Create a package
package myPackage;

// Import a class from the package
import myPackage.MyClass;

public class Main {
 public static void main(String[] args) {
 // Create an instance of the class
 MyClass myObj = new MyClass();

 // Print the value of x
 System.out.println(myObj.x);
 }
}

Did you find this article valuable?

Support Snehasish Konger by becoming a sponsor. Any amount is appreciated!