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


Warning: count(): Parameter must be an array or an object that implements Countable in /homepages/40/d88105018/htdocs/cww/blog/wp-content/plugins/wp-codebox/main.php on line 31

Fatal error: Uncaught Error: Call to undefined function eregi() in /homepages/40/d88105018/htdocs/cww/blog/wp-content/plugins/wp-codebox/main.php:140 Stack trace: #0 /homepages/40/d88105018/htdocs/cww/blog/wp-content/plugins/wp-codebox/main.php(76): wp_codebox_is_windowsie() #1 /homepages/40/d88105018/htdocs/cww/blog/wp-content/plugins/wp-codebox/main.php(50): wp_codebox_highlight_geshi(Array) #2 [internal function]: wp_codebox_highlight(Array) #3 /homepages/40/d88105018/htdocs/cww/blog/wp-content/plugins/wp-codebox/main.php(134): preg_replace_callback('/<p>\\s*f137609c...', 'wp_codebox_high...', '<div class="ser...') #4 /homepages/40/d88105018/htdocs/cww/blog/wp-includes/class-wp-hook.php(286): wp_codebox_after_filter('<div class="ser...') #5 /homepages/40/d88105018/htdocs/cww/blog/wp-includes/plugin.php(203): WP_Hook->apply_filters('<div class="ser...', Array) #6 /homepages/40/d88105018/htdocs/cww/blog/wp-includes/post-template.php(240): apply_filters('the_content', 'To me it seems ...') #7 /homepages/40/d88105018/htdoc in /homepages/40/d88105018/htdocs/cww/blog/wp-content/plugins/wp-codebox/main.php on line 140