13 inheritance


Published on

  • Be the first to comment

  • Be the first to like this

13 inheritance

  1. 1. Inheritance<br />DhrubojyotiKayal<br />
  2. 2. Need for inheritance<br />Inheritance<br />Rules<br />Syntax<br />Method Overriding<br />Inheritance and method call<br />Protected<br />Upcasting<br />Agenda<br />
  3. 3. You create a class Vehicle and then forced to create a new class Truck because the later has some behavior(methods) similar to Vehicle but some rather unique<br />You want to re-use the code written in class Vehicle in other similar classes.<br />Need for inheritance<br />
  4. 4. Take the Vehicle class, clone it and create a new class Truck<br />Keep the common behavior in clone<br />Make additions and modifications in the clone<br />That’s inheritance for you!<br />Original class = base class<br />Clone class = derived class<br />It creates a new class as a type of an existing class. <br />You literally take the form of the existing class and add code to it without modifying the existing class. <br />Inheritance<br />
  5. 5. When base class is changed derived class automatically gets those changes. Reverse is not true.<br />Any number of classes can be derived from base class.<br />Put re-usable or generic behavior in base class<br />Put specific behavior in derived class<br />Represents is-a relationship<br />Derived class can add additional methods and fields.<br />Base class fields are not inherited. Logical they don't define special behavior<br />Private methods are not inherited<br />Rules<br />
  6. 6. You’re always doing inheritance when you create a class, because unless you explicitly inherit from some other class, you implicitly inherit from Java’s standard root class Object <br />extends <br />public class Rectangle extends Shape {<br />}<br />Syntax<br />
  7. 7. Inheritance in Action<br />public class Rectangle<br /> private double length;<br /> private double breadth;<br /> extends Shape{<br /> public void draw() {<br />System.out.println(“Drawing a rectangle : length : ” + length + “ breadth : ” + breadth);<br /> }<br /> public void setDimensions (double len,doublebrd) {<br /> length = len;<br /> breadth = brd;<br />}<br />}<br />public class Shape {<br /> public void draw() {<br />System.out.println(“Drawing a shape”);<br /> }<br />}<br />Method Overriding<br />
  8. 8. Changing the base class method in the derived class<br />You need more specific behavior relevant to the derived class<br />Derived class method must have exactly the same signature and return type as in the base class method<br />@Override<br />New in Java SE 5<br />Prevents accidental overload instead of override<br />Method Overriding<br />
  9. 9. Rectangle rectangle = new Rectangle()<br />rectangle.draw()? <br />Base class draw?<br />Derived class draw?<br />“Lowest one wins”<br />Lowest on inheritance tree<br />Inheritance and Method call<br />
  10. 10. Create a Java class ‘Animal’ with the following methods:<br />makeNoise();<br />eat();<br />sleep();<br />roam();<br />Create a class ‘Canine’ inheriting from ‘Animal’ and overrides method roam();<br />Create a class ‘Wolf’ inheriting from ‘Canine’ and inheriting from ‘Canine’ and overrides methods – makeNoise(), eat()<br />Each method return type is void and just prints the class name followed by the method name.<br />Create a test class with a main method.<br />In the main method create an instance of Wolf and invoke all the four methods in that order.<br />Exercise (courtesy HFJava)<br />
  11. 11. You want to keep the base class functionality and yet change the behavior in derived class<br />So you need a mechanism to call the base class method<br />Use super to invoke the immediate base class method <br />super.methodName();<br />Super<br />
  12. 12. Change the method makeNoise() in Wolf class to invoke the makeNoise method in the parent class<br />Exercise<br />
  13. 13. This is private as far as the class user is concerned, but available to anyone who inherits from this class or anyone else in the same package.<br />protected<br />
  14. 14. Create a package – myshape<br />Create a class Shape with one method draw which prints the class name and the method name<br />Create a class MyShapeUser with a main method. Create an instance of Shape and invoke the draw method<br />Create a new package yourshape<br />Create a class YourShapeUser with a main method. Create an instance of Shape in myshape package and try to invoke the draw method<br />Now create a class YourShape which extends Shape in myshape package.<br />Create an instance of YourShape in YourShapeUser and try to invoke the draw method<br />Exercise<br />
  15. 15. The new class is a type of the existing class<br />Java supports assigning derived class objects to base class reference<br />Shape shape = new Rectangle();<br />The act of converting a derived (Rectangle) reference into a base (Instrument) reference is called upcasting.<br />The term is derived from how inheritence tree is drawn<br />Opposite is downcasting<br />Upcasting<br />
  16. 16. Q&A<br />