Chapter8_PartII

  • Upload
    ghuru

  • View
    221

  • Download
    0

Embed Size (px)

DESCRIPTION

chapter

Citation preview

Slide 1

UNIT- ICHAPTER IN BOOK- 8part- II-K. IndhuSYLLABUS COVERED HEREINHERITANCEK. INDHUGoalsOrder of Constructors Calling in InheritanceMethod OverridingExample for Method OverridingExample with super()Method OverloadingDynamic Dispatch IntroductionExample for Dynamic DispatchAbstract Class in JavaDynamic Dispatch thru Abstract ClassUses of Abstract ClassUsing Final to prevent Overriding (Method)Using Final to prevent Inheriting (Class)Object Class & its methods in Java

K. INDHUOrder of constructorsK. INDHU

Order of constructorsK. INDHU

Method overridingDEFINITION->In a class hierarchy, when a method in a subclass has the same name and type signature as a method in its super-class, then the method in the subclass is said to override the method in the super-class.

When an overridden method is called from a subclass, it will always refer to the version of that method defined by the subclass.

The version of the method defined by the super-class will be hidden.K. INDHUexampleK. INDHU

exampleK. INDHU

Example with super()K. INDHU

Method overloadingK. INDHU

Method overloadingK. INDHU

Dynamic dispatch introMethod overriding forms the basis for one of Javas most powerful concepts: dynamic method dispatch.

Dynamic method dispatch is the mechanism by which a call to an overridden method is resolved at run time, rather than compile time.

Thru Dynamic Dispatch, Java implements run-time polymorphism.K. INDHUDynamic dispatch introA super-class reference variable can refer to a subclass object.

Java uses this fact to resolve calls to overridden methods at run time.

Therefore, if a super-class contains a method that is overridden by a subclass-then when different types of objects of sub-classes are referred to through a super-class reference variable,different versions of the method are executed at run time.K. INDHUDynamic dispatch (ex)K. INDHU

Dynamic dispatch (ex)K. INDHU

Dynamic dispatch (ex)K. INDHU

Dynamic dispatch (ex)K. INDHU

Abstract class in javaDEFINITION FOR ABSTRACT CLASS->A class->that is declared as abstract,which needs to be extended and its abstract method implemented and,that cannot be instantiated, -> is called as Abstract Class.

DEFINITION FOR ABSTRACT METHOD->A method that is declared as abstract and does not have implementation is known as abstract method.

Abstract Class can have both abstract and concrete methods.One of the ways to achieve Abstraction in Java.

K. INDHUAbstract class in javaK. INDHU

Dynamic dispatch thru Abstract classK. INDHU

Dynamic dispatch thru Abstract classK. INDHU

Uses of abstract class(1) It can provide basic functionality, but in order for that functionality to be used, one or more other classes must derive from the abstract class.

(2) Abstract Class is one of the ways to achieve abstraction (OOPS Principle).

(3) One of the major benefits of abstract classes is that you can reuse code without having to retype it. That has a plethora of benefits, such as reducing bugs and making coding faster.K. INDHUUsing finalto Prevent OverridingK. INDHU

Using finalto Prevent inheritingK. INDHU

Object class in javaThere is one special class, Object, defined by Java.

All other classes are subclasses of Object. That is, Object is a super-class of all other classes.

This means that a reference variable of type Object can refer to an object of any other class.

Also, since arrays are implemented as classes, a variable of type Object can also refer to any array.K. INDHUMethods in object classK. INDHU

So far we studiedInheritanceK. INDHUHAPPY LEARNING!!!