Module 8 Polymorphism And Inheritance Object Oriented ProgrammingJava

Module 8: Polymorphism And Inheritance Object Oriented Programming(Java)

Polymorphism Ø Polymorphism is the ability of objects belonging to different types to respond to methods of the same name, each one according to the right type-specific behavior. Ø It is the ability to redefine methods for derived classes.

Polymorphism Implementing Polymorphism 1. Method Overloading Using one method identifier to refer to multiple functions in the same class, In the Java programming language, methods can be overloaded but not variables or operators.

Polymorphism Method Overloading Ø Constructor Overloading - creating more than one constructor in a class Ø Method Overloading - creating multiple methods having same name in one class.

Polymorphism Example : Constructor Overloading public Student(){ … } public Student(String name, int stud. No){ this. name = “Anonymous”; } //more constructor here…

Polymorphism Example : Method Overloading public void eat(){ … } public void eat(String food){ System. out. println(“The animal is eating “ +food); } //more methods here…

Polymorphism Implementing Polymorphism 2. Method Overriding Providing a different implementation of a method in a subclass of the class that originally defined a method.

Polymorphism Example : Method Overriding public class Electronic. Device{ … public void on(){ System. out. println(“The device is turned on!”); } } public class Computer extends Electronic. Device{ public void on(){ System. out. println(“The computer boots…”); System. out. println(“The computer loads drivers…”); System. out. println(“Welcome to Windows XP!”); } }

Polymorphism Overloading VS. Overriding Overloaded functions supplement each other. Overloaded functions can exist, in any number, in the same class. Overloaded functions must have different argument lists. The return type of an overloaded function may be chosen freely. Overriding function replaces the function it overrides. Each function in a base class can be overridden at most once in any one derived class. Overriding functions must have argument lists of identical type and order. The return type of an overriding method must be identical to the function it overrides.

Inheritance ØThe technique of deriving new class definitions from an existing class definition.

Inheritance ØThe following are the benefits of using class inheritance in OOP: • Re-use of predefined and well-tested classes • Standardization of behaviors across a group of classes • Ability to use members of a family of classes interchangeably in methods

Inheritance Superclasses and Subclasses ØSuperclass is the class from which another class inherits properties. This is a class that is on top of a hierarchy.

Inheritance Superclasses and Subclasses ØSubclass is a class that inherits all the nonprivate attributes and methods, except constructors from a superclass. This class has the ability to override methods of the superclass.

Inheritance Vehicle Class Hierarchy Vehicle Water Vehicle Land Vehicle Air Vehicle Truck Car Family Car Luxury Car Sports Car

Inheritance Syntax for Implementing Inheritance:

Inheritance Types of Inheritance:

Inheritance Types of Inheritance:

Inheritance Take Note: Java does not support multiple inheritance, however, Java provides same effects and benefits of multiple inheritance through the use of interface.

Inheritance The this and the super Keywords: Øthis – contains a reference to the current object being constructed. It represents an instance of the class in which it appears. It can be used to access class variables and methods.

Inheritance The this and super Keywords: Øsuper – contains a reference to the parent class (superclass) object. It is used to access members of a class inherited by the class in which it appears.

Inheritance The Abstract Class and Interface: ØAbstract Class – contains one or more abstract methods and, therefore, can never be instantiated. It is defined so that other classes can extend them and make them concrete by implementing the abstract methods.

Inheritance

Inheritance Syntax for Declaring Abstract Class:

Inheritance The Abstract Class and Interface: ØInterface – an abstract class that represents a collection of method definitions and constant values. It can later be implemented by classes that define the interface using the implements keyword.

Inheritance

Inheritance Take Note: All abstract classes are public by default and cannot be instantiated. Constructors and public methods cannot be declared as abstract.

Questions and Comments ?
- Slides: 27