Download presentation
Presentation is loading. Please wait.
Published byDaniel Richards Modified over 6 years ago
1
A Concrete Presentation on Abstract Classes and Methods, Interfaces, and Polymorphism
CSC 202
2
Overview This attempts to show the relationships among several concepts supporting polymorphism. Abstract classes Abstract methods The class Object Interfaces Inheritance and implementation Polymorphic references Casting
3
Abstract Classes An abstract class must have the modifier abstract included in the class heading. An abstract class usually has at least one abstract method. You can’t create an object using an abstract class constructor, but you can use an abstract class as a base class to define a derived class.
4
Abstract Class as a Type
An abstract class is a type. You can define variables whose type is an abstract class and you can have parameters whose type is an abstract type.
5
Abstract Methods Serves as a placeholder for a method that will be fully defined in a descendant class. Has a complete method heading with the addition of the modifier abstract. Has no method body but does end with a semicolon in place of a method body. Can’t be private.
6
Abstract Methods To summarize, the abstract keyword allows you to create one or more methods in a class that have no definitions, i.e., You provide the method signature w/o providing a corresponding implementation, which is created by inheritors.
7
Interface The interface keyword allows creation of a class that has no implementation at all. Consider it a “pure” abstract class. The form for a class can be established: method names, argument lists, and return types, but no method bodies. An interface can contain fields that are implicitly static and final.
8
Interface An interface says, “This is what all classes that implement this particular interface look like.” A class that implements an interface should provide implementation for all its methods.
9
The implements Keyword
A class may implement as many interfaces as necessary. An interface may extend zero or more interfaces but may not extend any classes.
10
Inheritance in Java Single inheritance through class extension.
Multiple “inheritance” through interface extension and interface implementation.
11
Polymorphism Enabled in Java-1
Allows a subclass object to be referenced by a supertype reference. Animal myDog = new Dog(); Allows use of polymorphic arrays and ArrayLists. for (Employee emp:everyWorker) { emp.chainOfCommand(); }
12
Polymorphism Enabled in Java-2
The Object class As the supertype for all objects, an Object reference can be made to all objects. Object sumpn = new Dawg(); See Example.
13
Polymorphism Enabled in Java-3
A supertype reference can not invoke a method that does not exist in some form in the superclass. Object sumpn = new Dawg(); sumpn.walk(); // Nope. Permissible method calls are based on the class of the reference type, not the object type. Object has no walk().
14
Speaking of Object . . . The Object class is the “kernel” object for every class. Think of a user-defined class as an outer shell that includes all its super- classes as “inner” shells with, of course, the Object as the innermost.
15
Polymorphism Enabled in Java-4
Because which method calls are permissible is based on the class of the reference type, it may be desirable to cast an object reference to its real type. Object X = new Dawg(); Dawg y= (Dawg) x; y.bark();
16
Polymorphism Enabled in Java-5
To make sure the cast will not be “cast out” by the compiler, the instanceof operator can be used. If(x instanceof Dawg){ Dawg y = (Dawg) x;
17
Polymorphism Enabled in Java-6
One way that polymorphism is enhanced by interfaces is that the objects can be from anywhere in the inheritance tree. That is, there is not a strict requirement for an inheritance hierarchy. We will hear more of this . . .
18
Summary The use of these capabilities to support polymorphism was discussed. Abstract methods Abstract classes The class Object Interfaces Inheritance and implementation Polymorphic references Casting
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.