Download as pdf or txt
Download as pdf or txt
You are on page 1of 9

INHERITANCE

Inheritance is the ability of a class to derive something specific from a generic class.
The generic class is called super class or base class or parent class and the specific
class is known as child class or subclass or derived class.
There are two types of inheritance:
Single-level inheritance, where a child class inherits from its parent class
Multilevel inheritance, where a class inherits from its parent, this parent in turn inherits
from its parent and so on

For example class LibraryBook is a child class of a super class Book. Here, we say
LibraryBook inherits states and behaviours of objects of Book class such as isbn,
author, title and many others. Apart from deriving from Base class, Library-Book can
have its own data members such as number of copies and member functions
representing states and behaviours of objects
public class LibraryBook extends Book
{
int nCopies = 10; // number of copies of the book
public void show()
{
System.out.println(“isbn = ” + isbn + “\nauthor = ” + author + “\ntitle = ” +
title + “\ncopies = ” + nCopies);
}
public static void main(String[] args)
{
LibraryBook lb = new LibraryBook();
lb.show();
}
}

Output
isbn = 1234
author = Rex
title = Hello Java
copies = 10
The extends keyword causes a subclass to inherit all data members and member
functions declared in a nonfinal super class including the super class’s super classes.
The subclass can access all inherited nonprivate data members and member functions;
it cannot access any inherited private data members and member functions. In
inheritance, objects are instantiated starting from base class down in the class hierarchy

CALLING BASE CLASS CONSTRUCTOR

You already know that parameterized constructors have to be called manually by your
program. This is also applicable to inheritance. Suppose, a super class has a
parameterized constructor, then it is the duty of the child class constructor to call the
super class’s constructor first before executing any other Java statements inside its
constructor, otherwise JVM will generate a compile time error.

Child class constructor will use super() method along with the required argument values
to call super class constructor. Finally, keep in mind that any other member functions
apart from constructor cannot call super class’s constructor.
It is obvious that super class’s object will be constructed first before child class’s object
construction. Also, single inheritance can be extended to multiple levels of classes, one
class above another and is called multilevel inheritance

//OODog.java
class Dog
{
String name;
String colour;
String diet;
public Dog(String name1, String colour1, String diet1)
{
name = name1;
colour = colour1;
diet = diet1;
}
public Dog(String colour1)
{
colour = colour1;
}
public void eat(String food)
{
diet = food;
}
public void bark()
{
System.out.println(“Dog ” + name + “ can bark: Lol Lol Lol”);
}
public void showDog()
{
System.out.println(“Dog name: ” + name);
System.out.println(“Dog colour: ” + colour);
System.out.println(“Dog can eat food: ” + diet);
}
}
class PetDog extends Dog
{
String owner;
public PetDog(String name, String colour, String diet, String owner1)
{
// super() should be the first statement
super(name, colour, diet);
owner = owner1;
}
public void showLove()
{
System.out.println(“Dog ” + name + “ can express love: Ich Ich Ich”);
}
}
class StrayDog extends Dog
{
public StrayDog(String colour)
{
// super() should be the first statement
// stray dog has only colour, and call overloaded constructor
super(colour);
}
public void bite()
{
System.out.println(“Beware: This stray Dog will bite”);
}
}
public class OODog
{
public static void main(String[] args)
{
PetDog petDog = new PetDog(“Tommy”, “white”, “meat”, “Rajkumar”);
petDog.showDog();
petDog.showLove();
StrayDog strayDog = new StrayDog(“black”);
strayDog.showDog();
strayDog.bite();
}
}

Output
Dog name: Tommy
Dog colour: white
Dog can eat food: meat
Dog Tommy can express love
Dog name: null
Dog colour: black
Dog can eat food: null
Beware: This stray Dog will bite

CALLING BASE CLASS DATA MEMBER WITH SUPER


Suppose we have a data member whose name is same in base class and child class. In
this case, the child class can access the base class data member by using the keyword
super.i; // assume i appears both in base class and child class

OVERRIDING AND POLYMORPHISM


A child class member function can override or redefine a base class member function
with same signature of the base class member function. That is, same name, same
return type and same argument list can be given to both methods. Remember always
that child class method can override, only if base class method is not private, static and
final

class Instrument
{
String colour;
public void tune() { };
}
class Guitar extends Instrument
{
public void tune()
{
System.out.println(“Guitar sound: Ting Ting Ting...”);
}
}
class Keyboard extends Instrument
{
public void tune()
{
System.out.println(“Keyboard sound: Pam Pam Pam...”);
}
}
class Sax extends Instrument
{
public void tune()
{
System.out.println(“Sax sound: Bham Bham Bham...”);
}
}
public class InstrumentTest
{
public static void main(String[] args)
{
Instrument[] instrument = new Instrument[3];
instrument[0] = new Guitar(); // normal up casting
instrument[1] = new Keyboard();
instrument[2] = new Sax();
for(int i = 0; i < 3; i++)
instrument[i].tune();
}
}

Output
Guitar sound: Ting Ting Ting...
Keyboard sound: Pam Pam Pam...
Sax sound: Bham Bham Bham...

In this application, child class objects are assigned to base class reference by normal
upcasting which is something JVM will identify at compile time. During runtime, JVM
correctly identifies the appropriate child class version of the tune() method and invokes
it. It does not complain to us that we did not downcast.
Rather with a feature named runtime type identification (RTTI), it invokes appropriate
tune() method from any of the child class. This behaviour is known as polymorphism.
Suppose if there is a variation in the signature, say for instance, different arguments list,
then JVM will consider these two methods as overloaded instead of overridden
methods. So, as overloading it will invoke either a base class method or child class
method depending on the input values.
One final note about overriding. A child class method, defined in another package other
than the package where base class is defined, can override the base class method
which is only public or protected. It cannot override private or default access methods.
Before leaving this topic, let us consider the keyword protected. If you declare a super
class field or method as protected, any method, declared in any class that subclasses
the class that declares the protected field or method, can access that field or method.
CHECKING OBJECT’S CLASS TYPE

instanceof operator checks whether the given object is of the specified instance type of
a class

object-name instanceof class-name

PREVENTING INHERITANCE AND OVERRIDING


A class can restrict its subclasses to inherit data members and member functions by
declaring final. In other words, final classes cannot be subclassed. Similarly, a base
class can restrict its subclass method to override its base class counterpart by declaring
it final method. In other words, a final method cannot be overridden inside child class.
Further, if a class is final, then all of its methods are also final implicitly.
A compile time error will be thrown when an attempt is made to subclass a base class
or override a base class method.
Recollect, earlier we have defined final variables, which allows us to create Java
constants

VISIBILITY OF MEMBERS OF A CLASS

You might also like