Inheritance and Polymorphism

Slides:



Advertisements
Similar presentations
Inheritance. Many objects have a hierarchical relationship –Examples: zoo, car/vehicle, card game, airline reservation system Inheritance allows software.
Advertisements

Chapter 1 Inheritance University Of Ha’il.
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.
1 Chapter 6: Extending classes and Inheritance. 2 Basics of Inheritance One of the basic objectives of Inheritance is code reuse If you want to extend.
Objectives Introduction to Inheritance and Composition (Subclasses and SuperClasses) Overriding (and extending), and inheriting methods and constructors.
Java Inheritance. What is inherited A subclass inherits variables and methods from its superclass and all of its ancestors. The subclass can use these.
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. 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.
8.1 Classes & Inheritance Inheritance Objects are created to model ‘things’ Sometimes, ‘things’ may be different, but still have many attributes.
Chapter 7 - Generalization/Specialization and Inheritance1 Chapter 7 Generalization/Specialization and Inheritance.
Chapter 6 Class Inheritance F Superclasses and Subclasses F Keywords: super F Overriding methods F The Object Class F Modifiers: protected, final and abstract.
Specialization and Inheritance Chapter 8. 8 Specialization Specialized classes inherit the properties and methods of the parent or base class. A dog is.
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.
JAVA WORKSHOP SESSION – 3 PRESENTED BY JAYA RAO MTech(CSE) NEWTON’S INSTITUTE OF ENGINEERING 1.
OOP and Dynamic Method Binding Chapter 9. Object Oriented Programming Skipping most of this chapter Focus on 9.4, Dynamic method binding – Polymorphism.
Inheritance in the Java programming language J. W. Rider.
Object-Oriented Programming. An object is anything that can be represented by data in a computer’s memory and manipulated by a computer program.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
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.
© 2007 Lawrenceville Press Slide 1 Chapter 9 Inheritance  One class is an extension of another.  Allows a class to define a specialized type of an existing.
Inheritance and Polymorphism. Superclass and Subclass Inheritance defines a relationship between objects that share characteristics. It is a mechanism.
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
Object orientation and Packaging in Java Object Orientation and Packaging Introduction: After completing this chapter, you will be able to identify.
CSI 3125, Preliminaries, page 1 Inheritance. CSI 3125, Preliminaries, page 2 Inheritance Using inheritance, can create a general class that defines traits.
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)
COP INTERMEDIATE JAVA Inheritance, Polymorphism, Interfaces.
COMPUTER SCIENCE & TECHNOLOGY DEGREE PROGRAMME FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UVA WELLASSA ‏ Properties of Object Oriented Programming.
Geoff Holmes and Bernhard Pfahringer COMP206-08S General Programming 2.
Inheritance. Inheritance is a mechanism in which one object acquires all the properties and behaviors of parent object Inheritance represents the IS-A.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Java Programming Fifth Edition Chapter 9 Introduction to Inheritance.
Java Interfaces CS 21a: Introduction to Computing I Department of Information Systems and Computer Science Ateneo de Manila University (see Chapter 9 of.
Class Inheritance Part II: Overriding and Polymorphism Corresponds with Chapter 10.
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Polymorphism in Methods
Modern Programming Tools And Techniques-I
Sections Inheritance and Abstract Classes
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.
Objects as a programming concept
Inheritance and Polymorphism
One class is an extension of another.
Inheritance in Java.
Road Map Inheritance Class hierarchy Overriding methods Constructors
Object Oriented Programming
Modern Programming Tools And Techniques-I Inheritance
Object Oriented Analysis and Design
Chapter 10 Thinking in Objects
One class is an extension of another.
Inheritance Basics Programming with Inheritance
Sampath Kumar S Assistant Professor, SECE
Interfaces.
METHOD OVERRIDING in JAVA
Abstract Classes Page
Computer Programming with JAVA
Java – Inheritance.
Inheritance Cse 3rd year.
Java Programming, Second Edition
Java Inheritance.
Inheritance.
Chapter 9 Carrano Chapter 10 Small Java
Chapter 14 Abstract Classes and Interfaces
Chapter 8 Class Inheritance and Interfaces
Chapter 11 Inheritance and Polymorphism Part 1
Final and Abstract Classes
Presentation transcript:

Inheritance and Polymorphism Chapter No. : 3 Inheritance and Polymorphism

Topic Learning Outcomes Explain inheritance and polymorphism. Explain different types of inheritance supported by java. Apply method overriding and dynamic method dispatch. Identify and create abstract classes to solve a given scenario. School of Computer Science & Engineering

School of Computer Science & Engineering Content Inheritance: Basics Usage of super key word Method overriding Dynamic method dispatch Abstract classes Object class School of Computer Science & Engineering

School of Computer Science & Engineering Inheritance: Basics Inheritance in java is a mechanism in which one object acquires all the properties and behaviours of parent object. Inheritance represents the IS-A relationship, also known as parent-child relationship. Advantages of Inheritance. For Method Overriding (so runtime polymorphism can be achieved). For Code Reusability. School of Computer Science & Engineering

School of Computer Science & Engineering Inheritance: Basics Syntax : class <SubclassName> extends <SuperClassName>{ // body of a class } Example : class Vehicle extends TwoWheeler{ School of Computer Science & Engineering

School of Computer Science & Engineering Inheritance: Basics Types of Inheritance : School of Computer Science & Engineering

School of Computer Science & Engineering Inheritance: Basics Example School of Computer Science & Engineering

School of Computer Science & Engineering Inheritance: Basics Example School of Computer Science & Engineering

School of Computer Science & Engineering Inheritance: Basics Example School of Computer Science & Engineering

Usage of “super” keyword Using super to Call Superclass Constructors. Using super to acces Superclass members School of Computer Science & Engineering

School of Computer Science & Engineering IS-A Relationship School of Computer Science & Engineering

When Constructors are called School of Computer Science & Engineering

School of Computer Science & Engineering Method Overriding If subclass (child class) has the same method as declared in the parent class, it is known as method overriding in java. Usage : Method overriding is used to provide specific implementation of a method that is already provided by its super class. Method overriding is used for runtime polymorphism. Rules : method must have same name as in the parent class method must have same parameter as in the parent class. must be IS-A relationship (inheritance). School of Computer Science & Engineering

School of Computer Science & Engineering Method Overriding Example : School of Computer Science & Engineering

School of Computer Science & Engineering Method Overriding Example : School of Computer Science & Engineering

School of Computer Science & Engineering Method Overriding Example : School of Computer Science & Engineering

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. School of Computer Science & Engineering

Dynamic Method Dispatch Example : School of Computer Science & Engineering

School of Computer Science & Engineering Abstract Classes Abstraction is a process of hiding the implementation details and showing only functionality to the user. Another way, it shows only important things to the user and hides the internal details for example sending sms, you just type the text and send the message. You don't know the internal processing about the message delivery. Abstraction lets you focus on what the object does instead of how it does it. School of Computer Science & Engineering

School of Computer Science & Engineering Abstract Classes Summary : Abstract classes may or may not contain abstract methods i.e., methods with out body ( public void get(); ) But, if a class have at least one abstract method, then the class mustbe declared abstract. If a class is declared abstract it cannot be instantiated. To use an abstract class you have to inherit it from another class, provide implementations to the abstract methods in it. If you inherit an abstract class you have to provide implementations to all the abstract methods in it. School of Computer Science & Engineering

Abstract Classes A class that is declared as abstract is known as abstract class. It needs to be extended and its method implemented. It cannot be instantiated. abstract class A{}   A method that is declared as abstract and does not have implementation is known as abstract method. abstract void printStatus(); //no body and abstract School of Computer Science & Engineering

School of Computer Science & Engineering Abstract Classes Example : School of Computer Science & Engineering

School of Computer Science & Engineering Object Class There is one special class defined by Java. All other classes are subclasses of Object. That is, Object is a superclass of all other classes. This means that a reference variable of type Object can refer to an object of any other class. Also, since arrays are implemented as classes, a variable of type Object can also refer to any array. School of Computer Science & Engineering

School of Computer Science & Engineering Object Class Object defines the following methods, which means that they are available in every object. School of Computer Science & Engineering