Java Inheritance - GitHub Pages

Transcription

JavaInheritance

Inheritance Same inheritance concept of C in Java with somemodifications– One class inherits the other using extends keyword– The classes involved in inheritance are known assuperclass and subclass– Multilevel inheritance but no multiple inheritance– There is a special way to call the superclass’s constructor– There is automatic dynamic method dispatch Inheritance provides code reusability (code of anyclass can be used by extending that class)Prepared By - Rifat Shahriyar2

Simple InheritancePrepared By - Rifat Shahriyar3

Inheritance and Member Access A class member that has beendeclared as private will remainprivate to its class It is not accessible by any codeoutside its class, includingsubclassesPrepared By - Rifat Shahriyar4

Practical ExamplePrepared By - Rifat Shahriyar5

Superclass variable reference toSubclass objectPrepared By - Rifat Shahriyar6

Using super to call SuperclassConstructorssuper( ) must always be thefirst statement executed insidea subclass’ constructorPrepared By - Rifat Shahriyar7

Using super to call SuperclassConstructorsPrepared By - Rifat Shahriyar8

Using super to access Superclasshidden membersPrepared By - Rifat Shahriyar9

Multilevel InheritanceInside X's constructorInside Y's constructorInside Z's constructorPrepared By - Rifat Shahriyar10

Method OverridingPrepared By - Rifat Shahriyar11

Dynamic Method DispatchFor practical example pleaserefer to FindAreas.javaPrepared By - Rifat Shahriyar12

Abstract Class abstract class Acontains abstract method abstract method f()No instance can be created of an abstract classThe subclass must implement the abstract methodOtherwise the subclass will be a abstract class tooPrepared By - Rifat Shahriyar13

Abstract ClassFor practical example pleaserefer to FindAreas2.javaPrepared By - Rifat Shahriyar14

Anonymous SubclassPrepared By - Rifat Shahriyar15

Using final with InheritanceTo prevent overridingTo prevent inheritancePrepared By - Rifat Shahriyar16

Local Variable Type Inference andInheritance A superclass reference can refer to a derived classobject in Java When using local variable type inference, theinferred type of a variable is based on the declaredtype of its initializer– Therefore, if the initializer is of the superclass type, thatwill be the inferred type of the variable– It does not matter if the actual object being referred to bythe initializer is an instance of a derived classPrepared By - Rifat Shahriyar17

Local Variable Type Inference andInheritanceFor detail example please refer toInheritanceVarDemo.javaThe inferred type is determined by the return type of getObject( ),not by the actual type of the object obtained. Thus, all threevariables will be of type APrepared By - Rifat Shahriyar18

Object Class There is one special class, Object, defined by JavaAll other classes are subclasses of ObjectThat is, Object is a superclass of all other classesThis means that a reference variable of type Objectcan refer to an object of any other class Also, since arrays are implemented as classes, avariable of type Object can also refer to any arrayPrepared By - Rifat Shahriyar19

Object’s toString() The toString( ) method returns a string that containsa description of the object on which it is called Also, this method is automatically called when anobject is output using println() Many classes override this method Doing so allows them to provide a descriptionspecifically for the types of objects that they createPrepared By - Rifat Shahriyar20

Object’s toString()Prepared By - Rifat Shahriyar21

Object’s equals() and hashCode() is a reference comparison, whether bothvariables refer to the same object Object’s equals() method does the same thing String class override equals() to check contents If you want two different objects of a same class tobe equal then you need to override equals() andhashCode() methods– hashCode() needs to return same value to work properlyas keys in Hash data structuresPrepared By - Rifat Shahriyar22

Object’s equals() and hashCode()Prepared By - Rifat Shahriyar23

Inheritance Same inheritance concept of C in Java with some modifications –One class inherits the other using extends keyword –The classes involved in inheritance are known as superclass and subclass –Multilevel inheritance but no multiple inheritance –Th