CSCI 1006 Lecture 6: Polymorphism
Polymorphism
● Polymorphisms allow sharing attributes between different classes.
● Java classes can be organized into a hierarchy using superclasses and
subclasses.
● Subclasses inherit all instance variables and methods from their
superclasses.
● Superclasses are more general.
● Every java class is a subclass of object.
● Any class can be a superclass.
■ Any class can be a subclass if its code says “public <Subclass> extends
<Superclass>”.
● Subclasses can have new instance variables and methods.
■ They can also have new definitions of inherited methods. This is known as
overriding.
■ To use a superclass version of a method, they keyword super is used.
Super refused to an object of the class one level up.
■ The super( ) constructor, used at the first line of a subclass constructor,
can be used to mimic a constructor from the superclass. Otherwise, if the
super( ) constructor is used later in the code, memory is already allocated,
and it will not run.
● In order to allow instance variables to be edited by subclasses, the protection
protected is used.
find more resources at oneclass.com
find more resources at oneclass.com
Document Summary
Polymorphisms allow sharing attributes between different classes. Java classes can be organized into a hierarchy using superclasses and subclasses. Subclasses inherit all instance variables and methods from their superclasses. Every java class is a subclass of object. Any class can be a subclass if its code says public extends. Subclasses can have new instance variables and methods. They can also have new definitions of inherited methods. To use a superclass version of a method, they keyword super is used. Super refused to an object of the class one level up. The super( ) constructor, used at the first line of a subclass constructor, can be used to mimic a constructor from the superclass. Otherwise, if the super( ) constructor is used later in the code, memory is already allocated, and it will not run. In order to allow instance variables to be edited by subclasses, the protection protected is used.