Advanced Inheritance Concepts
Advanced Inheritance Concepts
• Abstract class
– A class from which you cannot create any concrete
objects, but from which you can inherit
– You can only extend abstract classes
– Use the keyword abstract
– You cannot use the keyword new
Creating and Using Abstract Classes
• Abstract method
– A method with no method statements
• To create an abstract method, you provide
– the keyword abstract
– the intended method type, name, and arguments
– but you do not provide any statements within the
method
• You must code a subclass method to override any inherited
abstract superclass method
Using Dynamic Method Binding
• When you create a superclass and one or more subclasses, each
object of the subclass “is a” superclass object
– Because every subclass “is a” superclass member, you can convert subclass
objects to superclass objects
• When you create a number of classes that inherit from each other,
you will often find it convenient to place these classes in a package
Design Hints for Inheritance
• Place common operations and fields in the superclass
• Don’t overuse protected fields.
– Malcious subclasses can access these fields.
• Use inheritance to model the “is-a” relationship.
• Don’t use inheritance unless all nherited methods make sense.
• Use polymorphism, not type information.
– if (x is of type 1)
action
else if (x is of type 2) …
think polymorphism.
• Don’t overuse reflection.
– reflection is used for tools, not applications.