MODULE 4 JAVA CLASSES AND METHODS CHAPTER 4 Polymorphism and Inheritance
What is Inheritance 4. Java Classes and Methods is when an object or class is based on another object using the same implementation or specifying a new implementation to maintain the same behavior Such an inherited class is called subclass of its parent class or super class It is a mechanism for code reuse and to allow independent extensions of the original software via public classes and interfaces
Types of Inheritance 4. Java Classes and Methods Single inheritance where subclasses inherit the features of one superclass. A class acquires the properties of another class Multiple inheritance where one class can have more than one superclass and inherit features from all parent classes Multilevel inheritance where a subclass is inherited from another subclass
Types of Inheritance 4. Java Classes and Methods Hierarchical inheritance where one class serves as a superclass (base class) for more than one subclass Hybrid inheritance a mix of two or more of the above types of inheritance
Type of Inheritance: Single 4. Java Classes and Methods
Type of Inheritance: Multiple 4. Java Classes and Methods
Type of Inheritance: Multilevel 4. Java Classes and Methods
Type of Inheritance: Hybrid 4. Java Classes and Methods
Type of Inheritance: Hierarchical 4. Java Classes and Methods
Type of Inheritance: Hierarchical 4. Java Classes and Methods
extends Keyword 4. Java Classes and Methods Used to inherit the properties of a class Syntax: class Super{ class Sub extends Super{ …. ….. } }
super Keyword 4. Java Classes and Methods It is used to differentiate the members of superclass from the members of subclass, if they have the same name It is used to invoke the superclass constructor from subclass
Invoking Superclass Constructor 4. Java Classes and Methods If a class is inheriting the properties of another class, the subclass automatically acquires the default constructor of the superclass. But to call a parameterized constructor of the superclass, there is a need to use the super keyword shown as: super(values);
Invoking Superclass Constructor 4. Java Classes and Methods If a class is inheriting the properties of another class, the subclass automatically acquires the default constructor of the superclass. But to call a parameterized constructor of the superclass, there is a need to use the super keyword shown as: super(values);
IS-A Relationship 4. Java Classes and Methods IS-A is way of saying: “this object is a type of that object”
The instanceof Keyword 4. Java Classes and Methods The instanceof operator compares an object to a specified type. You can use it to test if an object is an instance of a class, an instance of a subclass, or an instance of a class that implements a particular interface.
HAS-A Relationship 4. Java Classes and Methods Mainly based on the usage Helps to reduce duplication of code as well as bugs Example public class Vehicle{} public class Speed{} public class Van extends Vehicle{ private Speed sp ; }
Polymorphism 4. Java Classes and Methods Ability of an object to take on many forms When a parent class reference is used to refer to a child class object Any Java object that can pass more than one IS-A test is considered to be polymorphic Example public interface Vegetarian{} public class Animal{} public class Deer extends Animal implements Vegetarian{}
Polymorphism 4. Java Classes and Methods The Deer class is considered to be polymorphic since this has multiple inheritance. Following are true for the above examples: A Deer IS-A Animal A Deer IS-A Vegetarian A Deer IS-A Deer A Deer IS-A Object
Polymorphism 4. Java Classes and Methods When applying the reference variable fact to a Deer object reference, the following declarations are legal: Deer d = new Deer(): Animal a = d; Vegetarian v = d; Object o = d;
Coding Simulation 4. Java Classes and Methods
Sources https://www.tutorialspoint.com/java/index.htm https://www.javatpoint.com/java-tutorial https://dev.java/learn/getting-started/ https://www.w3schools.com/java/default.asp Next Module: File Handling and Exceptions in Java 4. Java Classes and Methods