Object Oriented Programming Java-Lecture 11 Polymorphism
Abstract Classes and Methods There will be a situation where you want to develop a design of a class which is common to many classes. Abstract class Abstract class purpose is to provide an appropriate super class from which other classes can inherit and thus share the common design Subclasses of an abstract super class has to define the missing pieces in the abstract Superclass Abstract classes are incomplete (they are just a design) so no objects of abstract class can be created Classes for which objects can be created are called concrete classes Concrete classes provide implementation of every method they declare An abstract class contains one or more abstract methods
Abstract Classes and Methods An abstract class contains one or more abstract methods Abstract methods do not provide implementations Each concrete subclass of an abstract superclass must provide concrete implementation of each of the superclass s abstract methods Constructors and static methods can not be declared abstract Constructors are not inherited Static methods can not be overridden
Abstract class and methods Example Implementation in : NumberSystemTest.java
Super class reference Vs Subclass object When we create a abstract superclass reference variable and this variable references to an concrete subclass object, we can use this reference variable to manipulate subclass objects Java run time environment knows which object method should be called. This is one form of polymorphism
Operator instanceof instanceof operator Since we are using abstract super class reference to process subclass objects, we may need to know what is the exact type of the object To check whether an object is an instance of a particular class we use instanceof operator Example: If(obj instanceof OctalNumberSystem) { } System.out.printf( Object is OctalNumberSystem object\n", i);
Allowed assignments between superclass and subclass variables A subclass object is also a superclass object Subclass may have additional members Assigning a superclass reference to a superclass variable Assigning a subclass reference to a subclass variable Assigning subclass reference to a superclass variable Superclass variable can be used to refer only to superclass members If subclass only members are referred compilation error will occur Attempting to assign a superclass reference to a subclass variable is a compilation error. To avoid error you must explicitly type cast At the execution time if the object to which the reference refers is not a subclass object an exception will occur
Final methods A final method in a super class can not be overridden in subclass Methods declared private are implicitly final (it not possible to override) Methods which are declared static are implicitly final A final method declaration can never change. Every subclass use the same method implementation Calls to final methods are resolved at compilation time static binding
Final classes A final class can not be superclass All methods in a final class are implicitly final String class is an example of final class. No subclasses can be made of String class
Java interfaces Interfaces are reference types similar to a class Contains only constants and method signatures (no implementation) One interface can extend none, one or more interfaces. Class can implement many interfaces If a class implements an interface does not implement all the methods then it should be declared abstract
Abstract classes Vs interfaces Abstract Class used to declare common characteristics of subclasses Abstract class can not be instatiated Abstract classes provide template or design for concrete subclasses Abstract class can contain fields and methods. (methods may be abstract) If a class has a abstract method it should be declared abstract Drawback no multiple inheritance Interfaces Can be used to define generic template and one or more abstract classes to define the partial implemetations of the interface Interafce just specifies method declaration (implicitly public abstract) all the fields are public static final Interface can not be instanciated Multiple inheritance is possible. One interface can extend none, one or more interfaces. Class can implement many interfaces If a class implements an interface does not implement all the methods then it should be declared abstract
Interfaces Example Interface Shape TwoDimentionalShape Point ThreeDimentionalShape Quadrilateral Circle Cube Sphere Rectangle Rectangle Implementation in : InterfaceTest.java