Chapter 11: Inheritance and Polymorphism Flashcards
What is a superclass?
A superclass is a big generalized class that allows subclasses to inherit its data in their more detailed class. EXAMPLE: GeometricObjects is a super class Rectangle, circle, and triangle are subclasses.
What is the difference between overriding and overloading?
Overriding is when a subclass needs to modify a method inherited from the super class. Overloading is when you use the same method name but have different make ups.
What is polymorphism?
In simple terms, polymorphism means that a variable of a supertype can refer to a subtype object. You can pass an instance of a subclass to a parameter of its superclass.
What is dynamic binding?
The type that declares a variable is called the variable’s declared type. Here, o’s declared type is Object. A variable of a reference type can hold a null value or a reference to an instance of the declared type. The instance may be created using the constructor of the declared type or its subtype. The actual type of the variable is the actual class for the object referenced by the variable at runtime. Here, o’s actual type is GeometricObject, because o references an object created using new GeometricObject(). Which toString() method is invoked by o is determined by o’s actual type. This is known as dynamic binding.
How is the protected modifier used in the relationship between super classes and sub classes?
You can use the protected modifier to allow a subclass to access its superclasses methods, while still keeping the superclass protected from other packages that aren’t its subclass.
PUBLIC: ALL ACCESS
PROTECTED: ONLY SUBCLASSES CAN ACCESS
PRIVATE: NO OTHER PACKAGES CAN ACCESS
When do you cast an object?
When you want to use an object reference an instance of an object that normally wouldn’t work.
To help understand casting, you may also consider the analogy of fruit, apple, and orange, with the Fruit class as the superclass for Apple and Orange. An apple is a fruit, so you can always safely assign an instance of Apple to a variable for Fruit. However, a fruit is not necessarily an apple, so you have to use explicit casting to assign an instance of Fruit to a variable of Apple.