best inheritance ppt in java. everyone can easily understand it.
Size: 103.89 KB
Language: en
Added: Mar 05, 2019
Slides: 15 pages
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..."); } }
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.