Inheritance in Java.

Slides:



Advertisements
Similar presentations
More on Classes Inheritance and Polymorphism
Advertisements

METHOD OVERRIDING 1.Sub class can override the methods defined by the super class. 2.Overridden Methods in the sub classes should have same name, same.
Inheritance Inheritance Reserved word protected Reserved word super
1 Lecture 3 Inheritance. 2 A class that is inherited is called superclass The class that inherits is called subclass A subclass is a specialized version.
Aalborg Media Lab 23-Jun-15 Inheritance Lecture 10 Chapter 8.
Inheritance and Polymorphism Recitation – 10/(16,17)/2008 CS 180 Department of Computer Science, Purdue University.
© 2006 Pearson Addison-Wesley. All rights reserved9 A-1 Chapter 9 Advanced Java Topics CS102 Sections 51 and 52 Marc Smith and Jim Ten Eyck Spring 2007.
Inheritance. © 2004 Pearson Addison-Wesley. All rights reserved 8-2 Inheritance Inheritance is a fundamental object-oriented design technique used to.
C++ Object Oriented 1. Class and Object The main purpose of C++ programming is to add object orientation to the C programming language and classes are.
Features of Object Oriented Programming Lec.4. ABSTRACTION AND ENCAPSULATION Computer programs can be very complex, perhaps the most complicated artifact.
Inheritance. Introduction Inheritance is one of the cornerstones of object-oriented programming because it allows the creation of hierarchical classifications.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
Inheritance and Access Control CS 162 (Summer 2009)
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
Programming With Java ICS201 University Of Ha’il1 Chapter 7 Inheritance.
Chapter 10: Introduction to Inheritance. Objectives Learn about the concept of inheritance Extend classes Override superclass methods Call constructors.
1 COSC2007 Data Structures II Chapter 9 Class Relationships.
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 9.1 – 9.4.
Method Overriding Remember inheritance: when a child class inherits methods, variables, etc from a parent class. Example: public class Dictionary extends.
Inheritance Inheritance is the process of extending the functionality of a class by defining a new class that inherit,all the features of extending class.
Classes, Interfaces and Packages
CSI 3125, Preliminaries, page 1 Inheritance. CSI 3125, Preliminaries, page 2 Inheritance Using inheritance, can create a general class that defines traits.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Java Programming Fifth Edition Chapter 9 Introduction to Inheritance.
Class Inheritance Part II: Overriding and Polymorphism Corresponds with Chapter 10.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Modern Programming Tools And Techniques-I
Inheritance Chapter 7 Inheritance Basics Programming with Inheritance
Programming in Java, 2e Sachin Malhotra Saurabh Choudhary.
Inheritance in Java Inheritance in java is a mechanism in which one object acquires all the properties and behaviors of parent object. The idea behind.
Chapter 11 Inheritance and Polymorphism
Inheritance and Polymorphism
PRINCIPALES OF OBJECT ORIENTED PROGRAMMING
Road Map Inheritance Class hierarchy Overriding methods Constructors
Week 8 Lecture -3 Inheritance and Polymorphism
Inheritance Chapter 7 Inheritance Basics Programming with Inheritance
Inheritance "Question: What is the object oriented way of getting rich? Answer: Inheritance.“ “Inheritance is new code that reuses old code. Polymorphism.
Object Oriented Programming
Designing for Inheritance
Modern Programming Tools And Techniques-I Inheritance
CSC 143 Inheritance.
Chapter 9 Inheritance and Polymorphism
Inheritance Basics Programming with Inheritance
CISC124 Assignment 4 on Inheritance due next Monday, the 12th at 7pm.
The super Reference Constructors cannot be used in child classes, even though they have public visibility Yet we often want to use the parent's constructor.
Java Programming Language
Inheritance, Polymorphism, and Interfaces. Oh My
Chapter 9: Polymorphism and Inheritance
Advanced Java Topics Chapter 9
Computer Programming with JAVA
Java – Inheritance.
Inheritance Inheritance is a fundamental Object Oriented concept
9: POLYMORPHISM Programming Technique II (SCSJ1023) Jumail Bin Taliba
Pointers Dr. Bhargavi Goswami Department of Computer Science
Inheritance Cse 3rd year.
Java Programming, Second Edition
Inheritance Chapter 7 Inheritance Basics Programming with Inheritance
Java Inheritance.
Inheritance Chapter 7 Inheritance Basics Programming with Inheritance
Inheritance.
Inheritance and Polymorphism
Review of Previous Lesson
Chapter 11 Inheritance and Polymorphism Part 1
Final and Abstract Classes
C++ Object Oriented 1.
Programming in C# CHAPTER 5 & 6
Computer Science II for Majors
Presentation transcript:

Inheritance in Java

The term inheritance implies that one class can inherit a part or all of its structure and behavior from another class. Inheritance provides the idea of reusability, i.e., a code once written can be used again and again in a number of new classes. The class that does the inheriting is called a subclass of the class from which it inherits. P.Ray@HIT-CSE,2012

If class B is a subclass of class A, it can also be said that class A is a super class of class B. (Sometimes the terms derived class and base class are used instead of subclass and super class) P.Ray@HIT-CSE,2012

A subclass can add to the structure and behavior that it inherits. It can also replace or modify inherited behavior (though not inherited structure). The relationship between a subclass and a super class can be shown in a diagram with the subclass below, and connected to, its super class. Class B extends A { //functions and fields } P.Ray@HIT-CSE,2012

P.Ray@HIT-CSE,2012

P.Ray@HIT-CSE,2012

P.Ray@HIT-CSE,2012

P.Ray@HIT-CSE,2012

P.Ray@HIT-CSE,2012

P.Ray@HIT-CSE,2012

P.Ray@HIT-CSE,2012

P.Ray@HIT-CSE,2012

P.Ray@HIT-CSE,2012

TYPES OF INHERITANCE Inheritance is generally of five types : single level, multilevel, multiple, hierarchical and hybrid. P.Ray@HIT-CSE,2012

Single-level Inheritance In single level inheritance, there is just one base and one derived class. It is represented as follows: Diagram P.Ray@HIT-CSE,2012

Multilevel Inheritance In multilevel inheritance, there is one base class and one derived class at the same time at one level. At the next level, the derived class becomes base class for the next derived class and so on. This is shown as: P.Ray@HIT-CSE,2012

P.Ray@HIT-CSE,2012

The class A and class B together form one level, class B and class C together form another level and so on. For class B, class A is the parent and for class C, class B is the parent; thus in this inheritance level, A can be called the grandparent of class C, and class C the grandchild of class A. P.Ray@HIT-CSE,2012

Multiple Inheritance In a multiple inheritance, a child can have more than one parent, i.e., a child can inherit properties from more than one class. Diagrammatically, this is as shown below: P.Ray@HIT-CSE,2012

Not supported in Java P.Ray@HIT-CSE,2012

That is, in Java one cannot write the following syntax: Unfortunately, Java does not support the multiple inheritances through classes though it supports using interfaces which is discussed in the next chapter. That is, in Java one cannot write the following syntax: class A { } class B {} class C extends A extends B P.Ray@HIT-CSE,2012

Hierarchical Inheritance In this type of inheritance, multiple classes share the same base class. That is, numbers of classes inherit the properties of one common base class. The derived classes may also become base classes for other classes. P.Ray@HIT-CSE,2012

P.Ray@HIT-CSE,2012

Examples For example, a university has a number of colleges under its affiliation. Each college may use the university name, the name of its chairperson, its address, phone number, etc. P.Ray@HIT-CSE,2012

Similarly, a vehicle possesses a number of properties or features and the common properties of all the vehicles may be put under one class vehicle, and different classes like two-wheeler, four-wheeler and three- wheeler can inherit the vehicle class. P.Ray@HIT-CSE,2012

As another example, in an engineering college, various departments can be termed as various classes which may have one parent common class-the name of the engineering college. Again for each department, there may be various classes, like lab staff, faculty class, etc. P.Ray@HIT-CSE,2012

P.Ray@HIT-CSE,2012

P.Ray@HIT-CSE,2012

P.Ray@HIT-CSE,2012

P.Ray@HIT-CSE,2012

Create a class Area. Having length and breadth. Create another class Volume extends Area. In main() create object of class volume. Calculate area and volume. Enter data through keyboard. P.Ray@HIT-CSE,2012

P.Ray@HIT-CSE,2012

P.Ray@HIT-CSE,2012

P.Ray@HIT-CSE,2012

P.Ray@HIT-CSE,2012

P.Ray@HIT-CSE,2012

P.Ray@HIT-CSE,2012

P.Ray@HIT-CSE,2012

METHOD OVERRIDING In method overriding, a base class method is overridden in the derived class. That is, the same method is written with the same signature as of the base class method but different implementation. P.Ray@HIT-CSE,2012

In method overloading, arguments and type of arguments are used to distinguish between two functions, but in method overriding no such distinction can be made. In method overriding, the signature of the two methods must match. This is shown in the program given below. P.Ray@HIT-CSE,2012

P.Ray@HIT-CSE,2012

If you write int show() { and a return 0;} it can’t override and show error. P.Ray@HIT-CSE,2012

P.Ray@HIT-CSE,2012

P.Ray@HIT-CSE,2012

P.Ray@HIT-CSE,2012

P.Ray@HIT-CSE,2012

Write a program to override calculate() method. In super class write method calculate for cube. In base class write method calculate for square. From main() calculate square and cube of a number. P.Ray@HIT-CSE,2012

P.Ray@HIT-CSE,2012

P.Ray@HIT-CSE,2012

You may write that int show(int x) in class B also. P.Ray@HIT-CSE,2012

DYNAMIC METHOD DISPATCH Binding is the process of linking the function call with the place where function definition is actually written so that when a function call is made, it can be ascertained where the control has to be transferred. Binding is also called linking, and is of two types: 1. Static binding 2. Dynamic binding. P.Ray@HIT-CSE,2012

Static binding When at compile time, it is known which function will be called in response to a function call, the binding is said to be static binding, compile time binding or early binding. Static binding is called so because before the program executes, it is fixed that a particular function be called in response to a function call. Each time the program executes, the same function will be called. As the linking is done at compile time, early to the execution of the program, it is also known as compile time binding or early binding. P.Ray@HIT-CSE,2012

Dynamic Binding When it is not ascertained as to which function is to be called in response to a function call, binding is delayed till the program executes. At run time, the decision is taken as to which function is to be called in response to a function call. This type of binding is known as late binding, runtime binding or dynamic binding. P.Ray@HIT-CSE,2012

It is necessary in situations, where there are two functions with the same name in both derived class and base class. At run time, it is decided using references and objects, as to which function of which class is to be called. P.Ray@HIT-CSE,2012

‘Dynamic method dispatch’ is the mechanism by which a call to an overridden function is resolved at run time, rather than at compile time. It is the mechanism through which Java implements run time polymorphism. Before understanding this, it is important to understand the concept of Java in inheritance: “A super class reference can refer to a derived class object”. For example consider the code: P.Ray@HIT-CSE,2012

A super class reference can refer to a derived class object (no overriding case) P.Ray@HIT-CSE,2012

Make show_A() and show_B() as a same overriding function. Then obj2 Make show_A() and show_B() as a same overriding function. Then obj2.show it will print from B. But in this case(previous slide) there is no overriding. Show_A() and show_B() are different. Using base class reference you can’t access derive class method. So obj2.show_B() is not possible. P.Ray@HIT-CSE,2012

P.Ray@HIT-CSE,2012

P.Ray@HIT-CSE,2012

Using one object we can access methods of base class and derived class P.Ray@HIT-CSE,2012

P.Ray@HIT-CSE,2012

P.Ray@HIT-CSE,2012

P.Ray@HIT-CSE,2012

P.Ray@HIT-CSE,2012

P.Ray@HIT-CSE,2012

HIERARCHICAL INHERITANCE In this type of inheritance, one class can be a parent of many other classes. Here class A is the base class for all three classes: B, C and D. They can share all fields, methods of class A except private members. P.Ray@HIT-CSE,2012

Suppose that a program has to deal with motor vehicles, including cars, trucks and motorcycles. The program could use named Vehicles to represent all types of vehicles. The Vehicles class could include instance variables, such as registration number and owner, and instance method, such as transferOwnership(). These are variables and methods common to all vehicles. Three subclasses of Vehicle-Car, Truck and Motorcycle-could then be used to hold variables and methods specific to particular types of vehicles. The Car class might add an instance variable 'numberOfDoors‘, the Truck class might have 'numberOfAxels‘ and the Motorcycle class could have a Boolean variable 'hasSidecar'. The declarations of these classes in Java program would look, in outline, like the following: P.Ray@HIT-CSE,2012

Write a program to enter data and display. P.Ray@HIT-CSE,2012

P.Ray@HIT-CSE,2012

THE SUPER KEYWORD Whenever the base class version of the function is called which is overridden in the derived class, the super keyword can be used. The super keyword always refers to the immediate base class. For example, consider the following code: P.Ray@HIT-CSE,2012

P.Ray@HIT-CSE,2012

P.Ray@HIT-CSE,2012

P.Ray@HIT-CSE,2012

P.Ray@HIT-CSE,2012

P.Ray@HIT-CSE,2012

P.Ray@HIT-CSE,2012

Usage of java super Keyword super is used to refer immediate parent class instance variable. super() is used to invoke immediate parent class constructor. super is used to invoke immediate parent class method. P.Ray@HIT-CSE,2012

1)super is used to refer immediate parent class instance variable. P.Ray@HIT-CSE,2012

In the above example Vehicle and Bike both class have a common property speed. Instance variable of current class is refered by instance by default, but we have to refer parent class instance variable that is why we use super keyword to distinguish between parent class instance variable and current class instance variable. P.Ray@HIT-CSE,2012

2) super is used to invoke parent class constructor. P.Ray@HIT-CSE,2012

Note: super() is added in each class constructor automatically by compiler. If u don’t use super keyword in previous program; the output remain same. P.Ray@HIT-CSE,2012

P.Ray@HIT-CSE,2012

Another example of super keyword where super() is provided by the compiler implicitly. P.Ray@HIT-CSE,2012

3) super can be used to invoke parent class method P.Ray@HIT-CSE,2012

In the above example Student and Person both classes have message() method if we call message() method from Student class, it will call the message() method of Student class not of Person class because priority is given to local. In case there is no method in subclass as parent, there is no need to use super. P.Ray@HIT-CSE,2012

Unit Charge Rs 5/ for commercial and Rs 2.5/ for domestic. Create a class Commercial. It will have the method setname(),getname() and calculatebill(int unit) to calculate Electric bill of Commercial connection subscriber. Create another class Domestic which is a subclass of Commercial. It will have the method calculatebill(int unit) to calculate the electric bill of domestic connection Subscriber. From the main() use setname(String) and calculatebill(int unit) to find the total amount of Electricity Bill. Unit Charge Rs 5/ for commercial and Rs 2.5/ for domestic. P.Ray@HIT-CSE,2012

P.Ray@HIT-CSE,2012

P.Ray@HIT-CSE,2012

1.It is a keyword used to store current object reference. this(current class) super(super class) 1.It is a keyword used to store current object reference. 1.It is a keyword used to store super class object in sub class. 2.Pre define instance variable used to hold current object reference. 2.Pre define instance variable used to hold super class object reference through sub class object. 3.Used to separate state of multiple objects of same class and also used to separate local variables and class level variables in a non-static method if both have same name. 3.Used to separate super class and subclass members if both have same name. 4.It must be used explicitly if non-static variables and local variables or parameter name is same. 4.It must be used explicitly if super class and sub class members have same names. 5.Can't be referred from static context. It can be printed, means can be called from System.out.println. For example System.out.println(this.x); 5.Can't be referred from static context. It can't be printed, means cannot be called from System.out.println. For example System.out.println(super.x); it leads to compile time error. P.Ray@HIT-CSE,2012

CONSTRUCTOR AND INHERITANCE This section discusses how the constructors are called when they are present both in base and derived classes, and how the values are passed from derived class to base class. Assume a small example of single level inheritance in which class A is inherited by class B. Both the classes have their default constructors. When an object of class B is created, it calls the constructor of class B, but as class B has got A as its parent class, constructor of class A will be called first, followed by that of class B. P.Ray@HIT-CSE,2012

This is so because when derived class has inherited base class, obviously it will be using the data member from base class. Now without calling the constructor of base class, data members of base class will not be initialized and if derived class uses the uninitialized data members of base class, unexpected result may follow. P.Ray@HIT-CSE,2012

P.Ray@HIT-CSE,2012

P.Ray@HIT-CSE,2012

P.Ray@HIT-CSE,2012

P.Ray@HIT-CSE,2012

P.Ray@HIT-CSE,2012

P.Ray@HIT-CSE,2012

P.Ray@HIT-CSE,2012

P.Ray@HIT-CSE,2012

FINAL CLASS A final class is a class which is declared final by placing the keyword final before class definition. A final class has the property that it cannot be inherited. One example of final class is “System class” defined in package java.lang. When you say that an entire class is final, you state that you do not want to inherit from this class or allow anyone else to do so. P.Ray@HIT-CSE,2012

In other words, for some reason, the design of your class is such that there is never a need to make any changes, or for safety or security reasons, you do not want sub-classing. Note that the fields of a final class can be final depending on how you write. The same rules apply to final for fields regardless of whether the class is defined as final. However, because it prevents inheritance, all methods in a final class are implicitly final since there is no way to override them. P.Ray@HIT-CSE,2012

P.Ray@HIT-CSE,2012

P.Ray@HIT-CSE,2012