Intro to Java Objects and Classes

This entry is part 1 of 13 in the series Intro to Java

This lesson will cover some basic concepts in Java related to classes and objects. It was created as a lesson for a class room setting. I have converted it to a post here.

Class vs Object

Definition from page 10, Java A Beginner’s Guide (Herbert Schildt)

A class defines the form of an object. It specifies both the data and the code that will operate on that data. Java uses a class specification to construct objects. Objects are instances of a class. Thus, a class is essentially a set of plans that specify how to build an object.

The code and data that constitute a class are called members of the class. Specifically, the data defined by the class are referred to as member variables or instance variables. The code that operates on that data is referred to as member methods or just methods.

Sun Trails

Index: http://java.sun.com/docs/books/tutorial/index.html

OO Trail: http://java.sun.com/docs/books/tutorial/java/javaOO/index.html
Continue reading

Intro to Java and Static Stuff

This entry is part 2 of 13 in the series Intro to Java

This lesson will cover some basic concepts in Java related to static variables and methods. It was created as a lesson for a class room setting. I have converted it to a post here.

What does static mean?

Lucky for us, static has nothing to do with statics, a class I had to take in Engineering about the forces on objects that are not moving.

What is does have to do with, seems to go against the concepts we talked about in the Classes vs. Objects lesson. If you followed along there, we talked about how the Class was the blueprint, and the Object was a concrete, instantiated, “built” Class. Its variables were its own, and the methods belonged to this Object.

When something is static, it means that it belongs to the class, and not the object. Every object has access to this item, but it is not unique to the object.

Sun Trails

Index: http://java.sun.com/docs/books/tutorial/index.html

Link into OO Trail: http://java.sun.com/docs/books/tutorial/java/javaOO/classvars.html
Continue reading

Intro To Java Classes Revisited

This entry is part 3 of 13 in the series Intro to Java

This lesson will look more at class concepts in Java: Extending, overriding, composition, and inner classes. It was created as a lesson for a class room setting. I have converted it to a post here.

Is a Class just a Class?

I recently heard that the classic contractor response to a question is “Well, it depends.” That might just apply here. There are several different types of classes. There are regular classes, like we have been using, there are inner classes, and there are anonymous inner classes. In all cases though, they behave just the same. They still need to be instantiated, and they still contain variables and methods. What we will look at here is what those different types are, and when you might use them.

To do this, we will once again revisit the Robot. This time we will be adding a head that rotates to the Robot. We will look at placing the code directly into the Robot without using another class, using an inner class to Robot, and using containment to hold another class.

Before we get to that, lets revisit the parent/child relationship that exists when a class is extended and how casting works. Then we will get to the RobotHead.
Sun Trails Index: http://java.sun.com/docs/books/tutorial/index.html
Link into Objects and Classes Trail: (nested classes) http://java.sun.com/docs/books/tutorial/java/javaOO/nested.html

Continue reading

Intro to Java Interfaces

This entry is part 4 of 13 in the series Intro to Java

Interfaces are a very important part of Java. They can also be quite confusing. In this lesson, we want to take a look at Interfaces, what they are, their relationship to classes, and how and why we use them.

Sun Trails

Index: http://java.sun.com/docs/books/tutorial/index.html

Link into Object Oriented Programming Concepts Trail: (interfaces) http://java.sun.com/docs/books/tutorial/java/concepts/interface.html

What is an Interface?

I think of an interface as like a contract. If you want to fulfill a contract, you have certain obligations to fulfill. The same for a Java contract (interface). An interface defines a set of obligations that an implementing class needs to provide. The obligations are nothing more than method signatures. No code, just the signatures. The class that implements the interface provides the code.

The other thing an interface does, is it allows different classes to appear similar, at least in so far as they implement the interface. As you will see, a Bird, a Fish, and a Mammal are not the same, but if they all implement the Animal interface, then we can treat them all like Animals.

Continue reading

Intro to Java Abstract Classes

This entry is part 5 of 13 in the series Intro to Java

Abstract classes are an interesting piece of Java. They can’t be used on their own, and they have a unique place in our code. They are often confused with interfaces, but they provide a great way to prevent code duplication and ensure consistent structures when building an application.

 In this tutorial we want to look at what Abstract classes are and when to use them. We will take a look at the previous Interfaces lesson and see how we could refactor our code from that lesson to use an Abstract class.
Continue reading

Intro to Java Reflection

This entry is part 6 of 13 in the series Intro to Java

Reflection is a very interesting part of Java. It is sometimes considered an advanced topic, but I think it is worth exploring here. There are several practical applications for using reflection, and we will look at a few here.

Sun Trails

Index: http://java.sun.com/docs/books/tutorial/index.html
Link into the Reflections Trail: http://java.sun.com/docs/books/tutorial/reflect/index.html

What is reflection?

Reflection is a little like cheating. It allows you to get access to classes and its methods and variables without accessing them the normal way. We don’t use the new operator to create an instance, and by using reflection we can get access even to private variables and methods. For this reason, we should be careful about using reflection. It is a powerful way of writing programs, but be sure not to overuse it.
Continue reading

Intro to Java Collections

This entry is part 7 of 13 in the series Intro to Java

I don’t want to get too deep into every little thing in the collections classes here. In fact, you already know a fair bit about them, because we have used them with some of our Animal class examples. This is more an overview of some of the basic Collections classes.

What do they look like?

There are three main types of collections: Lists, Maps, and Sets. Technically you could try to argue that Map doesn’t extend Collection, so therefore it is not a Collection, but I think of it as part of the group.
They are used for groups of objects, or exactly as its name would imply, collections of objects.

List

A List is just that, a list of objects. The List interface provides a way to add, remove and get objects from a list. It however is just an interface. ArrayList is one of the more popular concrete collections that implements the List interface. Each implementation of the List interface may behave slightly different, but once you understand how a List works, you have a good start on understanding any List.
Continue reading

Intro to Java Generics

This entry is part 8 of 13 in the series Intro to Java

To me it seems a better word would be Specifics. The use of Generics is applying a specific type to a class that can be used in a generic way. One of the most common generic classes is List. A list allows you to put any type of Object in to it. In fact, you can put several different types of Objects into a List. But, there is a way to create a specific List type that can only take one type of Object.

What do they look like?

Since we are talking about Lists, lets look at what a ArrayList would look like that could only take Strings.

List aList = new ArrayList();

What goes between the <> is the type that you want to specify for the List. You need to do it in the left side declaration, and on the right side where you are creating the Object.
Continue reading

Intro to Java Annotations

This entry is part 9 of 13 in the series Intro to Java

Annotations allow you to attach metadata to a field, class, or method. Metadata is data that describes something else. For example, metadata about a song in mp3 format could be the artists name or the bit rate it was encoded at. A jpeg image could have metadata that described the image height or the number of colors in the image.

What do they look like?

@Override
public String getName() {
	return "no name";
}

The word after the ‘@’ is the Annotation. It precedes a method or class name. The Override annotation is built into Java. It is used to indicate that a method overrides a parents method. These are used for compiler hinds, for documentation, and to apply meta data.
Continue reading

Intro to Java Patterns

This entry is part 10 of 13 in the series Intro to Java

Patterns are a very important part of Java. They allow us to speak a common language. The names can be misused, and thrown around by people trying to sound important, but they really do help us to be able to communicate ideas between us.

It does take some Java knowledge to be able to read, use and apply patterns, but I think we have seen enough Java and OO principles to start looking at patterns. It is good to start hearing about and seeing different patterns at any level of Java programming.

So what is a pattern? In the physical world, it would be a series of steps taken over and over again that could be written down and repeated. In the software world, it is not really a set of steps, but a software solution that people apply over and over again in their coding. Experienced developers have put these solutions together, and given their pattern a name. By looking at and studying patterns, we can take advantage of the experience of others, and learn from their solutions. One interesting thing about patterns, is that they don’t need to be language specific. If you learn about the Adapter or the Command pattern, you can apply that pattern to PHP, Ruby, or Java.
Continue reading