Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Software Engineering Module: Core of Java Topic: Inheritance TALENTSPRINT | © Copyright 2012 Inheritance By the end of this session, you will be able to: • Define inheritance • Explain the need for Inheritance • Write Java code to create classes and subclasses in an inheritance hierarchy • Explain constructor calling chain • Use “super” keyword • Create hierarchy of classes and use them TALENTSPRINT | © Copyright 2012 2 Inheritance Write a java program for the following specifications • There will be three shapes - Square, Triangle and Circle. • When the user selects a shape the shape rotates clockwise 3600 if it is png. EXERCISE Change In Specification: • Add one more Shape – amoeba • When user selects amoeba it rotates only if it is a jpg image One more change: The amoeba rotation point should be at the corner not centre. TALENTSPRINT | © Copyright 2012 3 Inheritance OO Thinking TALENTSPRINT | © Copyright 2012 4 Inheritance Inheritance is the concept of a child class (sub class) automatically inheriting the variables and methods defined in its parent class (super class). TALENTSPRINT | © Copyright 2012 5 Inheritance Deriving a Subclass To derive a child class, we use the extends keyword. Example: Suppose we have a parent class called Person. Then a subclass Student can be created as . . . public class Person { protected String name; protected String address; /** * Default constructor */ public Person(){ System.out.println(“Inside Person:Constructor”); name = ""; address = ""; } . . . . } TALENTSPRINT | © Copyright 2012 public class Student extends Person { public Student(){ System.out.println(“Inside Student:Constructor”); } . . . . } 6 Inheritance Create classes for Shapes activity specifications as follows and complete the program. • Superclass Shape with methods rotate() and playSound(). • Subclasses Square, Circle, Triangle, Amoeba. • Then create a main class and then use the above class to complete the previous Activity. TALENTSPRINT | © Copyright 2012 EXERCISE 7 Inheritance What one can do in a Sub-class regarding Attributes The inherited attributes can be used directly, just like any other attributes. You can declare new attributes in the subclass that are not in the super class. You can declare an attribute in the subclass with the same name as the one in the super class, thus hiding it (not recommended). A subclass does not inherit the private members of its parent class. However, if the super class has public or protected methods for accessing its private fields, these can also be applied by the subclass TALENTSPRINT | © Copyright 2012 8 Inheritance What one can do in a Sub-class regarding Methods The inherited methods can be used directly as they are. You can declare new methods in the subclass that are not in the super class. You can write a new instance method in the subclass that has the same signature as the one in the super class, thus overriding it. TALENTSPRINT | © Copyright 2012 9 Inheritance Object Class Object class is mother of all classes in java . Following are some of the important methods of the Object class: getClass() equals() toString() TALENTSPRINT | © Copyright 2012 10 Inheritance Need for “super” Let us consider the following code snippet class A { int a1,a2,a3; public A(int m1, int m2, int m3){ a1 = m1; a2 = m2; a3 = m3; } } class B extends A { int a4,a5; public B(int x1, int x2, int x3, int x4, int x5) { a1=x1; a2=x2; a3=x3; a4=x4; a5=x5; } } TALENTSPRINT | © Copyright 2012 class B extends A{ int a4,a5; public B(intx1,intx2,intx3, intx4,intx5) { super(x1,x2,x3); a4=x4; a5=x5; } } Calls the super class constructor 11 Inheritance What is “super” keyword? A subclass can explicitly call a constructor of its immediate super class using the super constructor call e.g.: super(). A super constructor call in the constructor of a subclass will result in the execution of relevant constructor from the super class, based on the arguments passed. TALENTSPRINT | © Copyright 2012 12 Inheritance Few things to remember when using the super constructor call: The super() call must occur as the first statement in a constructor. The super() call can only be used in a constructor (not in ordinary methods). Another use of super is to refer to members of the super class (just like the this reference ). TALENTSPRINT | © Copyright 2012 13 Inheritance How Constructor Method of a Super Class gets Called A subclass constructor invokes the constructor of the superclass implicitly. When an object of the Student class (subclass) is instantiated, the default constructor of its super class i.e. Person, is invoked implicitly before the statements in the constructor method of the subclass are executed. Used explicitly when passing parameters to the constructor of the super class. TALENTSPRINT | © Copyright 2012 14 Inheritance Constructor Calling Chain Carnivores Mammal Animal class Carnivores extends Mammal class Mammal extends Animal class Animal If an object of Carnivores is created as Carnivores carniv = new Carnivores(); Constructor of Carnivores Constructor of Mammal Constructor of Animal { System.out.println (“Carnivores executed last"); { System.out.printl n(“Mammal executed second"); { System.out.println (" Animal executed first."); } } } TALENTSPRINT | © Copyright 2012 15 Inheritance Constructor Calling Chain public class Animal { public Animal() { System.out.println (“Animal executed first."); } } public class Mammal extends Animal { public Mammal() { System.out.println (“Mammal executed second"); } } public class Carnivore extends Mammal{ public Carnivore() { System.out.println ("Carnivores executed last"); } } Output : Animal executed first. Mammal executed second Carnivores executed last TALENTSPRINT | © Copyright 2012 16 Inheritance Java compiler and “super” keyword The Java compiler automatically inserts the necessary constructor calls in the process of constructor chaining, or we can do it explicitly. The Java compiler inserts a call to the parent constructor (super) if we don't have a constructor call as the first statement of our constructor. Example: public Point(int x, int y) { super(); // Automatically done if you don't call constructor here. m_x = x; m_y = y; } TALENTSPRINT | © Copyright 2012 17 Inheritance Default Constructor class A{ int x1; public A(int x){ x1=x; } } class Test{ public static void main(String args[]){ A oa= new A(); } } class Test{ public static void main(String args[]){ A oa= new A(10); } } Note : The java compiler does not insert default constructor into class A when there is already a user defined constructor. TALENTSPRINT | © Copyright 2012 18 Inheritance Execute the following code class SuperClass{ public SuperClass(){ System.out.println(“In SuperClass”); } } class SubClass extends SuperClass{ public SubClass( int att1){ System.out.println("In SubClass"); } } class SubSubClass extends SubClass{ public SubSubClass(){ System.out.println("In SubSubClass"); } } class MainClass{ public static void main(String args[]){ new SubSubClass(); } } class SuperClass{ public SuperClass(){ System.out.println(“In SuperClass”); } } class SubClass extends SuperClass{ public SubClass( int att1){ System.out.println("In SubClass"); } } class SubSubClass extends SubClass{ public SubSubClass(){ super(10); System.out.println("In SubSubClass"); } } class MainClass{ public static void main(String args[]){ new SubSubClass(); } } When the super class constructor is overloaded, it must be called explicitly. TALENTSPRINT | © Copyright 2012 19 Inheritance Create a class Employee and the sub classes Manager and Clerk, Employee: Instance variables: name, empId, salary. Methods: set and get methods for name, empId, getSalary, setSalary --- Method Manager: Instance variables: type Methods: setSalary() Clerk: Instance variables : int speed , int accuacy Methods: setSalary() EXERCISE Provide proper constructors for all classes. Create a general class “MyClass”. In this clas,s create objects of Manager, Clerk and Employee class and then set the name, empId and salary attributes for each object and display them. TALENTSPRINT | © Copyright 2012 20 Inheritance TALENTSPRINT | © Copyright 2012 21