Java Presentation
Java Presentation
PRESENTATION
Submitted by:
Divit Wadhwani
Kanish Jadhwani
Guided by:
Sneha Pakle ma’am
Inheritance and
Polymorphism
Inheritance
• Derive a new class (subclass) from an existing
class (base class or superclass). A increasingly
general
• Declaring subclasses
"is a"
class B extends A
{ . . . } B increasingly
– means class B is a specialization of class specialized
A
– the "is a" relationship exists
– a B object is an A object
Inheritance
• Other names:
– superclass also called "parent class"
– subclass also called "child class"
• These names help understand concept of
inheritance
• Child class inherits characteristics of parent
class
– attributes
– methods
Example
class A
{
public:
integer d;
};
class B :
public A
{
public:
};
Example
•
• The class B in the example does not have any direct data
member does it?
• Yes, it does. It inherits the data member d from class A.
acquires
When oneallclass
of its methods
inherits and data.
from another, it
•We can then instantiate an object of class B and call
into that data member.
void func()
{
B b;
b.d = 10;
};
Example: A Trip to Aviary
•Consider a collection of birds
which have different properties
– name
–color (some of the same name
are of different colors)
– they eat different things
– they make different noises
–some make multiple kinds of
sounds
Bird class
• Note Bird class is a super class, previous fig
• All subclasses are derived from this bird class.
• Attributes common to all birds
– color
– food
– movement
Inheritance
• When we say …
class TalkingParrot extends Parrot
{ … }
– then a TalkingParrot object inherits
all
Parrot attributes
– (which, in turn, inherits both
FlyingBird
and Bird attributes)
• In general, descendant classes inherit the
attributes of ancestor classes
Results of Inheritance
• Used to eliminate redundant coding
• When we send toString() message to
a Goose or Parrot or
TalkingParrot object
– none of these classes implement the
toString() method
– but … they inherit it from Bird
– toString() need not be redefined in the
subclasses.
Don’t
• Consider the declaration:
Bird abird = new Goose();
– this is legal
– a Goose object "is a" Bird object
• Contrast
Goose aGoose = new Bird("gray",
"walking", "bugs");
– this is NOT legal
– A Bird object is not necessarily a Goose
object
Types of Inheritance
• Multiple Inheritance
Multiple inheritance
Multilevel Inheritance
Hybrid Inheritance
Polymorphism
Polymorphism
• polymorphism (from the Greek meaning
"having multiple forms") is the characteristic
of being able to assign a different meaning to
a particular symbol or "operator" in different
contexts.
• Polymorphism is about an objects ability to
provide context when methods or operators
are called on the object.
Example
• class A
{
public:
virtual void f()=0;
};
• class B
{
public:
virtual void f()
{std::cout << "Hello from B" << std::endl;};
};
Example
• class C
{
public:
virtual void f()
{std::cout << "Hello from C" << std::endl;};
};
Example
• If I have an object A, then calling the method
f() will produce different results depending on
the context, the real type of the object A.
• func(A & a)
{ A.f
();
};
Overloading
• Ability of one function to perform different
tasks.
• Creating several methods with the same
name which differ from each other in the type
of the input and the output of the function.
• The overloaded function must differ by data
types.
Example
• main()
• { cout<<volume(10); cout<<volume(2.5,8);
cout<<volume(100,75,15); }
• // volume of a cube
int volume(int s)
{ return(s*s*s); }
• // volume of a
cylinder
double volume(double r,int h)
{ return(3.14*r*r*h); }
• // volume of a cuboid
long volume(long l,int b,int h)
{ return(l*b*h); }
Overriding
• overriding is a concept used in inheritance
which involves a base class implementation of
a method .
• Then in a subclass, you would make
another implementation of the method.
• Here is one simple example
Example
•
class Base
{
public:
virtual void DoSomething() {x =
x + 5;} private:
int x;
};
public:
virtual void DoSomething() { y = y + 5;
Base::DoSomething(); } private:
int y;