Week 06 Lab Inheritance
Week 06 Lab Inheritance
Week 06 Lab Inheritance
Inheritance in Java
Inheritance in Java is a mechanism in which one object acquires all the properties
and behaviors of a parent object. It is an important part of OOPs (Object Oriented
programming system).
The idea behind inheritance in Java is that you can create new classes that are built
upon existing classes. When you inherit from an existing class, you can reuse
methods and fields of the parent class. Moreover, you can add new methods and
fields in your current class also.
class Subclass-name extends Superclass-name
{
//methods and fields
}
The extends keyword indicates that you are making a new class that derives from
an existing class. The meaning of "extends" is to increase the functionality.In the
terminology of Java, a class which is inherited is called a parent or superclass, and
the new class is called child or subclass.
As displayed in the above figure, Programmer is the subclass and Employee is the
superclass. The relationship between the two classes is Programmer IS-A
Employee. It means that Programmer is a type of Employee.
Example 01
class Employee{
float salary=40000;
}
class Programmer extends Employee{
int bonus=10000;
public static void main(String args[]){
Programmer p=new Programmer();
System.out.println("Programmer salary is:"+p.salary);
System.out.println("Bonus of Programmer is:"+p.bonus);
}
}
Output:
In the above example, Programmer object can access the field of own class as well
as of Employee class i.e. code reusability.
Types of inheritance in java
On the basis of class, there can be three types of inheritance in java: single,
multilevel and hierarchical. In java programming, multiple and hybrid inheritance is
supported through interface only. We will learn about interfaces later.
When one class inherits multiple classes, it is known as multiple inheritance. For
Example:
Single Inheritance Example
When a class inherits another class, it is known as a single inheritance. In the
example given below, Dog class inherits the Animal class, so there is the single
inheritance.
Example 02
class Animal{
void eat(){System.out.println("eating...");}
}
class Dog extends Animal{
void bark(){System.out.println("barking...");}
}
class TestInheritance{
public static void main(String args[]){
Dog d=new Dog();
d.bark();
d.eat();
}}
Output:
Multilevel Inheritance Example
Example 03
class Animal{
void eat(){System.out.println("eating...");}
}
class Dog extends Animal{
void bark(){System.out.println("barking...");}
}
class BabyDog extends Dog{
void weep(){System.out.println("weeping...");}
}
class TestInheritance2{
public static void main(String args[]){
BabyDog d=new BabyDog();
d.weep();
d.bark();
d.eat();
}}
Output:
Hierarchical Inheritance Example
When two or more classes inherits a single class, it is known as hierarchical
inheritance
Example 04
class Animal{
void eat(){System.out.println("eating...");}
}
class Dog extends Animal{
void bark(){System.out.println("barking...");}
}
class Cat extends Animal{
void meow(){System.out.println("meowing...");}
}
class TestInheritance3{
public static void main(String args[]){
Cat c=new Cat();
c.meow();
c.eat();
//c.bark();//C.T.Error
}}
Output:
If a class inherits a method from its superclass, then there is a chance to override the
method provided that it is not marked final.
The benefit of overriding is: ability to define a behavior that's specific to the
subclass type, which means a subclass can implement a parent class method based on
its requirement.
In object-oriented terms, overriding means to override the functionality of an existing
method.
Example 05
class Animal {
public void move() {
System.out.println("Animals can move");
}
}
Output:
Rules for Method Overriding
The argument list should be exactly the same as that of the overridden method.
The return type should be the same or a subtype of the return type declared in
the original overridden method in the superclass.
The access level cannot be more restrictive than the overridden method's
access level. For example: If the superclass method is declared public then the
overriding method in the sub class cannot be either private or protected.
Instance methods can be overridden only if they are inherited by the subclass.
A method declared final cannot be overridden.
A method declared static cannot be overridden but can be re-declared.
If a method cannot be inherited, then it cannot be overridden.
A subclass within the same package as the instance's superclass can override
any superclass method that is not declared private or final.
A subclass in a different package can only override the non-final methods
declared public or protected.
Constructors cannot be overridden.
Example 06
class Animal {
public void move() {
System.out.println("Animals can move");
}
}
Exercise:
1. Write a program to create a class named shape. In this class we have three Sub classes circle,
triangle and square each class has two member function named draw () and erase (). Create
these using polymorphism concepts.
2. Write a Java Program to elaborate Constructors of subclass and superclass.
3. Create a class called Employee whose objects are records for an employee. This class
will be a derived class of the class Person . An employee record has an employee's name
(inherited from the class Person), an annual salary represented as a single value of
type double, a year the employee started work as a single value of type int and a national
insurance number, which is a value of type String.
Your class should have a reasonable number of constructors and accessor methods, as
well as an equal’s method. Write another class containing a main method to fully test
your class definition.
4. Write a java program that elaborates the concept of method overloading.
5. Write your own java program as example of Hierarchical Inheritance