Introduction to Java : Java Classes part-2

Operator Overloading

Another cool feature of Java is that methods in Java can easily be overloaded such that the same method name can be used for several different implementations of the same action. Remember our previous example of printNumber(), printLetter(), printImage()?

The only requirement for overloading is that each version of the method takes a different set of parameters as arguments (sometimes, we say that it has a “different signature”) such as in the following example:

Finally, it is worth noting that since methods belong to specific classes, it is fine for you to have the same method name with the same arguments in different classes. Thus, the Button class might have a setLabel(String s) method and a Label might have a setLabel(String s) method and neither will conflict since each method is specific to a specific class.

Inheritance

Inheritance in Java is very simple and is handled by the “extends” keyword. As you might expect, subclasses work just like their superclass (inherit all of the functionality) except that they typically add extra funtionality.

If a class does not specifically extend anything than it will be automaticaly extended from the root Object class

Thus, in the following example, we create a subclass of Applet caled myApplet:

Further, extension is transitive. That is, if you extend from a class which is an extension of another, then you gain the functionality of your superclass as well as its superclass.

It is also worth noting that not only can you add to the functionality of a superclass, but you can modify existing functionality as well. This is achieved through overriding a method in one’s superclass. To override a superclass’ method, you would implement your own method with the same signature as your superclass’ method. Thus, the Java Virtual machine would use your implementation rather than your superclass’. Of course, you can explicitly use your superclass’ methods and fields using the “super” keyword such as:

The super keyword is particularly important when working with Constructors because subclasses do not inherit the constructors of their superclasses. Thus, in order to utilize the constructor of your superclass, you must explicitly call it using the super keyword such as in the following example:

Java also provides the “final” keyword which prevent a class from being subclassed, a method from being overridden and a variable from changing its initialized value. However, it is rarely used for regular application code.

Creating and Using Classes by Example

So what does a class look like? Well, let’s make one. However, since we have not yet gone into the depths of Java, our class will be very small. We will create an Announcer class that is solely responsible for announcing some phrase. Consider the following code (we will explain it in just a minute so don’t get too caught up on the syntax or keywords):

Okay, so believe it or not, in 23 lines of code we have demonstrated the creation of an object, the creation of a public API, encapsulation, and polymorphism (we’ll show an example of inheritance later). See how easy Java makes object-oriented programming!

Okay, let’s take this code apart and see how we achieved out object-oriented goal.


Access Specifiers

So the first thing you see in this class is the word “public”. A couple of lines down, you also see the word “private”. What do these keywords mean?

Well, the words private, public, and protected (which we did not use in the example above), are used to define scope.

The scope of a class, method or property defines who is allowed access to it. A “public” scope for example, means that anyone is allowed access. A “private” scope on the other hand means that only the object itself has access. A “protected” scope means that only objects derived from the class can have access. Finally, if you do not define a scope at all (usually called “friendly”), then all classes in the same package will have access.

Thus, in our example above, any object in the object space has access to the Announcer class and can use it in order to create Announcer objects. Everyone also has access to use the printAnnouncement() method in order to tell the Announcer object to print out the message. However, only an Announcer object itself has access to the actual content of the announcement that is stored in the _announcement property. Of course, outside objects can request that the announcement be changed using the get and set API provided by the Announcer class as well.

The benefit of scope or course, is that it “enforces” encapsulation. Programmers are strongly pushed to hide their objets’ data and provide an API such as setAnnouncement().

Essentially, the “private” keywords constructs the walls of the black box that are so crucial for object oriented design.