Types of classes

Today and over the last couple of days we have been talking about classes in Java. I’m fairly certain I understand them enough to now talk about them here. Tonight I will introduce them and continue the discussion later.

Basically, a class “consists of a collection of types of encapsulated instance variables and types of methods, possibly with implementation of those types together with a constructor function that can be used to create objects of the class. A class is a cohesive package that consists of a particular kind of compile-time metadata. A Class describes the rules by which objects behave; these objects are referred to as “instances” of that class. …” In basic terms, it is the blue prints of an object. Inside of a class, you will find instance varibles with values, methods to manipulate these variables, and constructors of those objects. If we were to make a class called Student , it might look something like this:

public class Student{

private String name;
private int age;
private float gpa;
private char status;

public Student (String stuName, int stuAge, float stuGpa, char stuStat){
name= stuName;
age= stuAge;
gpa= stuGPA
status= stuStat

}
public Student(){
name= “”;
age= 0;
gpa= 0.0;
status= null;
}

}

This class has 2 methods- both constructors- and 4 instance variables in it. The variables are initialized, but have no values. This is where the constructors come in. The first constructor for Student has 4 arguments sent to it. One for name, age, gpa and status. The constructor takes these 4 values sent by the client (NOT necessarily a user) and applies them to our 4 instance variables. The second constructor, which has no arguments, simply creates an object where all of them variables are set to nothing values.

For right now we only have constructors in our class. Next time, I will talk about other possible methods for our Student class, and subclasses that might inherit out Student class. Later

PS- to read ahead, and check out the counter class we have been working on in class, you can find it here
I know this was a little rushed, so if you have any questions, feel free to comment them or email me.

Leave a Reply

Your email address will not be published. Required fields are marked *