Java is an Object Oriented Programming (OOP) language; Thus, understanding what it is and how it works is very important for going further. In this article, I would like to go through the core definitions of OOP such as class, constructor and instance.
Class, what the heck is it?
Class is an abstract thing used to describe one thing generally. For example, we have a class named Student. By that way, a student would have some properties such as studentName, studentID, studentAge, grade.
class Student{ private int studentAge; private String studentName; private String studentId; private int grade; }
In other words, the class is a type of data which will be defined by the programmer. In a class, we can declare the properties, functions, etc.
What about the Constructor
A Constructor of the class is a method that is used to create an Object which is belonged to that class. A constructor would be:
- Same name as class’s name
- A constructor would have no argument, one argument or more than one argument. No argument constructor is called Default Constructor
- A class can have more than one Constructor
class Student{ private int studentAge; private String studentName; private String studentId; private int grade; //Constructor public Student(int age, String name, String ID, int grade){ studentAge = age; studentName = name; studentId = ID; this.grade = grade; } //Default constructor public Student(){} //accessor int getAge(){return studentAge;} int getGrade(){return grade;} String getName(){return studentName;} String getId(){return studentId;} public String toString(){ return "Name: "+ studentName +"\nID: "+ studentId + "\nAge: "+ studentAge; } }
We currently have a general student. There is two way to create a student; we either use default constructor or use the constructor which has arguments.
The code below will describe a school which has many student, I want to create 2 students including student Alpha and student Beta.
public class School{ public static void main(String[] args){ //creating new students Student alpha = new Student(23, "Alpha", "A435",80); Student beta = new Student(); //printing out the information of the student System.out.println(alpha.toString()); System.out.println(); System.out.println(beta.toString()); } } class Student{ private int studentAge; private String studentName; private String studentId; private int grade; //Constructor public Student(int age, String name, String ID, int grade){ studentAge = age; studentName = name; studentId = ID; this.grade = grade; } //Default constructor public Student(){} //accessor int getAge(){return studentAge;} int getGrade(){return grade;} String getName(){return studentName;} String getId(){return studentId;} public String toString(){ return "Name: "+ studentName +"\nID: "+ studentId + "\nAge: "+ studentAge; } }
Name: Alpha ID: A435 Age: 23 Name: null ID: null Age: 0
In the program above, student Alpha had all the information that we need for it. In another hand, the information of Beta was assigned as Null. By this example, we can see that an Object that was created by default constructor will have no value.
Instance
So far, we know that Class is used to describe the objects generally. In the case, I want to state the specific object such as student A, and student B, those students are called the instance of class Student.
Conclusion
Overall, there are something about class in Java and OOP. In the next article, I will write about the inheritance and its properties.
Best post ever!
LikeLiked by 1 person