Java Inheritance
Java Inheritance
• The derived class is also called subclass and the base class is
also known as super-class.
• The derived class can add its own additional variables and
methods.
Inheritance
Syntax:
public class ChildClass extends BaseClass {
// derived class methods extend and possibly override
}
Example
// A subclass which extends for vehicle
// A class to display the class Car extends Vehicle {
attributes of the vehicle int CC;
class Vehicle { int gears;
void attributescar() {
String color;
// The subclass refers to the
int speed; members of the superclass
int size; System.out.println("Color of Car : " +
void attributes() { color);
System.out.println("Speed of Car : "
System.out.println("Color + speed);
: " + color);
System.out.println("Size of Car : " +
System.out.println("Speed size);
: " + speed); System.out.println("CC of Car : " +
System.out.println("Size : CC);
" + size); System.out.println("No of gears of
} Car : " + gears);
}
} }
public class Test {
public static void main(String args[]) {
Car b1 = new Car();
b1.color = "Blue"; The output is
b1.speed = 200 ;
b1.size = 22; Color of Car : Blue
b1.CC = 1000; Speed of Car : 200
b1.gears = 5; Size of Car : 22
CC of Car : 1000
b1.attributescar();
No of gears of Car : 5
}
}
class Teacher Output:
{
String designation = "Teacher"; XIE
String collegeName = “XIE"; Teacher
void does() Physics
{ Teaching
System.out.println("Teaching");
}
}
• The derived class cannot inherit a member of the base class if the
derived class declares another member with the same name.
Example of member access in Inheritance
Constructors and Inheritance
Output:
class ParentClass{
//Parent class constructor Constructor of Parent
ParentClass(){ Constructor of Child
System.out.println("Constructor of Parent");
}
}
class JavaExample extends ParentClass{
JavaExample(){
/* It by default invokes the constructor of parent class
* You can use super() to call the constructor of parent.
* It should be the first statement in the child class
* constructor, you can also call the parameterized constructor
* of parent class by using super like this: super(10), now
* this will invoke the parameterized constructor of int arg
*/
System.out.println("Constructor of Child");
}
public static void main(String args[]) {
//Creating the object of child class
new JavaExample();
}
}
Types of Inheritance
• Single inheritance
• Multilevel inheritance
• Multiple inheritance
• Hybrid inheritance
• Hierarchical inheritance
Single Inheritance
• Single inheritance is easy to understand.
• When a class extends another one class only then we call it
a single inheritance.
• Here A is a parent class of B and B would be a child class of
A.
Single Inheritance example program in Java
Class A
{
public void methodA()
{
System.out.println("Base class method");
}}
Class B extends A
{
public void methodB()
{
System.out.println("Child class method");
}
public static void main(String args[])
{
B obj = new B();
obj.methodA(); //calling super class method
obj.methodB(); //calling local method
}
}
o/p: Base class method
Child class method
Multiple Inheritance
class Car{
public Car()
{
System.out.println("Class Car");
}
public void vehicleType()
{
System.out.println("Vehicle Type: Car");
}
}
class Maruti extends Car{
public Maruti() public class Maruti800 extends Maruti{
{
System.out.println("Class public Maruti800()
Maruti"); {
}
System.out.println("Maruti Model: 800");
public void brand()
}
{ public void speed()
System.out.println("Brand: {
Maruti");
} System.out.println("Max:
public void speed() 80Kmph");
}
{
System.out.println("Max:
90Kmph");
}
}
public static void main(String args[])
{
Maruti800 obj=new Maruti800();
obj.vehicleType();
obj.brand();
obj.speed();
}
}
Output:
Class Car
Class Maruti
Maruti Model: 800
Vehicle Type: Car
Brand: Maruti
Max: 80Kmph
Hierarchical Inheritance
Example:
• One of the simplest example- Boy class
extends Human class.
• Both the classes have a common method void eat().
• Boy class is giving its own implementation to
the eat() method or in other words it is overriding the
method eat().
class Human{
public void eat()
{
System.out.println("Human is eating");
}
}
class Boy extends Human{
public void eat(){
System.out.println("Boy is eating");
}
public static void main( String args[]) {
Boy obj = new Boy();
Output:
obj.eat();
Boy is eating
}
}
Rules of method overriding in Java
• Argument list:
• Access Modifier:
✓ The Access Modifier of the overriding method (method of
subclass) cannot be more restrictive than the overridden
method of parent class.
class MyBaseClass{
protected void disp() {
System.out.println("Parent class method");
Output:
}
}
Child class
class MyChildClass extends MyBaseClass{ method
public void disp(){
System.out.println("Child class method");
}
public static void main( String args[]) {
MyChildClass obj = new MyChildClass();
obj.disp();
}}
Super keyword in Overriding
class B extends A
{
// overriding m1()
void m1()
{
System.out.println("Inside B's m1 method");
}
}
Dynamic Method Dispatch or (Runtime
Polymorphism in Java) example
class C extends A
{
// overriding m1()
void m1()
{
System.out.println("Inside C's m1 method");
}
}
// Driver class
class Dispatch
{
public static void main(String args[])
{
// object of type A
A a = new A();
// object of type B
B b = new B();
// object of type C
C c = new C();
Dynamic Method Dispatch or (Runtime
Polymorphism in Java) example
// obtain a reference of type A
A ref;
OUTPUT :
Q.8 Create a class Book and define a display method to display book
information. Inherit Reference_Book and Magazine classes from Book
class and override display method of Book class in Reference_Book and
Magazine classes. Make necessary assumptions required
Assignment 3: Q.2
Q.9 A university has two types of students — graduate students and research
students. The University maintains the record of the name, age and
programme of every student. For graduate students, additional information
like percentage of marks and stream, like science, commerce, etc. is recorded;
whereas for research students, additionally, specialization and years of
working experience, if any, is recorded. Each class has a constructor. The
constructor of subclasses makes a call to the constructor of the superclass.
Assume that every constructor has the same number of parameters as the
number of instance variables. In addition, every subclass has a method that
may update the instance variable values of that subclass. All the classes have a
function display_student_info( ), the subclasses must override this method of
the base class. Every student is either a graduate student or a research
student.
Perform the following tasks for the description given above using Java :
(i) Create the three classes with proper instance variables and methods,
with suitable inheritance.
(ii) Create at least one parameterised constructor for each class.
(iii) Implement the display_student_info( ) method in each class