Вы находитесь на странице: 1из 28

UNIT- I

CHAPTER IN BOOK- 8
PART- II
-K. Indhu

SYLLABUS COVERED
HERE

INHERITANCE

K. INDHU

GOALS
1. Order of Constructors Calling in Inheritance
2. Method Overriding
3. Example for Method Overriding
4. Example with super()
5. Method Overloading
6. Dynamic Dispatch Introduction
7. Example for Dynamic Dispatch
8. Abstract Class in Java
9. Dynamic Dispatch thru Abstract Class
10.Uses of Abstract Class
11.Using Final to prevent Overriding (Method)
12.Using Final to prevent Inheriting (Class)
13.Object Class & its methods in Java
K. INDHU

ORDER OF
CONSTRUCTORS

K. INDHU

ORDER OF
CONSTRUCTORS

K. INDHU

METHOD OVERRIDING

DEFINITION->
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 superclass will be hidden.


K. INDHU

EXAMPLE

K. INDHU

EXAMPLE

K. INDHU

EXAMPLE WITH
SUPER()

K. INDHU

METHOD
OVERLOADING

K. INDHU

METHOD
OVERLOADING

K. INDHU

DYNAMIC DISPATCH
INTRO

Method 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. INDHU

DYNAMIC DISPATCH
INTRO

A 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. INDHU

DYNAMIC DISPATCH
(EX)

K. INDHU

DYNAMIC DISPATCH
(EX)

K. INDHU

DYNAMIC DISPATCH
(EX)

K. INDHU

DYNAMIC DISPATCH
(EX)

K. INDHU

ABSTRACT CLASS IN
JAVA

DEFINITION 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. INDHU

ABSTRACT CLASS IN
JAVA

K. INDHU

DYNAMIC DISPATCH
THRU ABSTRACT
CLASS

K. INDHU

DYNAMIC DISPATCH
THRU ABSTRACT
CLASS

K. 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. INDHU

USING FINAL
TO PREVENT
OVERRIDING

K. INDHU

USING FINAL
TO PREVENT
INHERITING

K. INDHU

OBJECT CLASS IN JAVA

There 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. INDHU

METHODS IN OBJECT
CLASS

K. INDHU

SO FAR WE STUDIED
Inheritance

K. INDHU

HAPPY
LEARNING!!!

Вам также может понравиться