What is Class in Java

In this article we learn what is class in java, how to declare class and how to access members of the class.

What is Class in Java?

1. A class in Java is a blueprint which all the data.

2. It describes the state and behavior of a specific object.

3. In Java, we cannot declare a top-level class as private. Java allows only public and default access specifiers for top-level classes. We can declare inner classes as private.

4. We can include any type of the variables in Java – local, global variables.

5. There can be only one public class in a single program and its name should be the same as the name of the Java file. There can be more than one non-public classes in a single Java file.

6. A public class is visible to all classes from all the packages.

7. A class with default access is visible only to the classes within the same package.

8. We can also use the non-access modifiers for the class such as final, abstract and strictfp. This is called default/package accessibility.

9. We cannot create an object or instance of an abstract class.

10. No subclasses or child class can be created from a class that is declared as final.

11. A class cannot be declared both as final and abstract at the same time.

Syntax of Class in Java

class classname {

// member variable

// member functions

}

Can we declare a top-level class as protected or private in Java?

If a top-level class is declared as private the compiler will complain that the modifier private is not allowed here. This means that a top-level class cannot be a private, the same can be applied to protected access specifier also.

Declaration of Class in Java

In order to bring class into existence, we should declare it. We can declare a class with the use of a class keyword.

The components of the Java Class declaration are:

1. Access Modifiers: We can access Java classes using any access modifiers such as public, private, protected and default.

2. Class Name: In Java, the class name generally represents nouns which should begin with a capital letter without any spaces.

3. Superclass (if any): The name of the parent class is a superclass and its child class is a subclass, and child class inherits the properties of a parent using the extends keyword. A subclass can only inherit a single parent.

4. Interfaces (if any): To declare an interface, we just write the keyword interface followed by the interface name.

5. Class Body: The class body follows the class declaration and embeds within curly braces {}.

Accessing the members of a Class in Java:

We can access the data members of a class using the object of the class. We just write the name of the object which is followed by a dot operator then we write the name of the data member (either variables or methods) which we want to access.

Syntax of accessing data members and methods of a Java Class:

objectName.variableName;        //accessing the variables

objectName.methodName(); //accessing the methods

Multiple Java Classes

Multiple classes means that we can create an object of a class and use it in another class. Also, we can access this object in multiple classes.

Other Popular Articles

Why and How to Use Comments in Java?

Leave a Comment