decoration decoration
Stories

GROKLAW
When you want to know more...
decoration
For layout only
Home
Archives
Site Map
Search
About Groklaw
Awards
Legal Research
Timelines
ApplevSamsung
ApplevSamsung p.2
ArchiveExplorer
Autozone
Bilski
Cases
Cast: Lawyers
Comes v. MS
Contracts/Documents
Courts
DRM
Gordon v MS
GPL
Grokdoc
HTML How To
IPI v RH
IV v. Google
Legal Docs
Lodsys
MS Litigations
MSvB&N
News Picks
Novell v. MS
Novell-MS Deal
ODF/OOXML
OOXML Appeals
OraclevGoogle
Patents
ProjectMonterey
Psystar
Quote Database
Red Hat v SCO
Salus Book
SCEA v Hotz
SCO Appeals
SCO Bankruptcy
SCO Financials
SCO Overview
SCO v IBM
SCO v Novell
SCO:Soup2Nuts
SCOsource
Sean Daly
Software Patents
Switch to Linux
Transcripts
Unix Books

Gear

Groklaw Gear

Click here to send an email to the editor of this weblog.


You won't find me on Facebook


Donate

Donate Paypal


No Legal Advice

The information on Groklaw is not intended to constitute legal advice. While Mark is a lawyer and he has asked other lawyers and law students to contribute articles, all of these articles are offered to help educate, not to provide specific legal advice. They are not your lawyers.

Here's Groklaw's comments policy.


What's New

STORIES
No new stories

COMMENTS last 48 hrs
No new comments


Sponsors

Hosting:
hosted by ibiblio

On servers donated to ibiblio by AMD.

Webmaster
I agree Would this help? | 314 comments | Create New Account
Comments belong to whoever posts them. Please notify us of inappropriate comments.
A Cheap OO Primer
Authored by: greed on Saturday, May 05 2012 @ 11:26 AM EDT

Object Oriented Programming, at its root, is all about combining the data structures of a program with the code to work on those data structures.

This encapsulation of code and data allows programmers to concentrate on the flow and relationships of data in a program. And programs are all about data: without data, there is nothing for the program to do. Without data, the most efficient sorting algorithm is no faster than the worst.

To get an object to do something with its data, we have to invoke a subroutine that is part of the object. To make this convenient--and less prone to keyboard error--Object Oriented languages handle determining the correct subroutine for you. And the resulting action is known by several phrases, such as "invoking an instance method" or "sending a message to an object".

So, for example, in Objective C you could make yourself a Dog class:

@interface Dog : NSObject
- (void)Bark;
@end

What that means is, any new Dog object can be sent a Bark message. (There's no data involved--no arguments, no instance variables--so presumably Bark is a method that has side effects.)

Similarly, in Java, we can do the same thing, just with different syntax:

public class Dog {
  void Bark() { }
}

There's two differences in Java: first, there's no obvious ancestor class--the compiler will automatically make our class depend on java.lang.Object. Second, this is actually the implementation, and our Bark() method is far from finished--it doesn't actually do anything. (You can't tell in the Objective C code earlier, because I left out the implementation.)

So, we haven't actually done any serious object modelling, and we already have inheritance. Java, Objective C and SmallTalk all require all classes inherit from a single ancestor class, java.lang.Object, NSObject and Object respectively.

(I couldn't find an explicit claim for that for SmallTalk, but you create new classes by sending a message to an existing class--starting from Object. By induction, therefore, tree hierarchy (single root), not a forest (C++).)

This makes it very easy to support collections: all the collection has to recognize is the root object, and it will still be able to send messages to every element in a collection. In Objective C:

NSArray *dogs=[NSArray arrayWithObjects:myDog,yourDog,hisDog,nil];
[dogs makeObjectsPerformSelector:@selector(Bark:)];

The Array class doesn't need to know what it's holding, and yet you can ask it to make all the dogs bark. (An Objective C "selector" is just the name of a method, in a canonical form for the computer to use. By having special syntax for it, the compiler can typo-proof it before you run it.)

But what if you get a cat? There are things cats and dogs have in common--they're mammals, and we keep some as pets.

That first one: they are--we call that an "is-a" relationship. Anything something "is" will typically be a candidate for being made its own class. A car is a vehicle; so is a bicycle. A house is a building, so is a stadium.

public class FurryMammal {
  Colour FurColour();
}
public class Dog extends FurryMammal {
  void Bark() { ... }
  boolean Fetch(java.lang.Object what) { ... }
}
public class Cat extends FurryMammal {
  void Meow() { ... }
  boolean ShredCurtains() { ... }
}

So, Cat and Dog are both FurryMammals. A Dog can Bark, and you can ask it to Fetch an object. (It will say if it did or not.) Cat, on the other hand, can Meow and ShredCurtains.

Because of inheritance from FurryMammal, both Cat and Dog will answer the FurColour message--you don't have to write additional code for that, you inherited that method (and any data) from your superclass.

(These examples are to illustrate the concepts only, they are not an example of good practice in either language.)

That's the most important two: inheritance and encapsulation. You can find both of them in Simula 67, the accepted ultimate root class of Object Oriented Programming.

Some will say "hiding" and "protection" (private, protected keywords) are important. But that actually requires a huge conversation about trust: Perl, for example, went with "we're all just trying to code here", and has no object member security features--just an agreement that if you do something dumb, you pay the consequences. (Some might say your consequence is having to maintain Perl code.)

Others will mention "polymorphism": the ability to have different subroutines with the same name that take different argument types. It does tend to flow from the object design, but I don't consider it fundamental.

The C++ people will probably go on about "templates", because they help solve a problem central to their language environment.

Getting everyone to agree on just what an Object Oriented language is... a lot like trying to send a herd: message to an array of Cats.

[ Reply to This | Parent | # ]

I agree Would this help?
Authored by: Anonymous on Saturday, May 05 2012 @ 12:32 PM EDT
Would this help.

A Brief History of Object-Oriented Programming
http://web.eecs.utk.edu/~huangj/CS302S04/notes/oo-intro.html

[ Reply to This | Parent | # ]

better still "design by contract"
Authored by: Anonymous on Saturday, May 05 2012 @ 01:57 PM EDT
Please read this:

http://en.wikipedia.org/wiki/Design_by_contract

--David H.

[ Reply to This | Parent | # ]

I agree
Authored by: Anonymous on Saturday, May 05 2012 @ 02:35 PM EDT
FWIW, Alan Kay (one of the creators of the Smalltalk language) invented the term
"Object Oriented".

Smalltalk is a fully object-oriented language, meaning that every value (even
simple things like numbers and boolean values) is an object. You can "send
any message" to an object (this is like calling a function on it, but
dynamic: if the object supports that message name, then the method with the same
name is invoked. If it doesn't support the message name, a method called
#doesNotUnderstand: is invoked instead.)

[ Reply to This | Parent | # ]

I agree
Authored by: Anonymous on Saturday, May 05 2012 @ 07:40 PM EDT
Maybe a simpler way to address the issue would be to compare Java with
an older but more recent language like C++. Put code expressed in Java
next to the same code written in C++ using the same package/namespace
and class names and even a layman will see how little of object oriented
programming was created by Java. It will also reinforce the evolutionary
nature of the software world and how a bad decision on APIs would do to
the software industry what an asteroid did to the dinosaurs.

[ Reply to This | Parent | # ]

What we need most is a comparison of Java APIs and various OOP language APIs
Authored by: celtic_hackr on Sunday, May 06 2012 @ 02:23 PM EDT
For example There is C++ Standard Library API. How old is that and what was in
it prior to 1995?

Does any of the core 37 Java APIs use the same SSO as the C++ Standard Library?
Or Smalltalk's?

Smalltalk is very different syntactically, IIR, than either Java or C++.

I don't know what I have here at hand to help. I'll delve into it and see if I
can find something. Anything. Since PJ is asking.

But I'll end this post with some simple C++ and Java syntax.


C++
class BooHoo { // Declaration for class BooHoo
public:
int tears; // a variable

BooHoo(): tears(0) {}
// class constructor for Boo Hoo; Every class needs one ;initializes
// tears to 0. Without this the value of the
// variable would not consistently set

int cry(int i) { // class member function cry()
return 86*i + tears;
}
};



Java
class BooHoo { // This is a comment just like C/C++
public int tears;
// a variable, just like C/C++, slightly different
// implementation of the C++ qualifier "public" and Java by
// default sets variables to a consistent state (here it will be 0).

BooHoo() {
} // class constructor for Boo Hoo; just like C++

int cry(int i) { // a class member method just like C++
return 86*i + tears;
}
}

[ Reply to This | Parent | # ]

Groklaw © Copyright 2003-2013 Pamela Jones.
All trademarks and copyrights on this page are owned by their respective owners.
Comments are owned by the individual posters.

PJ's articles are licensed under a Creative Commons License. ( Details )