Inheritance and polymorphism in Java ppt

inheritance in java ppt with example and inheritance in java with example programs ppt
WilliamsMcmahon Profile Pic
WilliamsMcmahon,United States,Professional
Published Date:20-07-2017
Your Website URL(Optional)
Inheritance and the Collection classesInheritance in Java  Inheritance is implemented using the keyword extends. public class Employee extends Person //Class definition goes here – only the //implementation for the specialized behavior  A class may only inherit from only one superclass.  If a class is not derived from a super class then it is derived from java.lang.Object. The following two class declarations are equivalent: public class Person … public class Person extends Object … www.ThesisScientist.comPolymorphism  If Employee is a class that extends Person, an Employee “is-a” Person and polymorphism can occur. Creates an array of Person references Person p = new Person2; p0 = new Employee(); p1 = new Person(); www.ThesisScientist.comPolymorphism (cont.)  However, a Person is not necessarily an Employee. The following will generate a compile-time error. Employee e = new Person();  Like C++, polymorphism requires general class on left of assignment operator, and specialized class on right.  Casting allows you to make such an assignment provided you are confident that it is ok. public void convertToPerson(Object obj) Person p = (Person) obj; www.ThesisScientist.comVirtual Method Invocation  In Java, virtual method invocation is automatic. At runtime, the JVM determines the actual type of object a reference points to. Then, the JVM selects the correct overridden method for it.  Supposing the Employee class overrides the toString method inherited from the Person class, then the toString method of the derived class, Employee, is invoked even though the reference is a Person reference. Person p = new Employee; Invokes the toString method p.toString(); of the Employee class www.ThesisScientist.comWhat is inherited by the subclass?  All fields are inherited. Giving fields in super classes protected access allows methods of subclasses to reference the fields.  All methods are inherited except for constructors.  Inherited methods may be overloaded or overridden. www.ThesisScientist.comConstructors and Inheritance  The superclass constructors are always called by the constructors of the subclasses, either implicitly or explicitly.  To explicitly call the superclass constructor, in the first line of the subclass constructor make a call to the super method passing the appropriate parameters for the desired constructor. www.ThesisScientist.comThe super Reference  All overridden methods in a subclass also contain a reference to their corresponding methods in the superclass named super.  The following code contains the use of the super reference to call the super class constructor and to use the implementation of the toString method of the superclass.  Notice it also contains several uses of the this reference. www.ThesisScientist.comSuper Class Example public class Person protected String name; private int age; public Person(String name, int age) = name; this.age = age; public Person(String name) this(name, 0); Call to other constructor public String toString() return name; public int getAge() return age; public void setAge(int age) this.age = age; public void setName(String name) = name; www.ThesisScientist.comSubclass Example public class Employee extends Person private double salary; public Employee(String name, int age, double sal) Call to superclass constructor super(name,age); salary = sal; public Employee(String name, double salary) this(name,18, salary); Call to constructor above public double getSalary() return salary; public void setSalary(double sal) salary = sal; public String toString() Call to superclass toString method return super.toString() + “ has a salary of “ + salary; www.ThesisScientist.comPolymorphism in Action public class Test public static void main(String args) Person people = new Person3; people0 = new Person("Sam"); people1 = new Employee("Jane",45345.63); for(Person someone:people) System.out.println(someone); Output println invokes the toString method of Sam the object the reference is pointing to, Jane has a salary of 45345.63 as if it were a pointer in C++ and the null toString method were virtual. www.ThesisScientist.comAbstract Classes and Methods  Java also has abstract classes and methods like C++. If a class has an abstract method, then it must be declared abstract. public abstract class Node Abstract methods have no String name; implementation. public abstract void type(); public String toString() return name; public Node(String name) = name; www.ThesisScientist.comSubclass of Abstract Class  Subclass of an abstract class must provide implementation for ALL the abstract methods or it must be declared abstract as well. public class NumberNode extends Node int number; public void type() System.out.println(“Number node”); public NumberNode(String name, int num) super(name); number = num; public String toString() return super.toString() + “ “ + number; www.ThesisScientist.comMore about Abstract Classes  Like C++, abstract classes can not be instantiated. // OK because n is only a reference. Node n; // OK because NumberNode is concrete. Node n = new NumberNode(“Penta”, 5); // Not OK. Gives compile error. Node n = new Node(“Name”); www.ThesisScientist.comMultiple Inheritance in Java  There are always cases where a class appears to have characteristics of more than one class. Consider the following hierarchy. Person Student Employee TA has characteristics of TA both a Student and an Employee www.ThesisScientist.comInterfaces  Java only allows a class to extend one super class. It does not allow multiple inheritance like C++. However, to cope with the need for multiple inheritance, it created interfaces.  An interface is like class without the implementation. It contains only  public, static and final fields, and  public and abstract method headers (no body). www.ThesisScientist.comInterface Example  A public interface, like a public class, must be in a file of the same name.  The methods and fields are implicitly public and abstract by virtue of being declared in an interface. public interface Employable void raiseSalary(double d); double getSalary(); www.ThesisScientist.comInterfaces (cont.)  Many classes may implement the same interface. The classes may be in completely different inheritance hierarchies.  A class may implement several interfaces. public class TA extends Student implements Employable / Now TA class must implement the getSalary and the raiseSalary methods here / www.ThesisScientist.comInheritance Progression Inheritance of Inheritance of Implementation Interface Point AbstractShape Person 3DPoint ColorPoint Square Circle Student Employee TA Employable Note: In UML (Unified Modeling Language) •Solid line means extends a superclass. •Dotted line means implements an interface. Bear www.ThesisScientist.comThe Collections Framework  The Java Collections Framework implements a lot of the functionality of the C++ Standard Template Library.  It is a collection of interfaces, abstract and concrete classes that provide generic implementation for many of the data structures you will be learning about in this course.