I have 98 Java files prepared over the years for this class .. and most or them
were compiled using an older, less secure Java compiler. So your browser may
display a security warning. I think the code is safe for your browser to download and
run. You can, therefore, tell it to access them. You can probably tell it to accept any
applet from my web site quite safely.
The problem may be that I have no easy and simple way of signing and applet with a
certification from a trusted authority.
Objects compute things by sending messages. Every message is like a function
call and goes from one object to another. As a result you can draw a diagram
of an OO program that shows the sequence of calls to and from some objects.
Here is an example of part of a program that simulates a DiceGame that has
two Dice and throws them, and reads their values to work out a score:
The code is in
[ java/DiceGame.java ]
and
[ java/Die.java ]
, enjoy!
- All objects are constructed as an instance of a concrete class.
- An animal is an abstraction... but each cat and dog
is a concrete example of an animal.
You point at a cat and a dog with the same finger... and
always be pointing at an animal.
- You can have variables that are declared to point at abstraction.....
- but can not construct abstract objects.
- You can have variables that are declared to point at abstraction.....
- and point them at concrete objects derived from the abstraction.
Try out this program:
[ 15.cpp ]
- Cats and dogs behave the same even if we change their names.
- Call a cat an animal.... and it still behaves like a cat.
- In a non-object-oriented language the behavior depends on its
name.
- In an object-oriented language the behavior is determined by the
object.
- Base class Person and two extended classes Student and
Faculty.
- Each class defines an output function that displays the name of
the Person.
- Students also display their GPA.
- Faculty display their rank.
Person * pointer;
Faculty dick;
pointer = & dick;
pointer->output(); // name and rank
Student jo;
pointer = &jo;
pointer->output(); // name and GPA
pointer = new Student(...);
pointer->output(); // name and GPA
C++ is a hybrid language. Polymorphism is an option.
The default is to cast objects. In other words to change the
type of an object to a more abstract type.
This can result in programs that have bugs.
If an object is assigned to another object of a more general (base) type,
the specific data is stripped away.
It behaves according to the general rules.
This happens with pass-by-value!
So in C++ always
- Use pass by reference and pass by constant reference
- Do not use rather pass by value.
- Declare member functions to be virtual.
- Refer to objects via pointers.
Fully object-oriented languages (Java, SmallTalk, etc.) use late (dynamic)
binding by default and these errors are avoided. The cost is that the program
runs slower.