Modern Programming Tools And Techniques-I

Slides:



Advertisements
Similar presentations
INHERITANCE BASICS Reusability is achieved by INHERITANCE
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.
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.
CS 211 Inheritance AAA.
Inheritance Inheritance Reserved word protected Reserved word super
Objectives Introduction to Inheritance and Composition (Subclasses and SuperClasses) Overriding (and extending), and inheriting methods and constructors.
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.
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.
“is a”  Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class.
220 FINAL TEST REVIEW SESSION Omar Abdelwahab. INHERITANCE AND POLYMORPHISM Suppose you have a class FunClass with public methods show, tell, and smile.
(C) 2010 Pearson Education, Inc. All rights reserved. Java™ How to Program, 8/e.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
Programming With Java ICS201 University Of Ha’il1 Chapter 8 Polymorphism and Abstract Classes.
Inheritance. Introduction Inheritance is one of the cornerstones of object-oriented programming because it allows the creation of hierarchical classifications.
Inheritance and Polymorphism Daniel Liang, Introduction to Java Programming.
JAVA WORKSHOP SESSION – 3 PRESENTED BY JAYA RAO MTech(CSE) NEWTON’S INSTITUTE OF ENGINEERING 1.
Inheritance in the Java programming language J. W. Rider.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
Programming in Java CSCI-2220 Object Oriented Programming.
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.
Sadegh Aliakbary Sharif University of Technology Fall 2010.
Sadegh Aliakbary Sharif University of Technology Spring 2011.
Programming With Java ICS201 University Of Ha’il1 Chapter 7 Inheritance.
Coming up: Inheritance
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.
Peyman Dodangeh Sharif University of Technology Fall 2014.
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.
Inheritance ndex.html ndex.htmland “Java.
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.
JAVA ACCESS MODIFIERS. Access Modifiers Access modifiers control which classes may use a feature. A classes features are: - The class itself - Its member.
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,
Chapter 15 Abstract Classes and Interfaces
Advanced Programming in Java
Lecture 12 Inheritance.
Inheritance-Basics.
Inheritance and Polymorphism
Inheritance in Java.
Review Session.
Week 8 Lecture -3 Inheritance and Polymorphism
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
Modern Programming Tools And Techniques-I Inheritance
Inheritance, Polymorphism, and Interfaces. Oh My
Extending Classes.
Java Programming Language
Chapter 9: Polymorphism and Inheritance
Advanced Java Topics Chapter 9
Advanced Programming Behnam Hatami Fall 2017.
METHOD OVERRIDING in JAVA
Java – Inheritance.
Inheritance Inheritance is a fundamental Object Oriented concept
Inheritance Cse 3rd year.
Java Inheritance.
Inheritance and Polymorphism
Final and Abstract Classes
Chapter 11 Inheritance and Encapsulation and Polymorphism
មជ្ឈមណ្ឌលកូរ៉េ សហ្វវែរ អេច អ ឌី
Advanced Programming in Java
Computer Science II for Majors
Presentation transcript:

Modern Programming Tools And Techniques-I Lecture 8: Inheritance

Introduction Object-oriented programming allows classes to inherit commonly used state and behavior from other classes. In the Java programming language, each class is allowed to have one direct super-class, and each super- class has the potential for an unlimited number of subclasses. Inheritance is a fundamental object-oriented design technique used to create and organize reusable classes.

Inheritance Inheritance defines an is-a relationship between a super-class and its subclasses. It means that the subclass (child) is a more specific version of the super-class (parent). An object of a subclass can be used wherever an object of the super-class can be used. Inheritance is used to build new classes from existing classes. The inheritance relationship is transitive: if class y extends class x, and a class z extends class y, then z will also inherit from class x.

Inheritance Using inheritance, we can create a general class that defines traits(state and behaviors) 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. In Java, a class that is inherited is called a super-class. The class that does the inheriting is called a subclass. Therefore, a subclass is a specialized version of a super-class. It inherits all of the instance variables and methods defined by the super-class and adds its own, unique elements.

Types of Inheritance The following kinds of inheritance are there in java. Simple Inheritance Multilevel Inheritance Simple Inheritance: A  subclass is derived simply from it's parent class. There is only a sub class and it's parent class. It is also called single inheritance or one level inheritance. Multi-level Inheritance: A subclass is derived from a derived class.

Example (Simple inheritance)

Multiple Inheritance The mechanism of inheriting the features of more than one base class into a single class is known as multiple inheritance. Java does not support multiple inheritance using classes but the multiple inheritance can be achieved by using the interface. In Java Multiple Inheritance can be achieved through use of Interfaces by implementing more than one interfaces in a class.

Inheritance Example class Shape { int area(){…} } class Rectangle extends Shape{ int area() { area = length * width;} int length; int width; class Square extends Rectangle { int area() {…} int length; int width = length;

Accessing parent class members

Example (access parent member)

Example (access parent member)

Abstract Class and Abstract method

Abstract Class and method An abstract class is a class that is declared abstract. An abstract method is a method that is declared without an implementation (without braces, and followed by a semicolon), like this: abstract void moveTo(double deltaX, double deltaY); Abstract class may or may not include abstract methods. Abstract classes cannot be instantiated, but they can be sub- classed.

If a class includes abstract methods, the class itself must be declared abstract, as in: public abstract class GraphicObject { // declare fields // declare non-abstract methods abstract void draw(); } When an abstract class is sub-classed, the sub-class usually provides implementations for all of the abstract methods in its parent class. However, if it does not, the sub-class must also be declared abstract.

Example (abstract class)

Example (abstract class)

Example (abstract method)

Functionality of constructors in Inheritance

Example (constructor calling in inheritance)

Example (constructor calling in inheritance)

Role of access specifiers in Inheritance

Key Points Private members of the super-class are not inherited by the subclass. Members that have default accessibility in the super- class are also not inherited by subclasses in other packages. Constructors and initializer blocks are not inherited by a subclass. A subclass can extend only one super-class.

Example (access specifiers in same package)

Example (access specifiers in same package)

Method overloading in Inheritance

Example (method overloading)

Method overriding

Method Overriding Method overriding means having a different implementation of the same method in the inherited class. These two methods would have the same signature, but different implementation. One of these would exist in the base class and another in the derived class. These cannot exist in the same class. Note:- A class declared as final cannot be inherited and a method declared final cannot be overridden in sub-class.

Key Points The version of a method that is executed will be determined by the object that is used to invoke it. If an object of a parent class is used to invoke the method, then the version in the parent class will be executed. If an object of the subclass is used to invoke the method, then the version in the child class will be executed.

Example (method overriding)

Example (accessing parent method)

Difference b/w Overloading & Overriding

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.

Example 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 C extends A { void callme() { System.out.println("Inside C'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     C c = new C();  // object of type C      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()      r = c;  // r refers to a C object      r.callme();  // calls C's version of callme()    } }

Example

Role of instanceof operator

instanceof The java instanceof operator is used to test whether the object is an instance of the specified type (class or subclass or interface). The instanceof in java is also known as type comparison operator because it compares the instance with type. It returns either true or false. If we apply the instanceof operator with any variable that has null value, it returns false.

An object of subclass type is also a type of parent class An object of subclass type is also a type of parent class. For example, if Dog extends Animal then object of Dog can be referred by either Dog or Animal class.

Downcasting with java instanceof operator When subclass type refers to the object of Parent class, it is known as downcasting. If we perform it directly, compiler gives Compilation error. If you perform it by typecasting, ClassCastException is thrown at runtime.

Possibility of downcasting with instanceof

Downcasting to different classes with instanceof

Role of static methods in Inheritance

Static methods in inheritance

Example (static method in inheritance)

Key points

Example (accessibility)