0% found this document useful (0 votes)
475 views

Presentation of Inheritance in Java

The document discusses inheritance in Java. It defines inheritance as a mechanism where a subclass acquires properties and behaviors of a superclass. It describes different types of inheritance like single, multilevel, and hierarchical. It also covers access specifiers, the 'this' keyword, the 'final' keyword, and advantages and disadvantages of inheritance.

Uploaded by

zain 123
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
475 views

Presentation of Inheritance in Java

The document discusses inheritance in Java. It defines inheritance as a mechanism where a subclass acquires properties and behaviors of a superclass. It describes different types of inheritance like single, multilevel, and hierarchical. It also covers access specifiers, the 'this' keyword, the 'final' keyword, and advantages and disadvantages of inheritance.

Uploaded by

zain 123
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 25

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?

• Programming is the term that refers to teaching, instructing or giving


commands to the computer.

WHY JAVA?
• Simple
• Object-Oriented
• Platform Independent
• Secure
• Robust
• Multithreaded
Why use inheritance in java:

• For Method Overriding (so runtime polymorphism can be achieved).


• For Code Reusability.
Introduction To Inheritance:

• 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:

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.
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:

There are four Access Specifiers in Java


1. Public: When a member of a class is declared as public specifier, it can be
accessed from any code.
2. Protected: Protected is only applicable in case of Inheritance. When a member of
a class is declared as protected, it can only be accessed by the members of its class
or subclass.
3. Private: A member of a class is declared as private specifier, can only be accessed
by the member of its class.
4. Default: When you don't specify a access specifier to a member, Java automatically
specifies a default. And the members modified by default can only be accessed by
any other code in the package, but can't be accessed outside of a package.
Types of inheritance in java:

• On the basis of class, there can be three types of inheritance in java:


I. Single
II. multilevel and
III. hierarchical.

• In java programming, multiple and hybrid inheritance is supported through interface


only.
Simple Inheritance

• 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 a subclass is derived from a derived class.


• In multilevel, one-to-one ladder increases.
• Multiple classes are involved in inheritance, but one class extends only one.
• The lowermost subclass can make use of all its super classes' members.
Multilevel Inheritance Example:
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();
}}
Hierarchical Inheritance:

• In hierarchical type of inheritance, one class is extended by many subclasses.


• It is one-to-many relationship
Hierarchal Inheritance Example:
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
}}
Multiple 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

public static void main(String args[]){


C obj=new C();
obj.msg();//Now which msg() method would be invoked?
}
}
Why multiple inheritance is not supported in java?

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:

Hybrid Inheritance is the combination of multiple, hierarchical inheritance.


‘This’ Keyword:

• 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:

• It can also be called as Restricting Inheritance.


• Classes that cant be extended are called final classes.
• We use the final modifier in the definition of the class to indicate this.

final class myclass


{
// Insert code here
}
Advantages And Disadvantages

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.

You might also like