Inheritance in OOPs with java

aakankshabarjatya 602 views 21 slides Sep 08, 2020
Slide 1
Slide 1 of 21
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
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21

About This Presentation

Inheritance in java introduces the concept of reusability by implementing a mechanism in which one object acquires all the properties and behaviors of the parent object.


Inheritance in Java is a mechanism in which one object acquires all the properties and behaviors of a parent object. It is an im...


Slide Content

OOPs with JAVA Inheritance in JAVA

Table of content What is Inheritance ? Why it is used ? Declaration? Basic program. Inheritance 01 Types of Inheritance. Basic syntax and declarations Inheritance type 02 Inheritnace Importance 03 What we have discussed so far, some problem statements Summary 04 Basic program and implementations

Introduction Name : AAKA N KSHA JAI N Qualification : B. Tech , M. Tech Designation : Assistant Professor Research Area : Machine Learning and Data Mining Experience : 2+Yrs. E-mail Id : aakanksha.jain @poornima.edu.in Other : 30+ certification (10+ MOOC course , 7 FDP ) Research Work : 5 Research Paper in area of Data mining and Machine Learning and 2 Chapters in book “ Cognitive Machine Learning with Concept of Distributive Parallel Computing “ by Eureka Publications.

Inheritance in JAVA What you can define as a Inheritance?

What is Inheritance? I nheritance in java introduce the concept of reusability by implementing a mechanism in which one object acquires all the properties and behaviors of parent object. Inheritance in Java is a mechanism in which one object acquires all the properties and behaviors of a parent object. It is an important part of OOPs (Object Oriented programming system). 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 the parent class. Moreover, you can add new methods and fields in your current class also. Inheritance represents the IS-A relationship which is also known as a parent-child relationship.

What is Inheritance? Java Inheritance is one of the foundations of object-oriented programming as it provides the creation of hierarchical classifications. Using inheritance, you can create a general class that defines traits common to a set of related items. This class can then be inherited by other, more specific classes, each adding those things that are unique to it. • In the specification of Java, a class that is inherited is called superclass. The class that does the inheriting is called a subclass. • Accordingly, a subclass is a specialised version of a superclass. It inherits all of the instance variables and methods defined by the superclass and adds its own, unique elements

Types of Inheritance Types of Inheritance There are mainly four kinds of inheritance in java : • Single inheritance • Multi-level inheritance • Hierarchical inheritance • Hybrid inheritance

Types of Inheritance Single Level Inheritance: In this type of Single level inheritance, there is single base class and a single obtained class, i.e., A support mobile features is extended by I-Phone brand.

Types of Inheritance Multi-level Inheritance In this type of Multilevel inheritance, there is more than one single level of derivation. i.e. - After i-phone brand extends base features. Now i-phone the brand has manufactured its new model with new added features or its advanced OS

Types of Inheritance Hierarchical Inheritance I n Hierarchical Inheritance, multiple derived class would be extended from the base class; it’s similar to single level inheritance but this time along with i-phone, Samsung is also taking part in inheritance.

Types of Inheritance Hybrid Inheritance Hybrid Inheritance is a group/combination of Single, Multilevel, & hierarchal inheritance all together constructs a hybrid inheritance.

Basic syntax 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.

Importance of Inheritance in Java It is an essential concept that can make the difference between a real programming language and a great programming l anguage. If the code is written, the subroutine should be able to return a function. When this is performed, the argument can be raised to a higher kth power. Once this has occurred, it can be done in some various methods.

Importance of Inheritance in Java Example for Inheritance Importance: When two arguments are taken, one can be utilised as a function. It can be applied to different cases many times over, and the result will stay the same. It will be calculated by using a mathematical notion. It can also be used to implement some various systems, and it can help you to find solutions to queries that deal with redundancy. It may also be possible for elements or variables to have a third variety of the true or false get-together. It could be used as an additional process to decide when the nesting process should be rejected. When it is observed in this composition, it is the same as being in the "while loop" that is often displayed in procedural programming languages

Basic terminologies 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. 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 the previous class.

Basic Code 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); } } OUTPUT: Element at index 0 : 10 Element at index 1 : 20 Element at index 2 : 30 Element at index 3 : 40 Element at index 4 : 50

Sample code 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(); }}

Use case Inheritance With Some Real World Scenarios • Object Oriented Programming is considering as a design methodology for creating a non-rigid application. In OOPS, every logic is written to get our work done, but this is done based on an entity which we call it as Objects. OOP allow us to decompose our problem into a small unit of work which is located by an Objects. We create function around this objects. • Inheritance in the main feature of OOPS, because inheritance implements in real world scenario all the time. Inheritance is the main feature of OOPS because inheritance performs in real world scenario itself all the time.

Use case It is the essential functionality for which it was invented were Calling & eceiving a call & Messaging when we take a mobile as an object. But nowadays thousands of new features & models were added & the count is still increasing. In figure, each brand (Samsung, Nokia, BlackBerry) have their list of features along with the core functionality of dialing, receiving a call & messaging. Considering the above example, the figure shows what inheritance is. Primary Mobile feature is to Send Message, dial & receive the call and so on. So the brands of mobile are accepting this basic functionality by extending the mobile type functionality and computing their new features to their individual brand

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

Thank You Any Question?