0% found this document useful (0 votes)
65 views19 pages

Inheritance and Polymorphism

Inheritance allows new classes to acquire properties and behaviors of existing classes. This allows for code reusability. There are different types of inheritance like single, multilevel, and hierarchical. Polymorphism allows the same method to be called in different ways by overriding methods. Method overriding provides specific implementation at runtime, while overloading relies on compile-time resolution based on parameters.

Uploaded by

ANDFAN DAKA
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
65 views19 pages

Inheritance and Polymorphism

Inheritance allows new classes to acquire properties and behaviors of existing classes. This allows for code reusability. There are different types of inheritance like single, multilevel, and hierarchical. Polymorphism allows the same method to be called in different ways by overriding methods. Method overriding provides specific implementation at runtime, while overloading relies on compile-time resolution based on parameters.

Uploaded by

ANDFAN DAKA
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 19

Inheritance and Polymorphism

what is it Inheritance?
what is it Inheritance?

 Inheritance in Java is a mechanism in which one object acquires all


the properties and behaviours of a parent object. It is an important part
of OOPs (Object Oriented programming system).

 The idea behind inheritance in Java is that you can create new classes
 that are built upon existing classes. When you inherit from an existing
class, you can reuse methods and fields of the parent class. Moreover,
you can add new methods and fields in your current class also.

 Inheritancerepresents the IS-A relationship which is also known as


a parent-child relationship.
why use inheritance in java

 For Method Overriding (so runtime polymorphism


 can be achieved).

 For Code Reusability.


Terms used in inheritance
 Class:  A class is a group of objects which have common properties. It is a
template or blueprint from which objects are created.
 Sub Class/Child Class: Subclass is a class which inherits the other class. It is
also called a derived class, extended class, or child class.
 Super Class/Parent Class: Superclass is the class from where a subclass inherits
the features. It is also called a base class or a parent class.
 Reusability: As the name specifies, reusability is a mechanism which facilitates
you to reuse the fields and methods of the existing class when you create a new
class. You can use the same fields and methods already defined in the previous
class.
class syntax

 Public class Dog {


 String breed;
 Int age;
 String colour;
 Void barking (){
 }
 Void hungry(){
 }
 Void sleeping(){
 }
 }
To know more about the Inheritance concept we first need to

know about the following

constructors
access control
Methods
Key words
this
 final
 Extends (To derive a class in java the keyword extend is used )
 Super etc.
Types of inheritance
Single inheritance
When a class inherits another class, it is known as a single inheritance.

Multilevel inheritances
 When there is a chain of inheritance, it is known as multilevel inheritance.

Hierarchical Inheritance
 When two or more classes inherits a single class, it is known as hierarchical inheritance.
polymorphism
what is polymorphism

 The word ‘polymorphism’ literally means ‘a state of having many shapes’ or ‘the
capacity to take on different forms’. When applied to object-oriented
programming languages like Java, it describes a language’s ability to process
objects of various types and classes through a single, uniform interface.

 Polymorphism in Java has two types: Compile time polymorphism (static


binding) and Runtime polymorphism (dynamic binding). Method overloading is
an example of static polymorphism, while method overriding is an example of
dynamic polymorphism.
WHY we need POYMORPHISM

 Polymorphism allows us to perform a single


action in different ways.

 polymorphism allows you to define one interface


and have multiple implementations.
Types of polymorphism
Runtime polymorphism :
is a process in which a call to an
overridden method is resolved at runtime
rather than compile-time.In this process,
an overridden method is called through
the reference variable of a superclass.
The determination of the method to be
called is based on the object being
referred to by the reference variable.
Compile time Polymorphism

 Polymorphism that is resolved during compiler time is


known as static polymorphism. Method overloading is an
example of compile time polymorphism.
Method Overloading: This allows us to have more than
one method having the same name, if the parameters of
methods are different in number, sequence and data types
of parameters.
Different ways to overload the method

There are two ways to overload the method in java


 By changing number of arguments
 By changing the data type

 Example follows
Usage and rules of Java Method
Overriding
Usage
 Method overriding is used to provide the specific implementation of a method
which is already provided by its superclass.
Rules
 The method must have the same name as in the parent class
 The method must have the same parameter as in the parent class.
 There must be an IS-A relationship (inheritance).
how to over lode

 Class animal{
 Public void add (){
// my code
 }
 Public void add(int a, int b){
 //my code
 }
 Public int void add(){
 // my code
 }
Method overriding

 Class Animal {
 public void add() {
 // my animal code
 }
 }
 Class dog extends animal {

 Public void add()


 // modified code
 }
 }

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy