JAVA WORKSHOP SESSION – 3 PRESENTED BY JAYA RAO MTech(CSE) NEWTON’S INSTITUTE OF ENGINEERING 1.

Slides:



Advertisements
Similar presentations
Final and Abstract Classes
Advertisements

Chapter 1 Inheritance University Of Ha’il.
INHERITANCE BASICS Reusability is achieved by INHERITANCE
Module 8 “Polymorphism and Inheritance”. Outline Understanding Inheritance Inheritance Diagrams Constructors in Derived Classes Type Compatibility Polymorphism.
1 Inheritance Lecture 9 from Chapter 8. 2 Review Command line arguments Basic Inheritance Member access and inheritance Using super Creating a multi-level.
1 l Inheritance Basics l Programming with Inheritance l Dynamic Binding and Polymorphism Inheritance.
Inheritance Inheritance Reserved word protected Reserved word super
Inheritance Java permits you to use your user defined classes to create programs using inheritance.
Objectives Introduction to Inheritance and Composition (Subclasses and SuperClasses) Overriding (and extending), and inheriting methods and constructors.
Inheritance Polymorphism Briana B. Morrison CSE 1302C Spring 2010.
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 8.1 – 8.5.
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.
CS 2511 Fall  Abstraction Abstract class Interfaces  Encapsulation Access Specifiers Data Hiding  Inheritance  Polymorphism.
Chapter 7 - Generalization/Specialization and Inheritance1 Chapter 7 Generalization/Specialization and Inheritance.
Inheritance. © 2004 Pearson Addison-Wesley. All rights reserved 8-2 Inheritance Inheritance is a fundamental object-oriented design technique used to.
“is a”  Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class.
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.
Inheritance - Polymorphism ITI 1121 Nour El Kadri.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
8. Inheritance “Is-a” Relationship. Topics Creating Subclasses Overriding Methods Class Hierarchies Abstract Class Inheritance and GUIs The Timer Class.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Inheritance. Inheritance is a fundamental object-oriented design technique used to create and organize reusable classes Chapter 8 focuses on: deriving.
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
Inheritance Objectives: Creating new classes from existing classes The protected modifier Creating class hierarchies Abstract classes Indirect visibility.
Programming With Java ICS201 University Of Ha’il1 Chapter 7 Inheritance.
Coming up: Inheritance
Topics Inheritance introduction
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 9.1 – 9.4.
Inheritance in Java. Access Specifiers private keywordprivate keyword –Used for most instance variables –private variables and methods are accessible.
Classes, Interfaces and Packages
AD Lecture #1 Object Oriented Programming Three Main Principles 1 Inheritance Encapsulation Polymorphism.
Java Software Solutions Lewis and Loftus Chapter 8 Copyright 1997 by John Lewis and William Loftus. All rights reserved. 1 Inheritance -- Introduction.
Basic Concepts of OOP.  Object-Oriented Programming (OOP) is a type of programming added to php5 that makes building complex, modular and reusable web.
CSI 3125, Preliminaries, page 1 Inheritance. CSI 3125, Preliminaries, page 2 Inheritance Using inheritance, can create a general class that defines traits.
Inheritance Chapter 11 in Gaddis. Is a relationships in ‘real’ life Exist when one object is a specialized version of another one –Examples An english.
Inheritance ndex.html ndex.htmland “Java.
Java Inheritance in Java. Inheritance Inheritance is a mechanism in which one object acquires all the properties and behaviors of parent object. The idea.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
Software Technology - I Classes Objects Methods (Cont.) A Baby is a Monkey.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Outline Creating Subclasses Overriding Methods Class Hierarchies Inheritance.
L 3.1 Forms of Inheritance Inheritance is used in a variety of way and for a variety of different purposes. Inheritance for Specialization Inheritance.
Polymorphism 1. Reuse of code: every time a new sub-class is defined, programmers are reusing the code in a super-class. All non-private members of a.
 Description of Inheritance  Base Class Object  Subclass, Subtype, and Substitutability  Forms of Inheritance  Modifiers and Inheritance  The Benefits.
UNIT-2. Inheritance –Definition Single Inheritance Benefits of inheritance Member access rules super classes Polymorphism Method overriding Using final.
Inheritance & Method Overriding BCIS 3680 Enterprise Programming.
COMPUTER SCIENCE & TECHNOLOGY DEGREE PROGRAMME FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UVA WELLASSA ‏ Properties of Object Oriented Programming.
Java Programming Fifth Edition Chapter 9 Introduction to Inheritance.
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Modern Programming Tools And Techniques-I
Programming in Java, 2e Sachin Malhotra Saurabh Choudhary.
Lecture 12 Inheritance.
Inheritance-Basics.
Final and Abstract Classes
Inheritance and Polymorphism
UNIT-2.
Inheritance AKEEL AHMED.
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
Object Oriented Analysis and Design
OOP’S Concepts in C#.Net
INHERITANCE.
Java – Inheritance.
Object Oriented Programming in JAVA
Index Understanding static Final Inheritance Super
Inheritance and Polymorphism
Chapter 11 Inheritance and Polymorphism Part 1
Final and Abstract Classes
Presentation transcript:

JAVA WORKSHOP SESSION – 3 PRESENTED BY JAYA RAO MTech(CSE) NEWTON’S INSTITUTE OF ENGINEERING 1

2 Session-III You learn  Inheritance  Constructors  Method overloading  Method overrding  Keywords extends, super,final  Abstract classes  Dynamic method despatch  Polymorphism

OOPs revolve around the three concepts: 1. Encapsulation OOPs revolve around the three concepts: 1. Encapsulation 2. Polymorphism 3. Inheritance 3

The three principles of OOP Encapsulation Encapsulation –Objects hide their functions (methods) and data (instance variables) Inheritance Inheritance –Each subclass inherits all variables of its superclass Polymorphism Polymorphism –Interface same despite different data types car auto- matic manual Super class Subclasses draw()

5 Inheritance A class can extend another class, inheriting all its data members and methods while redefining some of them and/or adding its own. A class can extend another class, inheriting all its data members and methods while redefining some of them and/or adding its own. Inheritance implements the “ is a ” relationship between objects. Inheritance implements the “ is a ” relationship between objects.

Using inheritance, you can create a Using inheritance, you can create a general class that defines traits common to a set of related items. 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. This class can then be inherited by other, more specific classes, each adding those things that are unique to it. In the terminology of Java, a class that is inherited is called a superclass. In the terminology of Java, a class that is inherited is called a superclass. The class that does the inheriting is called a subclass. The class that does the inheriting is called a subclass. 6

7

8 Inheritance implements the “is a” relationship. Inheritance implements the “is a” relationship. Not to be confused with embedding (an object has another object as a part), which represents the “ has a” relationship: Not to be confused with embedding (an object has another object as a part), which represents the “ has a” relationship: A sailboat is a boat A sailboat has a sail

Using inheritance, you can create a Using inheritance, you can create a general class that defines traits common to a set of related items. 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. This class can then be inherited by other, more specific classes, each adding those things that are unique to it. In the terminology of Java, a class that is inherited is called a superclass. In the terminology of Java, a class that is inherited is called a superclass. The class that does the inheriting is called a subclass. The class that does the inheriting is called a subclass. 9

Therefore, a subclass is a specialized version of a superclass. Therefore, a subclass is a specialized version of a superclass. It inherits all of the instance variables and methods defined by the superclass and adds its own, unique elements. It inherits all of the instance variables and methods defined by the superclass and adds its own, unique elements. 10

The new class will be similar to the existing The new class will be similar to the existing class, but will have some new characteristics. class, but will have some new characteristics. 11

BENEFITS Inheritance promotes the concept of code reusability. Inheritance promotes the concept of code reusability. Inheritance allows Inheritance allows a better data analysis, a better data analysis, reduction in development time, reduction in development time, less coding and better performance less coding and better performance 12

13 subclass or derived class superclass or base class extends Inheritance (cont’d)

14 In Java, a subclass can extend only one superclass. In Java, a subclass can extend only one superclass. In Java, a class can implement several interfaces — this is Java’s form of multiple inheritance. In Java, a class can implement several interfaces — this is Java’s form of multiple inheritance.

15 Inheritance (cont’d) Game GameFor2 BoardGame Chess Backgammon Solitaire

extends keyword 16 The general form The general form class subclass-name extends superclass-name { // body of class // body of class }

The following program creates a The following program creates a superclass called A and a superclass called A and a subclass called B. subclass called B. 17

class A { ; ; //super class } Class B extends class A { ; ; //sub class } class mainclass { class mainclass { public static void main(String args[]) { public static void main(String args[]) { } 18

// A simple example of inheritance. // A simple example of inheritance. // Create a superclass. // Create a superclass. class A { class A { int i, j; int i, j; void showij() { void showij() { System.out.println("i and j: " + i + " " + j); System.out.println("i and j: " + i + " " + j); } } // Create a subclass by extending class A. // Create a subclass by extending class A. class B extends A { class B extends A { int k; int k; void showk() { void showk() { System.out.println("k: " + k); System.out.println("k: " + k); } } void sum() { void sum() { System.out.println("i+j+k: " + (i+j+k)); System.out.println("i+j+k: " + (i+j+k)); } } } 19

class SimpleInheritance { class SimpleInheritance { public static void main(String args[]) { public static void main(String args[]) { A superOb = new A(); A superOb = new A(); B subOb = new B(); B subOb = new B(); subOb.showij(); subOb.showij(); subOb.showk(); subOb.showk(); System.out.println(); System.out.println(); System.out.println("Sum of i, j and k in subOb:"); System.out.println("Sum of i, j and k in subOb:"); subOb.sum(); subOb.sum(); } } } 20

21 Inheritance (cont’d) An object of a class at the bottom of a hierarchy inherits all the methods of all the classes above. An object of a class at the bottom of a hierarchy inherits all the methods of all the classes above.

Simple Single Inheritance Simple Single Inheritance The class that is used as a basis for defining a new class is called a parent class (or superclass or base class.) The class that is used as a basis for defining a new class is called a parent class (or superclass or base class.) The new class based on the parent class is called a child class (or subclass or derived class.) The new class based on the parent class is called a child class (or subclass or derived class.) In Java, children inherit characteristics from just one parent. In Java, children inherit characteristics from just one parent. This is called single inheritance. This is called single inheritance. 22

Single inheritance : A derived class with only one base class is called single inheritance. 23

24 Multiple inheritance A derived class with several base classes is called multiple inheritance A derived class with several base classes is called multiple inheritance

25

Multilevel inheritance The mechanism of deriving a class from another derived class is called multilevel inheritance. The mechanism of deriving a class from another derived class is called multilevel inheritance. 26

27

Hierarchical inheritance One class may be inherited by more than one classes. One class may be inherited by more than one classes. This process is known as hierarchical inheritance This process is known as hierarchical inheritance 28

29

Hybrid inheritance It is a combination of hierarchical and multiple inheritance. It is a combination of hierarchical and multiple inheritance. 30

31

32

Using super super has two general forms. super has two general forms. The first calls the superclass’ constructor. The first calls the superclass’ constructor. The second is used to access a member of the superclass that has been hidden by a member of a The second is used to access a member of the superclass that has been hidden by a member of a subclass. subclass. 33

Using super to Call Superclass Constructors A subclass can call a constructor method defined by its superclass by use of the A subclass can call a constructor method defined by its superclass by use of the following form of super: following form of super: super(parameter-list); super(parameter-list); super( ) must always be the first statement executed inside a subclass’ constructor. super( ) must always be the first statement executed inside a subclass’ constructor. 34

parameter-list is defined by the constructor in the superclass. parameter-list is defined by the constructor in the superclass. super(parameter-list) must be the first statement executed inside a subclass' constructor. super(parameter-list) must be the first statement executed inside a subclass' constructor. 35

Here is a demo for how to use super to call constructor from parent class Here is a demo for how to use super to call constructor from parent class See pgm shapessuper1.java See pgm shapessuper1.java 36

Use super to reference members from parent class Its general form is: Its general form is: super.member super.member member can be either a method or an instance variable. member can be either a method or an instance variable. See pgm super2.java See pgm super2.java 37

When Constructors Are Called In a class hierarchy, constructors are called in order of derivation, from superclass to subclass. In a class hierarchy, constructors are called in order of derivation, from superclass to subclass. The following program illustrates when constructors are executed: The following program illustrates when constructors are executed: See constructorder.java See constructorder.java 38

What is Method Overriding Method Overriding happens when a method in a subclass has the same name and type signature as a method in its superclass. Method Overriding happens when a method in a subclass has the same name and type signature as a method in its superclass. When an overridden method is called within a subclass, it will refer to the method defined in the subclass. When an overridden method is called within a subclass, it will refer to the method defined in the subclass. The method defined by the superclass will be hidden. The method defined by the superclass will be hidden. See pgm override1.java or shaperssuper2.java See pgm override1.java or shaperssuper2.java 39

super and overridden method To access the superclass version of an overridden function, you can do so by using super. To access the superclass version of an overridden function, you can do so by using super. See override2.java or shapessuper2.java See override2.java or shapessuper2.java 40

Method overriding vs method overload Method overriding occurs when the names and the type signatures of the two methods are identical. Method overriding occurs when the names and the type signatures of the two methods are identical. If not, the two methods are overloaded. If not, the two methods are overloaded. For example, consider this modified version of the preceding example: For example, consider this modified version of the preceding example: See prg override3.java See prg override3.java 41

Dynamic Method Dispatch When an overridden method is called through a superclass reference, Java determines which version of that method to execute. When an overridden method is called through a superclass reference, Java determines which version of that method to execute. Here is an example that illustrates dynamic method dispatch: Here is an example that illustrates dynamic method dispatch: see dmd.java see dmd.java 42

What are Abstract Classes You can require that certain methods be overridden by subclasses by specifying the abstract type modifier. You can require that certain methods be overridden by subclasses by specifying the abstract type modifier. To declare an abstract method, use this general form: To declare an abstract method, use this general form: abstract type name(parameter-list); abstract type name(parameter-list); 43

No method body is present for abstract method. No method body is present for abstract method. Any class that contains one or more abstract methods must also be declared abstract. Any class that contains one or more abstract methods must also be declared abstract. 44

abstract class MyAbstractClass{ abstract class MyAbstractClass{ abstract type name(parameter-list); abstract type name(parameter-list); } Here is an abstract class, followed by a class which implements its abstract method. Here is an abstract class, followed by a class which implements its abstract method. See prg abs1.java See prg abs1.java 45

Using abstract methods and classes string-lower-case-example string-lower-case-example string-lower-case-example string-lower-case-example vaCourse/Book/Part1/Java/Chapter02/ exercises.html vaCourse/Book/Part1/Java/Chapter02/ exercises.html 46