You are on page 1of 7

Programming NC IV

1
Constructor and Classes

Constructors and Classes

In this module, we will be covering constructors, overloading, nesting and


abstraction of objects. These topics will be crucial in data structuring and
organization.
Upon completing this module, students should be able to:
1. know what constructors are and learn how to use them;
2. create and use default or overloaded constructors;
3. differentiate inner-outer classes from nested ones; and
4. learn the fundamentals of abstract methods and classes.

Constructors
If you ever wonder what constructors and classes are used for, then think
about it this way – social media, and/or other sites that require an account,
use constructors and classes to process and serialize (encrypt) your data
before it goes to a data base.
Oh, and some more for you game savvy kids, these are used for item
generation, character classes and saves in your games to name a few.

Constructors
A Constructor in Java is a special method used to initialize objects. Upon
creation of an object, a constructor is invoked. It provides an object
with its default values constructing it, thus the name constructor.
Constructors have two rules you must follow as of now, but it’s not
compulsory if you’re a master magician. . . I mean an exceptional
programmer.
These rules are:
1. A constructor should have the same name as your class; and
2. A constructor cannot have an explicit return (return itself) type.

Syntax for default constructors:


<class_name>( ){ }

Syntax for parameterized/overloaded constructors:


<class_name>(param){ }

Course Module
Or
<class_name>( param, param . . . ){ }

Constructors can be overloaded with countless parameters as long as you


can keep track of them. The next lines of code are an implementation of how
classes are written with parameters.
But don’t get too attached to these examples as they are just examples and
there are is a myriad of things (methods/expressions) that you can write in
and around classes to make them behave the way you want them to.

public static class myClass(String s){


//do something with s
}
public static class yourClass(String s, int i, float f){
//do something with s, i and/or f
}

Overloading Constructors
What is overloading and why do you need to overload? First of all, this is not
the type of overloading that causes accidents, e.g. an overloaded jeepney that
crashed into a light post.
Not that kind of stuff, this is the overloading that you need to get things done.

Figure 1. Method Overload

Figure 1 is an example of a method overload. The several valueOf methods


with different overloads are the most common examples of method
overloading.
Programming NC IV
3
Constructor and Classes

So what does that have to do with constructor overloading? It’s no


different from a method overload; an overloaded method is a method which
takes different data types or parameters.
It works almost the same way, the only difference is that we are now dealing
with classes so we now follow class syntax and implementation.

Figure 2. Class Overload

Here in Figure 2, we have an example of an overloaded class, named


OverloadConstruct, which have a bunch of overloads that does different
stuff.
The first one is default; the next takes a string parameter and so on. The
thing is that, you have a single class that has multiple overloads just like in
method overloading.
The example is a bit shallow but you’ll never know when you will need a
weird overload or something like that when coding.
This is only the basic implementation so you will have to dig deeper to get
the hang of coding this bit.

Course Module
Classes
Nested Classes
Remember if-else and loop statements? Loopception and switchception?
Declaring a loop inside a loop inside a loop – nested loops?
You can also do that with classes. In Java, it is possible to define a class
within another class.
Why should you implement nested classes?
1. Nested classes provide a logical way to group classes that are used in
one place. If you have a class which is only useful to one class, then it is
logical that you embed it inside that class to group them together.
2. Nested classes increase encapsulation. You can hide the nested class
inside the outer class and keep it private.
3. Nested classes make the code more readable and maintainable. It is
easier to read if a particular class is close to where it is used (top level
class).
The syntax in defining nested classes is:
class OuterClass {
...
class NestedClass {
...
}
}

Inner and Outer Classes


Nested classes are placed in an inner class, that’s why the terms are
interchangeable among programmers. If you are to differentiate these, then
there is a deeper explanation, but we really need not bother with
complicated stuff as we are dealing with the basics for now.
The syntax in using inner and outer classes is:
class OuterClass {
...
class InnerClass {
...
}
}
Programming NC IV
5
Constructor and Classes

Figure 3. Nested Class

Looking at Figure 3, we have a class named Item and an inner or nested


class called Weapon which has two methods that invoke system prints
using strings from the outer class.
There can be a lot of inner classes nested in the outer class having their
own methods – let’s assume that there’s another inner class named
“Shield” with its own methods. These are regularly seen in codes that
create multiple sub methods or data structuring.
To invoke the methods inside the inner class (Figure 3, lines 23-26) the outer
class is constructed followed by the inner class referencing the outer
class.
After referencing the outer class, you can now access its methods. If you
have other classes nested besides the Weapon class, then you also need to
reference those.

Course Module
Abstract Classes
This type of class encloses one or more abstract methods. Abstract methods
are methods with no implementation or body that require sub-classes to be
responsible for implementations.

Figure 4. Abstraction

In Figure 4, an abstract class called Food is declared containing regular


methods and one abstract method called Eat.
Both the Prepare and Cook methods have their respective implementations
but the abstract one has none.
Notice the abstract keyword is used in both class and method. This makes
any food that is instantiated use the Eat method.
What this example implies is that you cannot just declare food and use its
methods, you have to eat it; so declaring a food class (Adobo in this case)
extends to the Food abstract and executes Eat to get to the methods.
Why are methods separated? That’s because the first methods declared
within the main abstract class are given for the following food items, both
need to be cooked and/or prepared.
By doing so, you can inherit other implementations of non-abstract methods.
Sure you can go and code them into separate classes that will clutter your
work or slow down the program, abstraction of classes makes it easier for
the application and compiler to go through such code.
Programming NC IV
7
Constructor and Classes

References
Oracle. Java™ Platform Standard Ed. 7. Retrieved from:
https://docs.oracle.com/javase/tutorial/java/

Course Module

You might also like