Chapter 10~ Polymorphism
Inheritance: Extending characteristics of a class to other classes, or defining a specialized version of an existing class.
When a class extends another class, it demonstrates an is-a relationship. This is different from a has-a relationship, where a class might use objects of another class.
The class that is extended is often called the base, super or parent class, and the extension the subclass, child, or derived class.
In a subclass, extra methods and members can be defined, and methods of the superclass can even be overridden. However, not everything has to be changed or redefined. If a subclass wants to access methods of the base class, the keyword super can be used.
However, visibility also plays a role. The super keyword cannot access private methods.
Polymorphism is the ability of objects to assume different types.
For example, though you can declare an object of one class, you can also reassign it a new subclass.
Circle wafer;
Disk cookie = new Disk(2, 0.5);
wafer = cookie; //Wafer began as a circle, now a Disk
After reassignment, the object uses methods of the subclass it was given. For example, though wafer’s Circle class might’ve have its own equals() method, it will now call Disk’s equals() method instead.
Abstract classes are for abstract concepts. For example, there are cellos, violins and violas, but there is no instrument. An instrument class would be an abstract class that cello, violin and viola can extend. This means that abstract class cannot be instantiated, since they are not supposed to represent real objects.
Another example: tree. The tree class could be an abstract class that is extended by different tree types.
Abstract classes are declared with the keyword abstract and are intended to be inherited. These classes can also have abstract methods. These methods only have a declaration, but no body.
Interface: A class with method declarations that have no implementation.
Though it may sound similar to an abstract class, interfaces are completely different. They are not inherited, and can only provide behaviors to a class.
<access level> interface <name> () {
<return_type> <method_name> (<method paraeters>);
//more methods included here that must be implemented in other classes
All methods in an interface have abstract and public scope, so you don’t need to rewrite it in the declaration.
Inherited methods can be defined by the subclass any way they want to, but programmers can include instructions in the comments detailing how an interface method should work.
When a class implements an interface it must define every method in the interface. Implements is also the keyword that is included in the method declaration in order for a class to use an interface.
Classes can implement multiple interfaces.
*Java also has a built in interface called the Comparable interface that has one method: compareTo(Object obj). It has instructions to “return 0 when objects are equal, negative when an obj is less than another, and positive when obj is greater than another.
Inheritance: Extending characteristics of a class to other classes, or defining a specialized version of an existing class.
When a class extends another class, it demonstrates an is-a relationship. This is different from a has-a relationship, where a class might use objects of another class.
The class that is extended is often called the base, super or parent class, and the extension the subclass, child, or derived class.
In a subclass, extra methods and members can be defined, and methods of the superclass can even be overridden. However, not everything has to be changed or redefined. If a subclass wants to access methods of the base class, the keyword super can be used.
However, visibility also plays a role. The super keyword cannot access private methods.
Polymorphism is the ability of objects to assume different types.
For example, though you can declare an object of one class, you can also reassign it a new subclass.
Circle wafer;
Disk cookie = new Disk(2, 0.5);
wafer = cookie; //Wafer began as a circle, now a Disk
After reassignment, the object uses methods of the subclass it was given. For example, though wafer’s Circle class might’ve have its own equals() method, it will now call Disk’s equals() method instead.
Abstract classes are for abstract concepts. For example, there are cellos, violins and violas, but there is no instrument. An instrument class would be an abstract class that cello, violin and viola can extend. This means that abstract class cannot be instantiated, since they are not supposed to represent real objects.
Another example: tree. The tree class could be an abstract class that is extended by different tree types.
Abstract classes are declared with the keyword abstract and are intended to be inherited. These classes can also have abstract methods. These methods only have a declaration, but no body.
Interface: A class with method declarations that have no implementation.
Though it may sound similar to an abstract class, interfaces are completely different. They are not inherited, and can only provide behaviors to a class.
<access level> interface <name> () {
<return_type> <method_name> (<method paraeters>);
//more methods included here that must be implemented in other classes
All methods in an interface have abstract and public scope, so you don’t need to rewrite it in the declaration.
Inherited methods can be defined by the subclass any way they want to, but programmers can include instructions in the comments detailing how an interface method should work.
When a class implements an interface it must define every method in the interface. Implements is also the keyword that is included in the method declaration in order for a class to use an interface.
Classes can implement multiple interfaces.
*Java also has a built in interface called the Comparable interface that has one method: compareTo(Object obj). It has instructions to “return 0 when objects are equal, negative when an obj is less than another, and positive when obj is greater than another.