Introduction
This is the forty-first installment in a series of articles about fundamental object-oriented (O-O) concepts and related object-oriented technologies. The material presented in these articles is based on material from the second edition of my book, The Object-Oriented Thought Process, 2nd edition. The Object-Oriented Thought Process is intended for anyone who needs to understand basic object-oriented concepts and technologies before jumping directly into the code. Click here to start at the beginning of the series.
In keeping with the code examples used in the previous articles, Java will be the language used to implement the concepts in code. One of the reasons that I like to use Java is because you can download the Java compiler for personal use at the Sun Microsystems web site http://java.sun.com/. You can download the standard edition, Java SE JDK 6, at http://java.sun.com/javase/downloads/?intcmp=1281 to compile and execute these applications. I often reference the Java J2SE 5.0 API documentation and I recommend that you explore the Java API further. Code listings are provided for all examples in this article as well as figures and output (when appropriate). See the first article in this series for detailed descriptions for compiling and running all the code examples (http://www.developer.com/design/article.php/3304881).
For this article, you will compare and create code for three object-oriented languages: Java, C# .NET, and Visual Basic .NET. For information on the tools used to compile the .NET code, please visit the Microsoft web site at http://www.microsoft.com/net/Basics.mspx.
The code examples in this series are meant to be a hands-on experience. There are many code listings and figures of the output produced from these code examples. Please boot up your computer and run these exercises as you read through the text.
In future articles, you will explore specific differences in these languages; in this article, I will provide a framework code set to provide a baseline for moving among various languages, not just the three that you explore here. Although you focus on these three languages in this article, it is helpful to first explore what it means to be an object-oriented language.
Structured and Object-Oriented Development
One historical fact that surprised me when I started programming with object-oriented languages was the fact that object-oriented programming history actually parallels that of structured programming. I had originally assumed that object-oriented design was a totally separate design methodology. Even today, I encounter many people who assume that object-oriented development and structured development are two totally distinct paradigms—an either-or proposition.
The truth of the matter is that both development paradigms are alive and well, and that the object-oriented paradigm in fact incorporates the major structured programming concepts within its design.
I contend that in any introductory programming class, you must teach the following fundamental software design concepts:
- Variables
- Data Structures
- If Statements
- Loops
- Classes/Objects
- Encapsulation
- Composition
- Inheritance
- Polymorphism
These first four bullet items are inherent to structured programming. The fact that they also are fundamental to object-oriented development indicates that the object-oriented discipline did some significant borrowing, or at least co-development. Thus, to learn how to program with objects, you also must learn the primary structured concepts.
Although most developers can identify many structured programming languages—such as Fortran, COBOL, Basic, C, and so forth—not many can trace the development of object-oriented languages. Take a quick look at the evolution of object-oriented languages, many of which you will see in this and future articles.
Object-Oriented Languages
What really makes a programming language object oriented? And, when did the object-oriented movement really start? Many people point to the early 1960s as the time when Simula-67 introduced the concept of an object.
Simula
As the name implies, Simula was created to aid in simulations. It is not a coincidence that simulations typically model real-world systems. Many of these real-world systems contained hundreds, or thousands, of interacting parts.
The initial version of the language, Simula-1, was introduced in 1966. The programming modules defined by Simula were based not on procedures, but on actual physical objects. Simula had a novel way of presenting the object, so that each object has its own behavior and data.
Smalltalk
Many consider that the first truly O-O language was Smalltalk, developed at the Learning Research Group at Xerox’s Palo Alto Research Center in the early 1970s. In Smalltalk, everything is really an object that enforces the O-O paradigm. It is virtually impossible to write a program in Smalltalk that is not O-O. This is not the case for other languages that support objects, such as C++ and Visual Basic (and Java, for that matter).
C++
C++ has its roots in a project to simulate software running on a distributed system. This simulator, actually written in Simula, is where Bjarne Stroustrup conceived of the idea of combining some of the features of Simula with the syntax of C.
While working at Bell, Stroustrup made personal contacts with people such as Brian Kernighan and Dennis Ritchie, who wrote the definitive book on C. When the initial simulator written in Simula failed, Stroustrup decided to rewrite it in a C predecessor called BCPL.
C++ was originally implemented in 1982 under the name C with Classes. As the name suggests, the most important concept of C with Classes was the addition of the class. The class concept provided the encapsulation now requisite with O-O languages.
Java
Java’s origins are in consumer electronics. In 1991, Sun Microsystems began to investigate how it might exploit this growing market. Some time later, James Gosling was investigating the possibility of creating a hardware-independent software platform for just this purpose. Initially, he attempted to use C++, but soon abandoned C++ and began the creation of a new language he dubbed Oak.
By fall 1995, Java beta 1 was released, and the Netscape Navigator 2.0 browser incorporated Java. Java 1.0 was officially released in January 1996. Over the past several years, Java has progressed to the current release Java 2 Platform Standard Edition 6.0 as well as other platforms such as an Enterprise Edition (J2EE) for the enterprise/server market and a Micro Edition (J2ME) for mobile and wireless.
.NET
Microsoft initially responded to the popularity of Java by producing a version of Java called Visual J++. However, Microsoft decided on a more comprehensive response. Using many of the groundbreaking concepts that Java implemented, Microsoft developed a language called C# that became the foundation for the company’s .NET platform. As with Java, C# relied heavily on the success and failures of earlier languages.
The .NET development environment includes many of the really good characteristics of several other platforms. .NET incorporates many of concepts introduced by the initial Java release. The .NET platform also builds upon many of the powerful features of the VB6 and Visual C++ environments.
Visual Basic 6 was one of the most popular programming languages. The programming environment for VB6 has had a huge impact on state-of-the-art development environments. VB6 has evolved steadily towards the object-oriented model until it finally joined the list of object-oriented languages with the release of Visual Basic .NET. VB6 was not totally object-oriented; it did not implement inheritance completely.
Comparing the Code
For the remainder of the article, I will (mostly) let the code speak for itself. In future articles, you will explore specific programming structures of various programming languages; however; in the context of this article, my intent is for you to take the following examples and develop a baseline for each of the three languages: Java, C#, and VB .NET.
The three example applications are named as follows:
- IntSquare: A single class to calculate the square of a number.
- Person: A single class to set and get the name and address of a person.
- Shape: A hierarchy of classes that calculates the area of various shapes.
These examples are the first step in creating projects in the various programming languages presented.
At this point, you can inspect the code just to get a feel for the syntax of the various languages. This has been very helpful in my programming classes. Often, classes will focus on a single instructional language to keep the development tool overhead, not to mention the language learning curve, to a minimum. Although you may focus on a single language to reinforce the object-oriented constructs, it really helps to see examples in other languages to augment and reinforce the primary language used in the class.
After you have inspected the code, the next step is to load the various development platforms so that you can compile and execute the code. The development platforms are a topic that you will explore at length in the next article.
For each of these examples, I provide brief comments as to the instructional highlights of the code. In each case, the examples are identical—at least in functionality. The point is to solve the same problem with each of the languages.
The three distinct examples are presented in three sections, IntSquare (Listing 1), Person (Listing 2), and Shape (Listing 3). Within each section, I break the code into three sub-listings, for Java (Listing a), C# .NET (Listing b), and Visual Basic .NET (Listing c).
IntSquare
In the IntSquare example, the focus, as the initial example, is on the syntax required to compile and execute a simple program in the three languages. The point here is simple enough; create a method to calculate the square of a number.
The class has a single attribute and two methods (a public interface and private implementation).
public class IntSquare { // private attribute private int squareValue; // public interface public int getSquare (int value) { squareValue =calculateSquare(value); return squareValue; } // private implementation private int calculateSquare (int value) { return value*value; } }
Listing 1a: Java Code for IntSquare Class
public class IntSquare { // private attribute private int squareValue; // public interface public int getSquare (int value) { squareValue =calculateSquare(value); return squareValue; } // private implementation private int calculateSquare (int value) { return value*value; } }
Listing 1b: C# Code for IntSquare Class
Public Class IntSquare Dim squareValue As Integer Property accessSquareValue() As Integer Get Return squareValue End Get Set(ByVal Value As Integer) squareValue = Value End Set End Property Public Function calculateSquare(ByVal Value As Integer) _ As Integer System.Console.WriteLine(Value) squareValue = Value * Value Return squareValue> End Function End Class
Listing 1c: VB Code for IntSquare Class
Person
The Person example creates a simple class that includes the code to illustrate the object-oriented concept of encapsulation. You create attributes and the various accessor/mutator methods (getters and setters).
public class Person{ //Attributes private String name; private String address; //Methods public String getName(){ return name; } public void setName(String n){ name = n; } public String getAddress(){ return address; } public void setAddress(String adr){ address = adr; } }
Listing 2a: Java Code for Person Class
public class Person{ //Attributes private string name; private string address; //Methods public string getName(){ return name; } public void setName(string n){ name = n; } public string getAddress(){ return address; } public void setAddress(string adr){ address = adr; } }
Listing 2b: C# Code for Person Class
Public Class Person Dim name As String Dim address As String Property accessName() As String Get Return name End Get Set(ByVal Value As String) name = Value End Set End Property Property accessAddress() As String Get Return address End Get Set(ByVal Value As String) address = Value End Set End Property End Class
Listing 2c: VB Code for Person Class
Shape
The Shape example creates a more complicated class structure that includes code to illustrate the object-oriented concept of inheritance. You create four separate classes: Shape, Rectangle, Circle, and TestShape. The primary purpose of this example is to illustrate how the classes are used together in the context of inheritance and how to create a primary application.
//Shape public abstract class Shape{ protected double area; public abstract double getArea(); } //Rectangle public class Rectangle extends Shape{ private double length; private double width; public Rectangle(double l, double w){ length = l; width = w; } public double getArea() { area = length*width; return (area); } } //Circle public class Circle extends Shape{ private double radius; public Circle(double r) { radius = r; } public double getArea() { area = 3.14*(radius*radius); return (area); } } //TestShape public class testShape { public static void main(String args[]) { Circle circle = new Circle(5); Rectangle rectangle = new Rectangle(4,5); } }
Listing 3a: Java Code for Shape
//Shape public abstract class Shape{ protected double area; public abstract double getArea(); } //Rectangle public class Rectangle : Shape{ private double length; private double width; public Rectangle(double l, double w){ length = l; width = w; } public override double getArea() { area = length*width; return (area); } } //Circle public class Circle : Shape { private double radius; public Circle(double r) { radius = r; } public override double getArea() { area = 3.14*(radius*radius); return (area); } } //TestShape public class TestShape { public static void Main() { Circle circle = new Circle(5); Rectangle rectangle = new Rectangle(4,5); } }
Listing 3b: C# Code for Shape
Public MustInherit Class Shape Protected area As Double Public MustOverride Function getArea() As Double End Class Public Class Rectangle Inherits Shape Dim length As Double Dim width As Double Sub New(ByVal l As Double, ByVal w As Double) length = l width = w End Sub Public Overrides Function getArea() As Double area = length * width Return area End Function End Class Public Class Circle Inherits Shape Dim radius As Double Sub New(ByVal r As Double) radius = r End Sub Public Overrides Function getArea() As Double area = 3.14 * (radius * radius) Return area End Function End Class Module Module1 Sub Main() Dim myCircle As New Circle(2.2) Dim myRectangle As New Rectangle(2.2, 3.3) Dim result As Double result = myCircle.getArea() System.Console.Write("Circle area = ") System.Console.WriteLine(result) result = myRectangle.getArea() System.Console.Write("Rectangle area = ") System.Console.WriteLine(result) System.Console.Read() End Sub End Module
Listing 3c: VB Code for Shape
Conclusion
In this article, you changed gears a bit. Rather than focus on a specific programming concept or technique, you inspected several basic applications and concentrated on how to develop the application in three different object-oriented languages: Java, C#, and VB .NET.
The intent was to whet your appetite. In future articles, you will delve deeper and compare specific programming features and see how the various languages implement these features. You also will compare the similarities and the differences of the various languages. Exploring different languages can help you better understand object-oriented concepts.
References
Weisfeld, M.A. (2005). “The Evolution of Object-Oriented Languages,” Developer.com.
About the Author
Matt Weisfeld is a faculty member at Cuyahoga Community College (Tri-C) in Cleveland, Ohio. Matt is a member of the Information Technology department, teaching programming languages such as C++, Java, C#, and .NET, as well as various web technologies. Prior to joining Tri-C, Matt spent 20 years in the information technology industry, gaining experience in software development, project management, business development, corporate training, and part-time teaching. Matt holds an MS in computer science and an MBA in project management. Besides The Object-Oriented Thought Process, which is now in its second edition, Matt has published two other computer books, and more than a dozen articles in magazines and journals such as Dr. Dobb’s Journal, The C/C++ Users Journal, Software Development Magazine, Java Report, and the international journal Project Management. Matt has presented at conferences throughout the United States and Canada.