Software Engineering Module Core of Java Topic Inheritance





















- Slides: 21

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 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 play. Sound(). • 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: get. Class() equals() to. String() TALENTSPRINT | © Copyright 2012 10

Inheritance Need for “super” Let us consider the following code snippet class A { int a 1, a 2, a 3; public A(int m 1, int m 2, int m 3){ a 1 = m 1; a 2 = m 2; a 3 = m 3; } } class B extends A { int a 4, a 5; public B(int x 1, int x 2, int x 3, int x 4, int x 5) { a 1=x 1; a 2=x 2; a 3=x 3; a 4=x 4; a 5=x 5; } } TALENTSPRINT | © Copyright 2012 class B extends A{ int a 4, a 5; public B(intx 1, intx 2, intx 3, intx 4, intx 5) { super(x 1, x 2, x 3); a 4=x 4; a 5=x 5; } } 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 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. println (“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 x 1; public A(int x){ x 1=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 Super. Class{ public Super. Class(){ System. out. println(“In Super. Class”); } } class Sub. Class extends Super. Class{ public Sub. Class( int att 1){ System. out. println("In Sub. Class"); } } class Sub. Class extends Sub. Class{ public Sub. Class(){ System. out. println("In Sub. Class"); } } class Main. Class{ public static void main(String args[]){ new Sub. Class(); } } class Super. Class{ public Super. Class(){ System. out. println(“In Super. Class”); } } class Sub. Class extends Super. Class{ public Sub. Class( int att 1){ System. out. println("In Sub. Class"); } } class Sub. Class extends Sub. Class{ public Sub. Class(){ super(10); System. out. println("In Sub. Class"); } } class Main. Class{ public static void main(String args[]){ new Sub. Class(); } } 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, emp. Id, salary. Methods: set and get methods for name, emp. Id, get. Salary, set. Salary --- Method Manager: Instance variables: type Methods: set. Salary() Clerk: Instance variables : int speed , int accuacy Methods: set. Salary() EXERCISE Provide proper constructors for all classes. Create a general class “My. Class”. In this clas, s create objects of Manager, Clerk and Employee class and then set the name, emp. Id and salary attributes for each object and display them. TALENTSPRINT | © Copyright 2012 20

Inheritance TALENTSPRINT | © Copyright 2012 21