Programming in Java, 2e Sachin Malhotra Saurabh Choudhary.

Slides:



Advertisements
Similar presentations
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.
Advertisements

Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 8 Inheritance and.
OOP: Inheritance By: Lamiaa Said.
1 Inheritance Chapter 9. 2 Module Outcomes To develop a subclass from a superclass through inheritance To invoke the superclass ’ s constructors and methods.
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.
1 l Inheritance Basics l Programming with Inheritance l Dynamic Binding and Polymorphism Inheritance.
Inheritance. Extending Classes It’s possible to create a class by using another as a starting point  i.e. Start with the original class then add methods,
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.
1 Chapter 6 Inheritance, Interfaces, and Abstract Classes.
Inheritance and Polymorphism Recitation – 10/(16,17)/2008 CS 180 Department of Computer Science, Purdue University.
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.
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 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.
Inheritance - Polymorphism ITI 1121 Nour El Kadri.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
1 Abstract Classes and Interfaces. 2 The abstract Modifier  The abstract class –Cannot be instantiated –Should be extended and implemented in subclasses.
Programming With Java ICS201 University Of Ha’il1 Chapter 7 Inheritance.
1 COSC2007 Data Structures II Chapter 9 Class Relationships.
CS 5JA Introduction to Java Pop Quiz Define/Explain encapsulation. In object-oriented programming, encapsulation refers to the grouping of data and the.
Chapter 11: Advanced Inheritance Concepts. Objectives Create and use abstract classes Use dynamic method binding Create arrays of subclass objects Use.
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
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.
Java Inheritance in Java. Inheritance Inheritance is a mechanism in which one object acquires all the properties and behaviors of parent object. The idea.
POLYMORPHISM Chapter 6. Chapter Polymorphism  Polymorphism concept  Abstract classes and methods  Method overriding  Concrete sub classes and.
COMPUTER SCIENCE & TECHNOLOGY DEGREE PROGRAMME FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UVA WELLASSA ‏ Properties of Object Oriented Programming.
BY:- TOPS Technologies
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Java Programming Fifth Edition Chapter 9 Introduction to Inheritance.
Inheritance.
Modern Programming Tools And Techniques-I
Inheritance Chapter 7 Inheritance Basics Programming with Inheritance
Chapter 15 Abstract Classes and Interfaces
Polymorphism.
Inheritance ITI1121 Nour El Kadri.
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.
Inheritance-Basics.
Final and Abstract Classes
Polymorphism, Abstract Classes & Interfaces
Inheritance and Polymorphism
One class is an extension of another.
Inheritance in Java.
INHERITANCE IN JAVA.
Week 8 Lecture -3 Inheritance and Polymorphism
Object Oriented Programming
Modern Programming Tools And Techniques-I Inheritance
One class is an extension of another.
Inheritance Basics Programming with Inheritance
More inheritance, Abstract Classes and Interfaces
Polymorphism and access control
METHOD OVERRIDING in JAVA
Computer Programming with JAVA
Java – Inheritance.
Inheritance Inheritance is a fundamental Object Oriented concept
Inheritance Cse 3rd year.
Java Inheritance.
Inheritance.
Chapter 14 Abstract Classes and Interfaces
Inheritance and Polymorphism
Chapter 11 Inheritance and Polymorphism Part 1
Final and Abstract Classes
Chapter 11 Inheritance and Encapsulation and Polymorphism
Topics OOP Review Inheritance Review Abstract Classes
Computer Science II for Majors
Inheritance and Polymorphism
Presentation transcript:

Programming in Java, 2e Sachin Malhotra Saurabh Choudhary

Chapter 3 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.

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.

Superclass Can Refer to a Subclass Object The real power of overriding is illustrated by the following code. A super class reference variable can be assigned a subclass object. E.g. in the example of method overriding we can create an object of class A by referring subclass B as follows, AClass a = new BClass(); a.doOverride();

super Keyword The “super” keyword refers to the parent class of the class in which the keyword is used. It is used for following three purposes, For calling 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.) { void show() class A { void show() System.out.println(“Super Class show method”); } class B extends A System.out.println(“Sub Class show method”); public static void main (String args[ ]) A s1=new A(); s1.show(); B s2=new B (); s2.show();

Problem and Solution o/p=> Super Class show method Sub Class show method Two methods show() and overridden show() are being called by two different objects A and B , instead the job can be done by one object only, i.e. using super keyword.

Case 1: Invoking Methods using super import java.io.*; class Anew { void show() System.out.println(“Super Class show method”); } class BNew extends Anew super.show(); System.out.println(“Sub Class show method”); public static void main (String args[ ]) BNew s2=new BNew (); s2.show();

OUTPUT => Super Class show method Sub Class show method

Case 2: Accessing variables using super import java.io.*; class Super_Variable { int b=30; } class Sub_Class 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[]) Sub_Class s=new Sub_Class(); s.show();

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

Case 3: constructors of the super class. import java.io.*; class Constructor_A { Constructor_A() System.out.println("Constructor A"); } class Constructor_B extends Constructor_A Constructor_B() System.out.println("Constructor B"); class Constructor_C extends Constructor_B Constructor_C() System.out.println("Constructor C"); public static void main (String args[]) Constructor_C c=new Constructor_C();

The Output Constructor A Constructor B Constructor C

Case 3: (contd.) import java.io.*; class Constructor_A_Revised { System.out.println("Constructor A Revised"); } class Constructor_B_Revised extends Constructor_A_Revised Constructor_B_Revised() System.out.println("Constructor B"); Constructor_B_Revised(int a) a++; System.out.println("Constructor B Revised "+ a ); class Constructor_C_Revised extends Constructor_B_Revised Constructor_C_Revised() super(11); System.out.println("Constructor C Revised"); public static void main (String args[]) Constructor_C_Revised a=new Constructor_C_Revised();

The Output Constructor A Revised Constructor B Revised 12 Constructor C Revised

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

Your Turn What is method overriding? Highlight the usage of super keyword? What are the usages of final keyword? What is late binding and early binding?

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.

Need of creating abstract method 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; void AnimalName(String n) name=n; System.out.println("Animal is "+name); } void AnimalEat(String fooditem) System.out.println(name + " likes to have "+ fooditem); abstract void AnimalSound(String sound); class Lion extends Animal void AnimalSound(String sound) System.out.println(name+" always "+sound); public static void main(String args[]) Lion a=new Lion(); a.AnimalName("Lion"); a.AnimalEat("meat"); a.AnimalSound("Roar");

Practical Problem: Circle class class Circle { float radius; final fl oat PI = 3.141f; //value of pi is fi xed Circle(){ radius = 1.0f;} Circle(fl oat radius) { this.radius = radius;} float getArea() { return PI * radius * radius; }}

Practical problem: Cylinder class class Cylinder extends Circle { float height; Cylinder(fl oat radius, fl oat height){ super(radius); this.height = height; } float getArea() { return 2 * super.getArea() + 2 * PI * radius * height; }

Practical problem public static void main(String args[]) { Circle c = new Circle(1.5f); System.out.println("The Area of Circle is: " + c.getArea()); Cylinder cy = new Cylinder(1.5f,3.5f); System.out.println("The Surface Area of Cylinder is: " + cy.getArea()); }}

Summary The concept of Inheritance is derived from real life. The properties and methods of a parent class are inherited by the children or subclasses. Subclasses can provide new implementation of method using method overriding, keeping the method names and signatures same as that of the parent class. The super keyword can be used to access the overridden methods, variables and even the constructors of the super class. Abstract classes are used to force the subclasses to override abstract methods and provide body and code for them. The final keyword is used to create constants and disallow inheritance.

Q.1 what will happen when you run the following code. class Demo { Demo(int i) System.out.println(“Demo”); } class InnerClass extends Demo public static void main(String args[]) InnerClass s=new InnerClass(); void InnerClass() System.out.println(“Inner Class”);

Compilation and output of string is “Inner Class ” at run time. Compile time error Compilation and no output at run time. Compilation and output of string is “Demo”