Академический Документы
Профессиональный Документы
Культура Документы
By R.M’sendo
• Introduction to Inheritance
• What is Class
• Structure of a Class
• Overview of Objects
• The ‘’New Operator” and Dot Operator
• Methods
• Constructors
2 © Vimachitika
INTRODUCTION TO JAVA PROGRAMMING
INHERITANCE
3 © Vimachitika
INTRODUCTION TO JAVA PROGRAMMING
• Inheritance is a process of defining a new class based on
an existing class by extending its common data members
and methods.
• Inheritance in java is a mechanism in which one object
acquires all the properties and behaviors of parent object.
It is an important part of OPPs(Object Oriented
programming system).
• Inheritance allows us to reuse of code, it improves
reusability in your java application.
• The parent class is called the base class or super class. The
child class that extends the base class is called the derived
class or sub class or child class.
4 © Vimachitika
INTRODUCTION TO JAVA PROGRAMMING
5 © Vimachitika
INTRODUCTION TO JAVA PROGRAMMING
• 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 parent class, and you
can add new methods and fields also.
6 © Vimachitika
INTRODUCTION TO JAVA PROGRAMMING
• Inheritance represents the IS-A relationship, also
known as parent-child relationship.
• This saves work, because the more specialized
class inherits all the properties of the general class
and you, the programmer, need only program the
new features.
7 © Vimachitika
INTRODUCTION TO JAVA PROGRAMMING
Why use inheritance in java
8 © Vimachitika
INTRODUCTION TO JAVA PROGRAMMING
Terms used in Inheritence
11 © Vimachitika
INTRODUCTION TO JAVA PROGRAMMING
12 © Vimachitika
INTRODUCTION TO JAVA PROGRAMMING
• As displayed in the above figure, Programmer is the
subclass and Employee is the superclass.
• Relationship between two classes is Programmer IS-
A Employee.
• It means that Programmer is a type of Employee.
13 © Vimachitika
INTRODUCTION TO JAVA PROGRAMMING
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);
}
}
14 © Vimachitika
INTRODUCTION TO JAVA PROGRAMMING
Secure
• We don’t have pointers and we cannot access out
of bound arrays (you get Array Index Out Of
Bounds Exception if you try to do so) in java.
• That’s why several security flaws like stack
corruption or buffer overflow is impossible to
exploit in Java.
15 © Vimachitika
INTRODUCTION TO JAVA PROGRAMMING
Types of inheritance in java
16 © Vimachitika
INTRODUCTION TO JAVA PROGRAMMING
17 © Vimachitika
INTRODUCTION TO JAVA PROGRAMMING
• Single Inheritance
• A structure having one and only one parent as well
• as child class.
• Child class is authorized to access the property of
• Parent class.
18 © Vimachitika
INTRODUCTION TO JAVA PROGRAMMING
• 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();
}} 19 © Vimachitika
INTRODUCTION TO JAVA PROGRAMMING
• Multilevel Inheritance
• Standard structure of Single Inheritance having
one Parent, one or more intermediate and one
child classes.
• Child class as well as intermediate class may access
the properties of upper level classes.
20 © Vimachitika
INTRODUCTION TO JAVA PROGRAMMING
21 © Vimachitika
INTRODUCTION TO JAVA PROGRAMMING
• 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();
}}
22 © Vimachitika
INTRODUCTION TO JAVA PROGRAMMING
• Hierarchical Inheritance
• A structure having one parent and more child
class.
• Child classes must be connected with only Parent
class.
23 © Vimachitika
INTRODUCTION TO JAVA PROGRAMMING
24 © Vimachitika
INTRODUCTION TO JAVA PROGRAMMING
26 © Vimachitika
INTRODUCTION TO JAVA PROGRAMMING
• Syntax :
• interface A{
• ……..}
• Interface B {
• ____}
• class M {
• -------}
• class N implements A,B extends M{
• =====
• _____------…………}
27 © Vimachitika
INTRODUCTION TO JAVA PROGRAMMING
28 © Vimachitika
INTRODUCTION TO JAVA PROGRAMMING
29 © Vimachitika
INTRODUCTION TO JAVA PROGRAMMING
30 © Vimachitika