You are on page 1of 17

Inheritance

Topics covered • Learn about the concept of inheritance • Extend classes • Override superclass methods • Work with superclasses that have constructors • Use superclass constructors that require arguments • Run time polymorphism .

Mechanism that enables one class to inherit both the behavior and the attributes of another class • The classes you create can inherit data and methods from existing classes • You can apply your knowledge of a general category to a more specific category .Learning about the Concept of Inheritance • Inheritance .

Inheritance Terms • Base class – a class that is used as a basis for inheritance – Superclass – Parent class • Derived class .a class that inherits from a base class – Subclass – Child class .

.

Extending classes • Use the keyword extends to achieve inheritance within the Java programming language public class EmployeeWithTerritory extends Employee • Creates a superclass .subclass relationship between Employee and EmployeeWithTerritory .

.

Using the same method name to indicate different implementations • Means “many forms” • Each child class method overrides the method that has the same name in the parent class .Overriding Superclass Methods • Polymorphism.

} public static void main(String[] args) { Cat myCat = new Cat()."). a subclass of Animal. is called Cat: public class Cat extends Animal { public void testInstanceMethod() { subclass System.").println("The instance method in Animal.out.println("The instance method in Cat.testInstanceMethod(). } } The second class.out. myCat.public class Animal { public void testInstanceMethod() Super { class System. } } Example for Overriding Superclass Methods .

Working with Superclasses that Have Constructors .

} } class DemoSuper { public static void main(String args[]) { BoxWeight mybox1 = new BoxWeight(10. class BoxWeight extends Box { double weight. depth = d. and then the subclass constructor executes class Box { private double width. 15. height. double h. double m) { super(w. private double depth. private double height. double d. // call superclass constructor weight = m. } // BoxWeight now uses super to initialize its Box attributes. d). // construct clone of an object Box(double w. height = h. // weight of box // initialize width. } } . double d) { // pass object to constructor width = w. 20. h.3). and depth using super() BoxWeight(double w. 34. the superclass constructor must execute first.double h.Case – 1: • When you create any subclass object.

derived class class Box { private double width. height = ob. BoxWeight myclone = new BoxWeight(mybox1). // construct clone of an object Box(Box ob) { // pass object to constructor width = ob. depth = ob.depth. you are actually calling at least two constructors: – the constructor for the base class – the constructor for the extended. } } .width.3).Case – 2: • When you instantiate an object that is a member of a subclass. } // BoxWeight now fully implements all constructors. 15. private double depth. } class DemoSuper { public static void main(String args[]) { BoxWeight mybox1 = new BoxWeight(10.weight. private double height. 20. 34. weight = ob. // weight of box // construct clone of an object BoxWeight(BoxWeight ob) { // pass object to constructor super(ob).height. class BoxWeight extends Box { double weight.

Using Superclass Constructors that Require Arguments • When you create a class and do not provide a constructor. you replace the automatically supplied version • The constructor you create for a class might require arguments . Java automatically supplies you with one that never requires arguments • When you write your own constructor.

but the first statement must call the superclass constructor – super(list of arguments).Using Superclass Constructors that Require Arguments • When a superclass requires arguments. you must include a constructor for each subclass • Your subclass constructor can contain any number of statements. • Keyword super always refers to the superclass of the class in which you use it .

• It has A class Object. • A a=new A(). • data members . .member methods.Run time polymorphism Class member‟s behavior in inheritance • There are two types of class members. • Methods depends on object in reference variables. • B is sub class of A. • „a‟ is reference variable of A. • It has B class Object. • A a=new B(). • Data members depends on reference variables. • „a‟ is reference variable of A.

} // private void f2() // { // System.").out.println("This is extended class f3 method ").println("This is base class f2 method "). } } .Example :Part 1 • • • • • • • • • • • • class A { String str="Here Str contains base class value (Because this is A's ref variable)".out.out.(Because this is B's ref variable)".println("This is base class f1 method . } final public void f2() { System.println("This is extended class f2 method "+str).out. public void f1() { System. // } public void f3() { System. } } • • • • • • • • • • • • • • • • • class B extends A { String str="Here Str contains extended class value.out.println("This is extended class f1 method "). public void f1() { System.

//B b2=new A(). //not possible b1.out. a1=new B().str).Example: Part 2 • • • • • • • • • • • • • • • • • • • • • • • • • • class C { public static void main (String args[]) { A a1=new A(). a1.f1(). System.f2(). System. System. System.f1().str).out.f2(). a1. a1.str).str).println(a1. B b1=new B().out. // not possible a1.str). b1.str).println(a1. b1. System.f3().println(a1.println(b1. System.f3().println(b1.out.f2(). //a2.println(a1. } } .println(b1.f1().out. System.str).out.out.