Classes, Objects, Methods, and Constructors

As a fan of the TheNewBoston.org website and YouTube channel, I am also a member of the TheNewBoston forums, where there are a bunch of knowledgeable and helpful people when it comes to computers, programming, and technology.

Recently, one of the users of the website posted in the Java section of the forums, asking what the difference was between classes, objects, methods, and constructor methods in Java, which I thought was a pretty good question, and figured I’d write a blog post about it to explain it to those who may not be a member of the TheNewBoston forums.

Since Java is an object-oriented programming language, many programs and applications written in the language are made up of multiple classes, which contain attributes, methods, constructors, etc., all of which work together to make the program run the way the programmer intended.

In a procedural language, like C, Pascal, Fortran, or BASIC, classes don’t really exist, so the idea of objects and object-oriented programming doesn’t apply (though there are some things that are similar to object-oriented programming languages with some procedural languages),  Java and other OOP languages rely on the concept of classes and objects, so it’s important to understand what these things are and how they work.

Classes

In object-oriented languages, like Java and C++, classes are essentially sections of code that are combined into a project to create a program.

Classes are useful, as they help to break up the various sections or pieces of code that may be a part of a larger program, and can be helpful when organizing and diagramming how a program or application should work.

Classes can contain attributes, such as variables and constants, as well methods (similar to functions in other languages, like C, JavaScript, PHP, etc.).

Variables and constants usually hold values (and constants are usually “permanent” values, meaning they can not be changed in the program), which can be used elsewhere in a class, or referenced from another class, via an object of a particular class.

Methods:

Methods, which are contained in classes, are used to perform operations on data, such as mathematical operations like addition, subtraction, multiplication, and division, obtain data from the user or from elsewhere in a program or file, parse data entered by a user, program, or file, and more.

Many classes available in the various Java libraries, such as the Math class, contain a bunch of methods (and attributes, like pi and e), that can be used to perform a variety of different operations or functions.

In the case of the Math class, there are methods, like sqrt()pow()abs(), and a variety of other methods, many of which also take parameters, which are values that are passed into a method, by putting attributes in the parenthesis of a method.

Objects:

Objects are basically different instances of a class, and allow you to access and use the public methods and attributes of the class that has been instantiated, or created, as an object.

For example, in the Scanner class (a class in the java.util.* package), you can use, or “call”, the various methods that are contained within the class, such as the nextInt()nextDouble()nextLine(), and other methods, provided that you have instantiated an object of the class.

When instantiating an object of a class, you usually use the following syntax:

ClassName objectName = new ClassName();

(Note: In the above line of code, the “ClassName()” segment, after the “new” operator, is actually a call to the class’s constructor method, which will be discussed later in this post.)

If you would like to call a method of a class, you can do so by placing the method name after the object name, using the “dot” operator, like in the example below:

objectName.methodName();

Many times, methods will return values, which can be stored in a variable, such as when finding the square root of a number, getting user or file input, etc., which could look something like:

double myDouble = Math.sqrt(27);

In this case, the value of the variable myDouble would be 3, since the square root of 27 is 3.

Constructor Methods:

Since Java is an object-oriented programming language, there are times when an object of a class requires a value upon instantiation, or creation, of an object of that class.

This is where constructor methods come in handy, as they enable objects to be created with initial values, either entered by a user or taken from elsewhere in the program, such as from another class, a return value, an attribute, etc.

Every class in Java contains a constructor method by default, even if one is not explicitly written in the class, regardless of whether or not the class requires one or not. Because of this, the default constructor will execute when an object of the class is instantiated, but won’t change any data or affect how the class or object works.

However, if a class requires a constructor method, a programmer can write one, therefore overriding the class’s default constructor method.

Whenever an object of a class is instantiated, its constructor method can be used to initialize or change the various values and attributes present within the class, which can be useful if a class requires user entered data or information from somewhere else in the program.

Below is a link to an example class that I wrote, called CircleClass, where you can see various parts of a class being used, including attributes, methods, a constructor, etc.

http://pastebin.com/9F32VL0q

Also, here is a link to another class that I wrote, called MainClass, which shows how an object is instantiated and how methods of an object are called, including how the constructor is used to initialize class attributes.

http://pastebin.com/9qwmcnTQ

After I wrote and compiled the code for these two classes, I was able to run the program successfully, and got the correct results (I checked using Google’s calculator functionality), which are below.

Area: 78.53975
Circumference: 31.4159

If you’d like to see my reply to the thread/post referenced in this blog post, you can check it out here, at the TheNewBoston forums.

For updates on future posts or if you’re interested in what I’m up to on a day to day basis, please feel free to follow me on Twitter (@Jamiemcg)! You can also follow TechnicalCafe’s Twitter (@TechnicalCafe) for news and updates regarding the website, YouTube channel, and more!

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s