Sunteți pe pagina 1din 30

INHERITANCE

By R.M’sendo

CISP,DISP, LIMIS, BSc (Hons) CS, Msc CS


INTRODUCTION TO JAVA PROGRAMMING
Learning Objectives

• 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

• The process by which one class acquires the


properties and functionalities of another class is
called inheritance.
• Inheritance provides the idea of reusability of code
and each sub class defines only those features that
are unique to it, rest of the features can be
inherited from the parent class.

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

• Note: The biggest advantage of Inheritance is that


the code in base class need not be rewritten in the
child class.
• The variables and methods of the base class can be
used in the child class as well.
• Syntax: Inheritance in Java
• To inherit a class we use extends keyword. Here
class A is child class and class B is parent class.

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

• For Method Overriding (so runtime polymorphism


can be achieved).
• For Code Reusability.

8 © Vimachitika
INTRODUCTION TO JAVA PROGRAMMING
Terms used in Inheritence

• Class: A class is a group of objects which have


common properties. It is a template or blueprint
from which objects are created.
• Sub Class/Child Class: Subclass is a class which
inherits the other class. It is also called a derived
class, extended class, or child class
• Super Class/Parent Class: Superclass is the class
from where a subclass inherits the features. It is
also called a base class or a parent class.
9 © Vimachitika
INTRODUCTION TO JAVA PROGRAMMING
• Syntax of Java Inheritance
• Reusability: As the name specifies, reusability is a
mechanism which facilitates you to reuse the fields
and methods of the existing class when you create a
new class. You can use the same fields and methods
already defined in previous class.
• class Subclass-name extends Superclass-name
• {
• //methods and fields
• }
10 © Vimachitika
INTRODUCTION TO JAVA PROGRAMMING

• 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 parent or super class and the new class is
called child or subclass.

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

• On the basis of class, there can be three types of


inheritance in java: single, multilevel and
hierarchical.

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

Hierarchical 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
}}
25 © Vimachitika
INTRODUCTION TO JAVA PROGRAMMING

INDIRECT MECHANISM OF INHERITANCE


• Java Supports a special feature called interface.
• This feature helps to connect a class with more
• than one classes.
• For this type of connectivity java uses ‘implements’
• keyword.

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

public interface Animal {


public void eat();

public void move();

public void sleep();

28 © Vimachitika
INTRODUCTION TO JAVA PROGRAMMING

public class Dog implements Animal {

public void eat() {


System.out.print("Eating...");
}

public void move() {


System.out.print("Moving...");
}

public void sleep() {


System.out.print("Sleeping...");
}
}

29 © Vimachitika
INTRODUCTION TO JAVA PROGRAMMING

CLASSES AND OBECTS

Every Level of SUCCESS require Commitment even


FAILURE!!!!

30 © Vimachitika

S-ar putea să vă placă și