Unit4-Inheritance

Basic Rules of Inheritance > public class Child extends Parent >> Mammal m = new Cat; >> Cat c = new Mammal; //not legal// Definitions When the parent variable contains a child object, the parent class (variable) determines what method **names** can be used. This means that the parent variable cannot call a method that exists in the child but not in the parent. The object type determines which **version** of the method is used. > > Mammal m = new Cat; > m. haveBabies --> is okay because the method exists in mammal. > m. purr --> is not okay because the method name exists only in cat and mammal can't find it. > Mammal m = new Cat; > Cat c = (Cat) m; > Mammal m = new Cat; > Cat c = (Whale) m; **Explain what an abstract method is:** An abstract method is a method with only a header and no body/implementation. Must have abstract in the header between public and the return type. An abstract method must be defined in the child classes OR the child class must be declared abstract.
 * Instance Variables and methods can only be inherited from an ancestor class by a child class, and  cannot be inherited "up" the hierarchy from a child to a parent or ancestor.
 * Methods from parent classes can be used simply by calling them.
 * Instance variables must be accessed/modified via accessor and mutator methods.
 * Methods from a parent class can be __overridden__ in the child class to make them more specific.
 * Overridden methods must have exact same header (which includes public/private, return type, name, and the parameters)
 * See below for use of super
 * In order to create a child object we use keyword: extends
 * Each class may only extend one other class.
 * The parent class may extend one class and the child class inherits from all classes in the chain of inheritance.
 * An object of the child type IS (has an IS A relationship) also of the parent type, but an object of the parent IS NOT of the child type.
 * A variable of the parent type can contain a value of the child type.
 * A variable of the child type CANNOT contain a value of the parent type.
 * When refering to the parent class within the child class we use "super" not the name of the parent class.
 * In a constructor in the child class, we use super to call the parent class construct.
 * If we call super, it must be the first thing in the constructor.
 * It may need parameters depending on the definition of the parent constructor.
 * To call methods from the parent class, we use super.method.
 * We cannot use super.super.method to call the ancestor method.
 * We use super.method to call the version of the method from the parent class in order to use it.
 * Can allow you to use the version from the parent class as part of the definition of the overridden method
 * Can let you skip over the overridden method and just use the parent version.
 * This usually saves us from rewriting code.
 * **Polymorphism:** from greek "many forms", In java: one of object can of multiple types. We can type cast between types if needed.
 * **Dynamic** or **Late Binding**:
 * **Downcast** - casting an object of the child type that was stored in variable of the parent type, back to its __real__ type.
 * **ClassCastException Error** - when you try to cast an object as a type that it cannot be

An abstract class a class that includes abstract in the header between public and "class". If a class is abstract, you cannot create an object of that class type.
 * Explain what an abstract class is. What are three reasons you might choose to make a class abstract?**
 * 1) If the parent class is so vague that it would never be used as an object type, (such as mediaItem or Animal), or ...
 * 2) If it contains abstract methods. You might choose to make the methods abstract because all the child classes should have them but their definitions will vary.
 * 3) If it implements an interface and doesn't define all its methods. OR if it extends an abstract class and doesn't define the abstract methods


 * List the rules of inheritance for abstract classes:**
 * If the child class of an abstract class must either define the abstract methods of the parent or it must also be declared abstract.
 * Child classes of an abstract class inherit their regular (non-abstract) methods, and the instance variables.
 * Even though they can't be instatiated (make an object), they have constructors. These can be used in the child class constructors as super.
 * You can have a variable of an abstract class type, but not an object of that type. The variable can hold objects that have an IS-A relationship with the abstract class.

An interface is the ultimate abstraction, in other words not a class. Interfaces provide a template of required methods for a class that implements them. They can contain method headers and constant variables ONLY. Objects cannot be created from interfaces. You can have a variable of an interface type that can hold an object of a class type that implements the interface.
 * Explain what an Interface is. What can be included as part of an interface?**

The main interface we've used so far is Comparable. This simple interfaces contains only the method:: int compareTo(Object obj):: CompareTo, when redefining in the class, must have an Object object as its parameter (You can cast the Object back down to the class' type within the method body).
 * What is the main interface we've talked about and what is method does it include (write the entire method header)?**


 * List the rules for using Interfaces:**
 * Method headers defined in an interface MUST be declared and defined in the class that implements them (otherwise the class must be declared abstract)
 * Interfaces are implemented by... public class ClassName //implements InterfaceName1, InterfaceName 2//
 * Interfaces are made by... //public interface InterfaceName//
 * One class can implement multiple interfaces
 * Within the body of the Interface, methods are assumed to be public and thus do not need public/private written in (only need return type, method name, parameters)
 * Method headers in interfaces DO have a semi-colon finishing the line