2. Disclaimer:This presentation is prepared by trainees of
baabtra as a part of mentoring program. This is not official
document of baabtra –Mentoring Partner
Baabtra-Mentoring Partner is the mentoring division of baabte System Technologies Pvt .
Ltd
3. Inheritance in Java
Ashwin Anand V
Email:ashwinanand99@gmail.com
Facebook id:ashwinanand99@gmail.com
4. Inheritance
A class can extend another class,
inheriting all its data members and
methods while redefining some of
them and/or adding its own.
Inheritance represents the is a
relationship between data types. For
example: a FemaleDancer is a
Dancer.
5. Inheritance Terminology:
subclass superclass
or extends or
derived class base class
public class FemaleDancer extends Dancer
{
...
}
7. Inheritance (cont’d)
Constructors are not inherited. The
FemaleDancer class only adds a constructor:
public class FemaleDancer extends Dancer
{
public FemaleDancer(String steps[],
int x, int y, int dir)
{
leftFoot = new FemaleFoot("left", x, y, dir);
rightFoot = new FemaleFoot("right", x, y, dir);
leftFoot.move(-Foot.getWidth() / 2, 0);
rightFoot.move(Foot.getWidth() / 2, 0);
}
}
9. public class FemaleFoot extends Foot
{
public FemaleFoot(String side, int x, int y, int dir)
{
super(side, x, y, dir); // calls Foot's constructor
}
//
public void drawLeft(Graphics g)
{
...
Supplies methods that
}
are abstract in
public void drawRight(Graphics g) Foot:
{
...
}
}
10. Inheritance may be used to define a
hierarchy of classes in an application:
Object
Foot
MaleFoot FemaleFoot
MaleLeftFoot MaleRightFoot FemaleLeftFoot FemaleRightFoot
11. • If this presentation helped you, please visit
our page facebook.com/baabtra and like it.
Thanks in advance.
• www.baabtra.com | www.massbaab.com |ww
w.baabte.com