top of page
Writer's pictureSai Charan

Classes

Updated: Jun 6, 2020

Now, this is what we've all been waiting for. The dreaded unit... classes. But it doesn't have to be like that! Trust my classes really aren't that bad once you understand how they work. And that's exactly what I want to do with this blog post. Get your cup of Java and sit down. Now let's explore the real power of Java: classes.


As always...the non-negotiable terms for success.


Access Level - Determines the level of access other files have to the class’s data or methods. Accessor - A method that allows public access to a private attribute.

Attributes - Stores an Object’s data.

Class Attribute - The Attribute is created once and it shared between all instances of the class. Class attributes can be accessed through the class name like Math.PI

Class Method - Can only access class attributes. Class methods can be accessed through the class name like Math.pow()

Constructors - Special methods used for constructing an Object.

Default Constructor - A constructor that receives 0 parameters. When a class has 0 constructors a hidden default constructor creates that will set all objects to null, booleans to false and all other primitives to 0.

Encapsulation - The combining of data (attributes) into a class. equals A method used to check the equality of two Objects. final Used to make a variable constant.

Information Hiding - When classes store their data privately prevent unwanted modifications. Instance Attribute - Every instance of an object gets its own copy of the variable.

Instance Methods - Can access both instance and class variables. It can only be accessed through an instance of the class.

Modifiers - Sets method or attribute properties.

Mutator - A method that publicly allows a private attribute to be changed.

package - A group of related classes.

private Data - cannot be accessed outside the file.

protected Data - can be modified by classes that share the same package or any subclass. public Data - can be fully accessed outside the file.

Static - Sets an attribute or method to be a class attribute or class method.

this - A keyword used to access the current instance of the class.

toString - A method used to return a text version of an Object.


With that out of the way, let's figure out what it all means!


To start, classes are essentially abstract concepts that can be duplicated over and over again. Imagine a car. No, it doesn't have to be a Ferrari. But, there are multiple cars out there, each with their own unique characteristics and attributes. These attributes are the first part of the class. In the case of a car, attributes can be the make of the car, the model, and the color of the car. However, there can also be different types of attributes. For instance, a class attribute is one that stays the same for all cars. So, a class attribute for a car would be the number of wheels. So, you would add the keyword static in front of this attribute. Now, let's say in the future, cars don't have any wheels. You don't have to make that change for every instance of a car. But, what you can do is change the class attribute within the class and change the number of wheels for all the cars to be 0.


The second part of any class is the constructor. This is the place where you make each car. Imagine this like a factory. Each car will be provided some instructions to assemble a custom car, known as parameters. The operator will remember this data. Translated to Java: a constructor is created, which stores the data provided into attributes. So, I will tell the car manufacturer I want a blue car with red seats. I know...nice taste, Right? The operator will then write down this data, let's say on a clipboard. However, and here's the catch. I don't have to provide all the details of the car if I don't want to. This is called a default constructor in Java and is only present if the operator doesn't require any details about the car, or parameters.


The final part of a class is the methods. The main methods that every class shares are accessors and modifiers. Essentially, the operator forgets the type of car. Now, he wants to recall this information from the clipboard: accessor. Or, let's say he wants to change the color to something else. What? Blue and red were good. IDK, maybe he has bad taste? The operator will change the data on the clipboard: modifier.


And that's it! That's all a class is!

Now let's put it together.


Format:

accessLevel class name

{

//insert attributes

public className(parameters)

{

//this is a constructor; usually you set the attribute to the parameter values

}

//add modifiers and accessors

//you can also add customizable methods

}


Examples:


Note: Good programming practice is to make classes start with uppercase letters


public class Car

{

private String model;

private String make;

private String color;

private String seatColor;


public Car(String model, String make, String color, String seatColor)

{

this.model = model;

this.make = make;

this.color = color;

this.seatColor = seatColor;

}


public void setColor(String color)

{

this.color = color;

}


public String getColor() {

return this.color;

}


public String toString()

{

return "make: "+this.make+" model:"+this.model;

}


public String color()

{

return "color: "+this.color+" seat color:"+this.seatColor;

}


}


Classes are really powerful... no, I don't mean Zoom. But, Java classes are really important in making the language more versatile.


I hope it helps and happy coding/cramming! Until next time.

42 views0 comments

Recent Posts

See All

Strings

留言


Post: Blog2_Post
bottom of page