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