Inheritance. Introduction Inheritance is one of the cornerstones of object-oriented programming because it allows the creation of hierarchical classifications.

Slides:



Advertisements
Similar presentations
OO Programming in Java Objectives for today: Overriding the toString() method Polymorphism & Dynamic Binding Interfaces Packages and Class Path.
Advertisements

Java Programming 2 Dr. Priti Srinivas Sajja Introductory concepts of java programming as specified in PGDCA 203:Object Technology, S P University.
INHERITANCE BASICS Reusability is achieved by INHERITANCE
METHOD OVERRIDING Sub class can override the methods defined by the super class. Overridden Methods in the sub classes should have same name, same signature.
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.
OOP: Inheritance By: Lamiaa Said.
1 Inheritance Lecture 9 from Chapter 8. 2 Review Command line arguments Basic Inheritance Member access and inheritance Using super Creating a multi-level.
Constructors And Instantiation. Constructor Basics Every class must have a constructor Even abstract classes!! No return types Their names must exactly.
ITEC200 – Week03 Inheritance and Class Hierarchies.
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.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
CSM-Java Programming-I Spring,2005 Fundamental Data Types Lesson - 2.
Inheritance. In this chapter, we will cover: The concept of inheritance Extending classes Overriding superclass methods Working with superclasses that.
1 Chapter 6 Inheritance, Interfaces, and Abstract Classes.
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.
Appendix A.2: Review of Java and Object-Oriented Programming: Part 2 “For the object-oriented project, remember that the primary unit of decomposition.
“is a”  Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class.
CSM-Java Programming-I Spring,2005 Objects and Classes Overview Lesson - 1.
1 Object-Oriented Software Engineering CS Interfaces Interfaces are contracts Contracts between software groups Defines how software interacts with.
Sun Certified Java Programmer, ©2004 Gary Lance, Chapter 5, page 1 Sun Certified Java 1.4 Programmer Chapter 5 Notes Gary Lance
JAVA WORKSHOP SESSION – 3 PRESENTED BY JAYA RAO MTech(CSE) NEWTON’S INSTITUTE OF ENGINEERING 1.
Inheritance - Polymorphism ITI 1121 Nour El Kadri.
OOP: Encapsulation,Abstraction & Polymorphism. What is Encapsulation Described as a protective barrier that prevents the code and data being randomly.
Programming in Java CSCI-2220 Object Oriented Programming.
Summing Up Object Oriented Design. Four Major Components: Abstraction modeling real-life entities by essential information only Encapsulation clustering.
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 9 Inheritance 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.
Topics Inheritance introduction
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Inheritance and Class Hierarchies Chapter 3. Chapter Objectives  To understand inheritance and how it facilitates code reuse  To understand how Java.
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.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
CSI 3125, Preliminaries, page 1 Inheritance. CSI 3125, Preliminaries, page 2 Inheritance Using inheritance, can create a general class that defines traits.
Inheritance in C++ Bryce Boe 2012/08/28 CS32, Summer 2012 B.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
POLYMORPHISM Chapter 6. Chapter Polymorphism  Polymorphism concept  Abstract classes and methods  Method overriding  Concrete sub classes and.
Internet Computing Module II. Syllabus Creating & Using classes in Java – Methods and Classes – Inheritance – Super Class – Method Overriding – Packages.
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.
COP INTERMEDIATE JAVA Inheritance, Polymorphism, Interfaces.
UNIT-2. Inheritance –Definition Single Inheritance Benefits of inheritance Member access rules super classes Polymorphism Method overriding Using final.
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.
Modern Programming Tools And Techniques-I
OOP: Encapsulation &Abstraction
Inheritance class TwoDShape { private double width;
INHERITANCE.
Inheritance-Basics.
Inheritance and Polymorphism
UNIT-2.
Introduction to Programming with Java
Inheritance in Java.
Object Oriented Programming
Modern Programming Tools And Techniques-I Inheritance
Extending Classes.
Java Programming Language
Advanced Java Topics Chapter 9
INHERITANCE.
INHERITANCE BASICS Reusability is achieved by INHERITANCE
Java – Inheritance.
Inheritance Cse 3rd year.
Java Programming, Second Edition
Java Inheritance.
Index Understanding static Final Inheritance Super
Inheritance and Polymorphism
Chapter 11 Inheritance and Polymorphism Part 1
Presentation transcript:

Inheritance

Introduction Inheritance is one of the cornerstones of object-oriented programming because it allows the creation of hierarchical classifications. Using inheritance, you can create a general class that defines traits common to a set of related items. Class Inheritance in java mechanism is used to build new classes from existing classes.

Introduction A class that is derived from another class is called a subclass. The class from which the subclass is derived is called a superclass. A subclass inherits all the members from its superclass. Constructors are not members, so they are not inherited by subclasses, but the constructor of the superclass can be invoked from the subclass.

Syntax class Subclass-name extends Superclass- name { //methods and fields }

Object Class The Object class, defined in the java.lang package, defines and implements behavior common to all classesObject

Types of Inheritance

Multiple Inheritance is not allowed in Java

Member Access and Inheritance A subclass includes all of the members of its superclass, it cannot access those members of the superclass that have been declared as private.

Example / /Create a superclass. class A { int i; // public by default private int j; // private to A void setij(int x, int y) { i = x; j = y; } // A's j is not accessible here. class B extends A { int total; void sum() { total = i + j; // ERROR, j is not accessible here }

Practical Example Box Example

A Superclass Variable Can Reference a Subclass Object Areference variable of a superclass can be assigned a reference to any subclass derived from that superclass.

A Superclass Variable Can Reference a Subclass Object BoxWeight weightbox = new BoxWeight(3, 5, 7, 8.37); Box plainbox = new Box(); double vol; vol = weightbox.volume(); System.out.println("Volume of weightbox is " + vol); System.out.println("Weight of weightbox is " +weightbox.weight); System.out.println(); // assign BoxWeight reference to Box reference plainbox = weightbox; vol = plainbox.volume(); // OK, volume() defined in Box System.out.println("Volume of plainbox is " + vol); /* The following statement is invalid because plainbox does not define a weight member. */ // System.out.println("Weight of plainbox is " + plainbox.weight);

Using super Using super to Call Superclass Constructors – super(arg-list); Here, arg-list specifies any arguments needed by the constructor in the superclass. super( ) must always be the first statement executed inside a subclass’ constructor. Since constructors can be overloaded, super( ) can be called using any form defined by the superclass. The constructor executed will be the one that matches the arguments

Example A complete implementation of BoxWeight.

A Second Use for super The second form of super acts somewhat like this, except that it always refers to the superclass of the subclass in which it is used. super.member Here, member can be either a method or an instance variable. This second form of super is most applicable to situations in which member names of a subclass hide members by the same name in the superclass.

A Second Use for super // Using super to overcome name hiding. class A { int i; } // Create a subclass by extending class A. class B extends A { int i; // this i hides the i in A B(int a, int b) { super.i = a; // i in A i = b; // i in B } void show() { System.out.println("i in superclass: " + super.i); System.out.println("i in subclass: " + i); }

Creating a Multilevel Hierarchy

class A { ….. } class B extends A { ……. } class C extends B { …… }

When Constructors Are Called class A { A() { System.out.println("Inside A's constructor."); }} class B { B() { System.out.println("Inside B's constructor."); }} class C { C() { System.out.println("Inside C's constructor."); }}

When Constructors Are Called class CallingCons { public static void main(String args[]) { C c = new C(); } Output: Inside A’s constructor Inside B’s constructor Inside C’s constructor

Method Overriding In a class hierarchy, when a method in a subclass has the same name and type signature as a method in its superclass, then the method in the subclass is said to override the method in the superclass.

Method Overriding class A { void show() { System.out.println(“Class A”); }} class B extends A { void show() { System.out.println(“Class B”); }} class Override { public static void main(String args[]) { B subOb = new B(); subOb.show(); // this calls show() in B }}

Method Overriding If you wish to access the superclass version of an overridden method, you can do so by using super. In class B of previous example: void show() { super.show(); // this calls A's show() System.out.println(“Class B”); }

Methods with differing type signatures are overloaded – not overridden. class A { void show() { System.out.println("i and j: " + i + " " + j); }} // Create a subclass by extending class A. class B extends A { // overload show() void show(String msg) { System.out.println(msg ); }} class Override { public static void main(String args[]) { B subOb = new B(); subOb.show("This is in B"); // this calls show() in B subOb.show(); // this calls show() in A }}

Dynamic Method Dispatch Method overriding forms the basis for one of Java’s most powerful concepts: dynamic method dispatch. Dynamic method dispatch is the mechanism by which a call to an overridden method is resolved at run time, rather than compile time. Dynamic method dispatch is important because this is how Java implements run-time polymorphism.

Dynamic Method Dispatch class A { void callme() { System.out.println("Inside A's callme method"); }} class B extends A { void callme() { System.out.println("Inside B's callme method"); }} class Dispatch { public static void main(String args[]) { A a = new A(); // object of type A B b = new B(); // object of type B A r; // obtain a reference of type A r = a; // r refers to an A object r.callme(); // calls A's version of callme r = b; // r refers to a B object r.callme(); // calls B's version of callme }}

Applying Method Overriding Example of run-time polymorphism.

Using Abstract Classes Define a superclass that declares the structure of a given abstraction without providing a complete implementation of every method.

Using Abstract Classes abstract class A { abstract void callme(); // concrete methods are still allowed in abstract classes void callmetoo() { System.out.println("This is a concrete method."); }} class B extends A { void callme() { System.out.println("B's implementation of callme."); }} class AbstractDemo { public static void main(String args[]) { B b = new B(); b.callme(); b.callmetoo(); }}

Using final with Inheritance Using final to Prevent Overriding class A { final void meth() { System.out.println("This is a final method."); }} class B extends A { void meth() { // ERROR! Can't override. System.out.println("Illegal!"); }}

Using final with Inheritance Using final to Prevent Inheritance final class A { //... } // The following class is illegal. class B extends A { // ERROR! Can't subclass A //... }

Thank You