Java Classes
The core of Java is the class. A class defines the form and characteristics of an object, so it is a logical structure in the Java language as a whole and forms the foundation of object-oriented programming. Programs are written by creating objects as instances of classes.
Programming is often compared to a building. Using that comparison, a class is a building blueprint, and an object can be seen as a building created from that blueprint. Objects are often compared to all the things and living beings we commonly see.
Class Members
The word member means a component. A class also has members. Broadly speaking, it consists of member variables that correspond to class attributes, and constructor and method definitions that correspond to class functions.
- Class
- Member variable
- Member method
- Constructor
- General method
Class Definition
A class is defined with the following structure.
[public/private] [final/abstract] class ClassName {
// Member variables
// Constructor
// Methods
}
A simple code example is shown below.
package com.devkuma.tutorial.clazz;
public class MyClass {
// Member variable
int a;
/**
* Constructor
*/
public MyClass() {
// Constructor body
}
/**
* Method
*/
public void method() {
// Method body
}
}
Class Declaration
Declaring a class is like creating a new type.
By convention, class names start with an uppercase letter.
Access Control Modifiers
-
public
- With
public, objects can be created from all classes.
- With
-
private
- With
private, creation is possible only from an inner class in another class.
- With
-
No access modifier
- A class declared without an access modifier can be instantiated only from classes in the same package.
final Classes
A class with final cannot have subclasses. In other words, it is a class that cannot be inherited. Preventing the current class from being inherited by another class is useful for protecting information.
The concept of inheritance will be explained again in more detail in another chapter.
abstract Classes
A class with abstract is an abstract class. It is a class from which objects cannot be created directly, and it can be used only when inherited by a subclass.
The concept of abstract classes will be explained again in more detail in another chapter.
Creating Objects with the new Keyword
Creating an object means dynamically allocating memory for the object.
To create an object using a class, first declare a variable of the class type, just as you would declare a normal data type.
Class and object are sometimes used interchangeably, but they are different things. A class defines the form of an object, but it is not the object itself. An object is a concrete entity based on a class and is also called an instance of the class.
Declaring an object is the same as declaring a variable. First comes the class name, followed by the object variable name.
ClassName objectVariableName;
MyClass myclass;
To create an object, use the new keyword. The class name on which the object is based comes after the new keyword.
objectVariableName = new ClassName();
myclass = new MyClass();
You can also declare and create an object at the same time.
ClassName objectVariableName = new ClassName();
MyClass myclass = new MyClass();