Send
Close Add comments:
(status displays here)
Got it! This site uses cookies. You consent to this by clicking on "Got it!" or by continuing to use this website.nbsp; Note: This appears on each machine/browser from which this site is accessed.
Classes, objects, inheritance, and polymorphism
1. Classes, objects, inheritance, and polymorphism
2. Object
In Java, the root base class
Object, or
java.lang.Object, can store any object but if not handled properly will case a
ClassCastException to be raised.

3. Liskov Substitution Principle
An object is an instance of a class.
The Liskov Substitution Principle (LSP, from Barbara Liskov, 1987, refined in 1994) can be stated (more simply) as follows.
Anywhere in a program an instance of a superclass may be used, an instance of a subclass may be used.
What does this mean? Let us look at it graphically rather than textually
.
Note: Rather than using a tree diagram, the classes will be compared side by side, much as one might compare photographs of generations of the same family for resemblance.
4. Class A
Anywhere in a program an instance of a superclass may be used, an instance of a subclass may be used.
For class A (variables/fields are omitted) there are two methods,
method1 and
method2. With one class, there are no overrides and no need/way to invoke
super.
5. Class B extends class A
Anywhere in a program an instance of a superclass may be used, an instance of a subclass may be used.
To share the similarities between class A and class B, class B overrides
method2 from class A, but can still invoke it via
super. Class B appears as if it were of class A.
6. Class C extends class B
Anywhere in a program an instance of a superclass may be used, an instance of a subclass may be used.
To share similarities between class A, B, and C, Class C adds
method4 and overrides
method2 from class B (but can still invoke it via
super). Class C appears as if it were of class B and also appears as if it were of class A.
7. End of page