Chapter 5. Inheritance

Similar documents
Programming in Java, 2e Sachin Malhotra Saurabh Choudhary

Inheritance in java is a mechanism in which one object acquires all the properties and behaviors of parent object.

Inheritance. Benefits of Java s Inheritance. 1. Reusability of code 2. Code Sharing 3. Consistency in using an interface. Classes

Day 4. COMP1006/1406 Summer M. Jason Hinek Carleton University

What is Inheritance?

Inheritance (Outsource: )

Java Object Oriented Design. CSC207 Fall 2014

OOPS Viva Questions. Object is termed as an instance of a class, and it has its own state, behavior and identity.

INHERITANCE Mrs. K.M. Sanghavi

C++ Important Questions with Answers

Argument Passing All primitive data types (int etc.) are passed by value and all reference types (arrays, strings, objects) are used through refs.

PROGRAMMING LANGUAGE 2

OVERRIDING. 7/11/2015 Budditha Hettige 82

Class, Variable, Constructor, Object, Method Questions

Chapter 5 Object-Oriented Programming

Object Orientated Programming Details COMP360

Lecture 2: Java & Javadoc

Inheritance and Polymorphism

CREATED BY: Muhammad Bilal Arslan Ahmad Shaad. JAVA Chapter No 5. Instructor: Muhammad Naveed

Practice for Chapter 11

Object-Oriented Concepts

OOPs Concepts. 1. Data Hiding 2. Encapsulation 3. Abstraction 4. Is-A Relationship 5. Method Signature 6. Polymorphism 7. Constructors 8.

HAS-A Relationship. If A uses B, then it is an aggregation, stating that B exists independently from A.

CS-202 Introduction to Object Oriented Programming

CSCI-142 Exam 1 Review September 25, 2016 Presented by the RIT Computer Science Community

Pieter van den Hombergh Thijs Dorssers Stefan Sobek. February 10, 2017

CS5000: Foundations of Programming. Mingon Kang, PhD Computer Science, Kennesaw State University

Instance Members and Static Members

CSEN401 Computer Programming Lab. Topics: Object Oriented Features: Abstraction and Polymorphism

HAS-A Relationship. Association is a relationship where all objects have their own lifecycle and there is no owner.

CONSTRUCTOR & Description. String() This initializes a newly created String object so that it represents an empty character sequence.

Unit 4 - Inheritance, Packages & Interfaces

What are the characteristics of Object Oriented programming language?

Inheritance and Polymorphism

Chapter 14 Abstract Classes and Interfaces

More Relationships Between Classes

Java Magistère BFA

ECE 122. Engineering Problem Solving with Java

CS 251 Intermediate Programming Inheritance

Object Oriented Programming is a programming method that combines: Advantage of Object Oriented Programming

Pieter van den Hombergh Thijs Dorssers Stefan Sobek. January 11, 2018

Chapter 9 Inheritance

Inheritance. Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L

Software Practice 1 - Inheritance and Interface Inheritance Overriding Polymorphism Abstraction Encapsulation Interfaces

Inheritance. Transitivity

Name Return type Argument list. Then the new method is said to override the old one. So, what is the objective of subclass?

Basics of Object Oriented Programming. Visit for more.

Starting Out with Java: From Control Structures Through Objects Sixth Edition

Java Class Design. Eugeny Berkunsky, Computer Science dept., National University of Shipbuilding

Java Inheritance. Written by John Bell for CS 342, Spring Based on chapter 6 of Learning Java by Niemeyer & Leuck, and other sources.

[ L5P1] Object-Oriented Programming: Advanced Concepts

JAVA. Lab-9 : Inheritance

COMP200 INTERFACES. OOP using Java, from slides by Shayan Javed

Java Fundamentals (II)

Arrays Classes & Methods, Inheritance

The major elements of the object-oriented model

EKT472: Object Oriented Programming. Overloading and Overriding Method

CSE1720. General Info Continuation of Chapter 9 Read Chapter 10 for next week. Second level Third level Fourth level Fifth level

1.00 Lecture 13. Inheritance

CS260 Intro to Java & Android 03.Java Language Basics

Example: Count of Points

More on Inheritance. Interfaces & Abstract Classes

Method Overriding in Java

Inheritance & Polymorphism Recap. Inheritance & Polymorphism 1

First IS-A Relationship: Inheritance

Object Oriented Programming in Java. Jaanus Pöial, PhD Tallinn, Estonia

Object Oriented Programming. Java-Lecture 11 Polymorphism

ASSIGNMENT NO 13. Objectives: To learn and understand concept of Inheritance in Java

Inheritance (Part 5) Odds and ends

HAS-A Relationship. Association is a relationship where all objects have their own lifecycle and there is no owner.

Superclasses / subclasses Inheritance in Java Overriding methods Abstract classes and methods Final classes and methods

Graphical Interface and Application (I3305) Semester: 1 Academic Year: 2017/2018 Dr Antoun Yaacoub

INHERITANCE & POLYMORPHISM. INTRODUCTION IB DP Computer science Standard Level ICS3U. INTRODUCTION IB DP Computer science Standard Level ICS3U

INHERITANCE AND EXTENDING CLASSES

24. Inheritance. Java. Fall 2009 Instructor: Dr. Masoud Yaghini

COMP 250 Fall inheritance Nov. 17, 2017

Inheritance -- Introduction

Computer Science II (20073) Week 1: Review and Inheritance

22. Inheritance. Java. Summer 2008 Instructor: Dr. Masoud Yaghini

CHAPTER. CHAPTER CONTENTS 6.1 What Is Inheritance? Animation 6.12 Advanced Graphics (Optional) 6.14 Summary

IT101. Inheritance, Encapsulation, Polymorphism and Constructors

Overview. Lecture 7: Inheritance and GUIs. Inheritance. Example 9/30/2008

Inheritance. Inheritance allows the following two changes in derived class: 1. add new members; 2. override existing (in base class) methods.

Unit 3 INFORMATION HIDING & REUSABILITY. -Inheritance basics -Using super -Method Overriding -Constructor call -Dynamic method

STUDENT LESSON A20 Inheritance, Polymorphism, and Abstract Classes

Inheritance (an intuitive description)

CS/B.TECH/CSE(New)/SEM-5/CS-504D/ OBJECT ORIENTED PROGRAMMING. Time Allotted : 3 Hours Full Marks : 70 GROUP A. (Multiple Choice Type Question)

Inheritance, Polymorphism, and Interfaces

CS1150 Principles of Computer Science Objects and Classes

Programming overview

Rules and syntax for inheritance. The boring stuff

Questions Answer Key Questions Answer Key Questions Answer Key

CS5000: Foundations of Programming. Mingon Kang, PhD Computer Science, Kennesaw State University

Exercise: Singleton 1

Comp 249 Programming Methodology

Islamic University of Gaza Faculty of Engineering Computer Engineering Department

CS1020: DATA STRUCTURES AND ALGORITHMS I

Computer Science 210: Data Structures

QUESTIONS FOR AVERAGE BLOOMERS

Lecture 36: Cloning. Last time: Today: 1. Object 2. Polymorphism and abstract methods 3. Upcasting / downcasting

Transcription:

Chapter 5 Inheritance

Objectives Know the difference between Inheritance and aggregation Understand how inheritance is done in Java Learn polymorphism through Method Overriding Learn the keywords : super and final Understand the basics of abstract class.

Inheritance Is the ability to derive something specific from something generic. aids in the reuse of code. A class can inherit the features of another class and add its own modification. The parent class is the super class and the child class is known as the subclass. A subclass inherits all the properties and methods of the super class.

Example class Bank { // prop // actions } class onlinebank extends Bank { // this class import all prop and actions from bank class } inheritance: the process of creating new class from old class is called inheritance. Use : code reusability

Inheritance

Aggregation We can make up objects out of other objects. The behaviour of the bigger object is defined by the behaviour of its component objects, separately and in conjunction with each other. cars contain engine which in turn contains an ignition system and starter motor. a whole-part relationship exists in aggregation car being the whole and engine being its part.

Types of Inheritance Single Multiple Multilevel Hierarchical Hybrid

Single level inheritance Classes have only base class

Multi level There is no limit to this chain of inheritance (as shown below) but getting down deeper to four or five levels makes code excessively complex.

Multiple Inheritance A class can inherit from more than one unrelated class

Hierarchical Inheritance In hierarchical inheritance, more than one class can inherit from a single class. Class C inherits from both A and B.

Hybrid Inheritance is any combination of the above defined inheritances

Deriving Classes Classes are inherited from other class by declaring them as a part of its definition For e.g. class MySubClass extends MySuperClass extends keyword declares that MySubClass inherits the parent class MySuperClass.

Inheritance

Method Overriding 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. it is a feature that supports polymorphism. When an overridden method is called through the subclass object, it will always refer to the version of the method defined by the subclass. The superclass version of the method is hidden.

Method Overriding Example class A { int i = 0; void dooverride (int k) { i = k; } } class B extends A { void dooverride(int k) { i = 2 * k; System.out.println( The value of i is: +i); } public static void main (String args[]) { B b = new B(); b.dooverride(12); }}

The value of i is: 24 The output

Late binding Vs Early binding Binding is connecting a method call to a method body. When binding is performed before the program is executed, it is called early binding. If binding is delayed till runtime it is late binding. also known as dynamic binding or runtime binding. All the methods in Java use late binding (except static and final).

Superclass Can Refer to a Subclass Object class SuperClass { int instancevariable = 10; static int classvariable = 20;} class SubClass extends SuperClass{ int instancevariable = 12; static int classvariable = 25; public static void main(string args[]) { SuperClass s=new SubClass();

Superclass Can Refer to a Subclass Object System.out.println("Superclass Instance variable: "+s.instancevariable); System.out.println("Superclass static variable: "+s.classvariable); SubClass st=new SubClass(); System.out.println("Subclass Instance variable: "+st.instancevariable); System.out.println("Subclass static variable: "+st.classvariable); } }

The Output Superclass Instance variable: 10 Superclass static variable: 20 Subclass Instance variable: 12 Subclass static variable: 25

super Keyword For invoking the methods of the super class. For accessing the member variables of the super class. For invoking the constructors of the super class.

super Keyword (contd.) class A { void show() { System.out.println( Super Class show method ); } } class B extends A void show() { System.out.println( Subclass show method ); } public static void main (String args[ ]) { A s1=new A(); s1.show(); B s2=new B (); s2.show(); }}

Problem and Solution Two methods being called by two different objects (inherited), instead the job can be done by one object only, i.e. using super keyword.

Case 1: Invoking Methods using super class ANew { void show() { System.out.println( Super Class show method ); } } class BNew extends ANew { void show() { super.show(); System.out.println( Subclass show method ); } public static void main (String args[ ]) { BNew s2=new BNew (); s2.show(); }}

Case 2: Accessing variables using super class Super_Variable { int b=30; } class SuperClass extends Super_Variable { int b=12; void show() { System.out.println( subclass class variable: + b); System.out.println( superclass instance variable: + super.b); } public static void main (String args[]) { SuperClass s=new SubClass(); s.show(); // call to show method of Sub Class B }}

The output subclass class variable: 12 superclass instance variable: 30

final keyword Declaring constants (used with variable and argument declaration) final int MAX=100; Disallowing method overriding (used with method declaration) final void show (final int x) Disallowing inheritance (used with class declaration). final class Demo {}

Abstract class Abstract classes are classes with a generic concept, not related to a specific class. Abstract classes define partial behaviour and leave the rest for the subclasses to provide. contain one or more abstract methods. abstract method contains no implementation, i.e. no body. Abstract classes cannot be instantiated, but they can have reference variable. If the subclasses does not override the abstract methods of the abstract class, then it is mandatory for the subclasses to tag itself as abstract.

Why create abstract methods? to force same name and signature pattern in all the subclasses subclasses should not use their own naming patterns They should have the flexibility to code these methods with their own specific requirements.

Example of Abstract class abstract class Animal { String name; String species; Animal(String n, String s) { name=n; species=s; } void eat(string fooditem) { System.out.println(species + + name + likes to have + fooditem); } abstract void sound(); }