Presentation of Inheritance in Java
Presentation of Inheritance in Java
Group Members
Hamad Zia(Roll #7102)
Saad Ullah Zain (Roll #8374)
Arslan Qadir (Roll #7103)
Ihsan Sadiq (Roll #8457)
Supervised /Co-Supervised By: Mam Farwa Sehar
Table of Content
• WHY PROGRAMMING?
• WHY JAVA?
• Introduction To Inheritance.
• Access Specifiers In Java.
• Types of inheritance in java.
• ‘This’ Keyword.
• ‘Final’ Keyword.
• Advantages and Disadvantages
WHY PROGRAMMING?
WHY JAVA?
• Simple
• Object-Oriented
• Platform Independent
• Secure
• Robust
• Multithreaded
Why use inheritance in java:
• As the name suggests, inheritance means to take something that is already made. It is
one of the most important feature of Object Oriented Programming.
• Inheritance in java is a mechanism in which one class acquires all the properties and
behaviours of another class. It is the concept that is used for reusability purpose.
• Sub Class: The class that inherits properties and behaviours from another class is called
Sub class or Derived Class.
• Super Class: The class whose properties and behaviours are inherited by sub class is
called Base Class or Super class.
The Syntax of Java Inheritance:
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.
Java Inheritance Example:
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);
}
}
Access Specifiers In Java:
• When a subclass is derived simply from it's parent class then this mechanism
is known as simple inheritance.
Single Inheritance Example:
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();
}}
Multilevel Inheritance:
• The process of more than one subclass is derived from a same base class. In
multiple, many-to-one ladder increases.
• Multiple classes are involved in inheritance, but one class extends only one.
Multiple Inheritance:
class A{
void msg(){System.out.println("Hello");}
}
class B{
void msg(){System.out.println("Welcome");}
}
class C extends A,B{//suppose if it were
To reduce the complexity and simplify the language, multiple inheritance is not supported
in java.
Consider a scenario where A, B, and C are three classes. The C class inherits A and B
classes. If A and B classes have the same method and you call it from child class object,
there will be ambiguity to call the method of A or B class.
Since compile-time errors are better than runtime errors, Java renders compile-time error
if you inherit 2 classes. So whether you have same method or different, there will be
compile time error.
Hybrid Inheritance:
• The keyword ’this’ is useful when we need to refer to instance of the class from its
method.
• ‘this’ keyword helps us to avoid name conflicts.
• As we can see in the program that we have declare the name of instance variable and
local variables same.
• The keyword this will reference the current class the word appears in.
• It will allow you to use the classes methods if used like this
this.methodName();
‘Final’ Keyword:
Advantages
• Inheritance promotes reusability. When a class inherits or derives another class, it can
access all the functionality of inherited class.
• Reusability enhanced reliability. The base class code will be already tested and debugged.
• As the existing code is reused, it leads to less development and maintenance costs.
Cont.
Disadvantages
• Inherited functions work slower than normal function as there is indirection.
• Improper use of inheritance may lead to wrong solutions.
• Often, data members in the base class are left unused which may lead to memory
wastage. Inheritance increases the coupling between base class and derived class. A
change in base class will affect all the child classes.