inheritance

JayPrajapati38 259 views 15 slides Mar 05, 2019
Slide 1
Slide 1 of 15
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15

About This Presentation

best inheritance ppt in java. everyone can easily understand it.


Slide Content

Inheritance in Java JAY PRAJAPATI ENROLL NO:161350116044 VRUSHANK PATEL ENROLL NO:161350116044

CONTENTS What is inheritance Why use inheritance in java Syntax of Java Inheritance TYPES OF INHERITANCE Why multiple inheritance is not supported in java? Advantages of Inheritance Disadvantages of Inheritance

What is inheritance ???? Inheritance in java is a mechanism in which one object acquires all the properties andbehaviors ofparentobject . 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 addnew methods andfields also. Inheritance represents the IS-A relationship, also known as parentchildrelationship .

Why use inheritance in java ▪ For Method Overriding (so runtime polymorphism can be achieved). ▪ For Code Reusability.

Syntax of Java Inheritance classSubclass - nameextendsSuperclass -name { // methodsandfields } 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.

TYPES OF INHERITANCE • On the basis of class, there can be mainly three types of inheritance in java: 1. Single inheritance 2. Multiline inheritance 3. Hierarchical inheritance 4. Multiple inheritance 5. Hybrid inheritance 1,2,3 Can be implement by using class and object 4,5 Can be implement using interface

SINGLE INHERITANCE classAnimal { void eat() { System.out.println ("eating..."); } } class Dog extends Animal { OUTPUT void bark() { barking... eating... System.out.println ("barking..."); } } class TestInheritance { public static void main(String args [ ]) { Dog d=new Dog(); d.bark (); d.eat (); } }

MULTILEVEL INHERITANCE classAnimal { 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..."); } }

MULTILEVEL INHERITANCE class TestInheritance2 { public static void main(String args [ ]) { BabyDog d=new BabyDog (); d.weep (); d.bark (); d.eat (); } } OUTPUT weeping... barking... eating...

HIERARCHICAL INHERITANCE 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..."); } }

HIERARCHICAL INHERITANCE class TestInheritance3 { public static void main(String args [ ]) { Cat c=new Cat(); Dog d=new Dog(); c.meow (); c.eat (); d.bark (); d.eat (); } } OUTPUT meowing... eating... barking… eating…

Why multiple inheritance is not supported in java? To reduce the complexity and simplify the language, multiple inheritance is not supportedin java. class A { void msg (){ System.out.println ("Hello");} } class B { void msg (){ System.out.println ("Welcome");} } class C extends A,B { Public Static void main(String args []) { C obj =new C(); obj.msg(); //Now which msg () method would be invoked? } }

Advantages of Inheritance ● Reusability -- facility to use public methods of base class without rewriting the same ● Extensibility -- extending the base class logic as per business logic of the derived class ● Data hiding -- base class can decide to keep some data private so that it cannot be altered by the derived class ● Overriding -- With inheritance, we will be able to override the methods of the base class so that meaningful implementation of the base class method can be designed in the derived class.

Disadvantages of Inheritance • Unsupported- Java not support Multiple inheritance as well as Hybrid inheritance. • Permits only one class- The extends keyword permits to connect a class with only one class. • Undefined- In Interface, properties are only declared and assigned, but never defined. • Re-factor- If a method is deleted in the "super class" or aggregate, then we will have to re-factor in case of using that method.

Thank you !!
Tags