0% found this document useful (0 votes)
35 views30 pages

Inheritance Basics

Inheritance allows the creation of hierarchical classifications where a general superclass can define common traits that are inherited by more specific subclasses. The superclass is also called the base, parent, or super class while subclasses are also known as derived, child, or extended classes. Subclasses inherit all instance variables and methods of the superclass and can add their own unique attributes. To inherit a class, the extends keyword is used in the subclass declaration. Private members of the superclass are not accessible to subclasses. Inheritance allows the creation of subclasses that precisely tailor their own classifications by extending the general aspects defined in the superclass.

Uploaded by

Ganesh Nelluri
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)
35 views30 pages

Inheritance Basics

Inheritance allows the creation of hierarchical classifications where a general superclass can define common traits that are inherited by more specific subclasses. The superclass is also called the base, parent, or super class while subclasses are also known as derived, child, or extended classes. Subclasses inherit all instance variables and methods of the superclass and can add their own unique attributes. To inherit a class, the extends keyword is used in the subclass declaration. Private members of the superclass are not accessible to subclasses. Inheritance allows the creation of subclasses that precisely tailor their own classifications by extending the general aspects defined in the superclass.

Uploaded by

Ganesh Nelluri
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/ 30

Inheritance-Basics

Inheritance-Basics
• Inheritance is one of the cornerstones of object-
oriented programming because it allows the
creation of hierarchical classifications.
• Using inheritance, you can create a general class
that defines traits common to a set of related
items.
• This class can then be inherited by other more
specific classes, each adding those things that
are unique to it.
• In the terminology of Java, a class that is inherited is
called a superclass.
• Superclass  is also called as base class or parent
class.
• The class that does the inheriting is called a subclass.
• subclass is also called derived class or child class
or extended class.
Inheritance-Basics Cont…
Super Class Sub Class

Base Class Derived Class

Parent Class Child Class


• A subclass is a specialized version of a
superclass.
• It inherits all of the instance variables and
methods defined by the superclass and adds
its own unique elements.
• To inherit a class, you simply incorporate the
definition of one class into another by using
the extends keyword.
• The general form of a class declaration that
inherits a superclass is shown here:
class subclass-name extends superclass-name {
// body of class
}
LTC: program to illustrate inheritance
// Create a superclass.
class A {
int i, j;
void showij() {
System.out.println("i and j: " + i + " " + j);
}
}
// Create a subclass by extending class A.
class B extends A {
int k;
void showk() {
System.out.println("k: " + k);
}
void sum() {
System.out.println("i+j+k: " + (i+j+k));
}
}
class SimpleInheritance {
public static void main(String args[]) {
A superOb = new A();
B subOb = new B();
// The superclass may be used by itself.
superOb.i = 10;
superOb.j = 20;
System.out.println("Contents of superOb: ");
superOb.showij();
System.out.println();
/* The subclass has access to all public members
of its superclass. */
subOb.i = 7;
subOb.j = 8;
subOb.k = 9;
System.out.println("Contents of subOb: ");
subOb.showij();
subOb.showk();
System.out.println();
System.out.println("Sum of i, j and k in subOb:");
subOb.sum();
}
}
The output from this program is shown here:
Contents of superOb:
i and j: 10 20
Contents of subOb:
i and j: 7 8
k: 9
Sum of i, j and k in subOb:
i+j+k: 24
• As you can see, the subclass B includes all of
the members of its superclass, A.
• This is why subOb can access i and j and call
showij( ).
• Also, inside sum( ), i and j can be referred to
directly, as if they were part of B.
• Even though A is a superclass for B, it is also a
completely independent, stand-alone class.
• Being a superclass for a subclass does not
mean that the superclass cannot be used by
itself.
• Further, a subclass can be a superclass for
another subclass.
Member access rules
Member access rules

• Although a subclass includes all of the


members of its superclass, it cannot access
those members of the superclass that have
been declared as private.
• For example, consider the following simple
class hierarchy:
/* In a class hierarchy, private members remain
private to their class.
This program contains an error and will not
compile.
*/
// Create a superclass.
class A {
int i; // public by default
private int j; // private to A
void setij(int x, int y) {
i = x;
j = y;
}
}
// A's j is not accessible here.
class B extends A {
int total;
void sum() {
total = i + j; // ERROR, j is not accessible here
}
}
class Access {
public static void main(String args[]) {
B subOb = new B();
subOb.setij(10, 12);
subOb.sum();
System.out.println("Total is " + subOb.total);
}
}
• REMEMBER A class member that has been
declared as private will remain private to its
class.
• It is not accessible by any code outside its
class, including subclasses.
• A major advantage of inheritance is that once
you have created a superclass that defines the
attributes common to a set of objects, it can
be used to create any number of more specific
subclasses.
• Each subclass can precisely tailor its own
classification.
• Remember, once you have created a
superclass that defines the general aspects of
an object, that superclass can be inherited to
form specialized classes.
• Each subclass simply adds its own unique
attributes.
• This is the essence of inheritance.
LTC:program using inheritance to extend Box

class Box {
double width;
double height;
double depth;
// constructor used when all dimensions
specified
Box(double w, double h, double d) {
width = w;
height = h;
depth = d;
}
// constructor used when no dimensions
specified
Box() {
width = -1; // use -1 to indicate
height = -1; // an uninitialized
depth = -1; // box
}
// compute and return volume
double volume() {
return width * height * depth;
}
}
// Here, Box is extended to include weight.
class BoxWeight extends Box {
double weight; // weight of box
// constructor for BoxWeight
BoxWeight(double w, double h, double d, double m)
{
width = w;
height = h;
depth = d;
weight = m;
}
}
class DemoBoxWeight {
public static void main(String args[]) {
BoxWeight mybox1 = new BoxWeight(10, 20, 15,
34.3);
double vol;
vol = mybox1.volume();
System.out.println("Volume of mybox1 is " + vol);
System.out.println("Weight of mybox1 is " +
mybox1.weight);
}
}

The output from this program is shown here:


Volume of mybox1 is 3000.0
Weight of mybox1 is 34.3
End of session

You might also like