Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.

Slides:



Advertisements
Similar presentations
Final and Abstract Classes
Advertisements

Lecture 5: Interfaces.
OO Programming in Java Objectives for today: Overriding the toString() method Polymorphism & Dynamic Binding Interfaces Packages and Class Path.
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.
Inheritance Inheritance Reserved word protected Reserved word super
Inheritance Java permits you to use your user defined classes to create programs using inheritance.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Inheritance and Polymorphism.
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.
Inheritance. In this chapter, we will cover: The concept of inheritance Extending classes Overriding superclass methods Working with superclasses that.
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
June 1, 2000 Object Oriented Programming in Java (95-707) Java Language Basics 1 Lecture 3 Object Oriented Programming in Java Language Basics Classes,
1 Chapter 6 Inheritance, Interfaces, and Abstract Classes.
Inheritance and Polymorphism Recitation – 10/(16,17)/2008 CS 180 Department of Computer Science, Purdue University.
1 Inheritance and Polymorphism. 2 Motivations Suppose you will define classes to model circles, rectangles, and triangles. These classes have many common.
Chapter 10 Classes Continued
OOP in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Programming Languages and Paradigms Object-Oriented Programming.
Chapter 6 Class Inheritance F Superclasses and Subclasses F Keywords: super F Overriding methods F The Object Class F Modifiers: protected, final and abstract.
CISC6795: Spring Object-Oriented Programming: Polymorphism.
CSM-Java Programming-I Spring,2005 Objects and Classes Overview Lesson - 1.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
Inheritance and Class Hierarchies Ellen Walker CPSC 201 Data Structures Hiram College.
What is inheritance? It is the ability to create a new class from an existing class.
Inheritance. Introduction Inheritance is one of the cornerstones of object-oriented programming because it allows the creation of hierarchical classifications.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 11 Inheritance and Polymorphism.
 2002 Prentice Hall. All rights reserved. 1 Introduction to Inheritance Inheritance: –1 of 3 main features of OOP –Form of software reusability –(Derived)
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.
Summing Up Object Oriented Design. Four Major Components: Abstraction modeling real-life entities by essential information only Encapsulation clustering.
Introduction to Java Chapter 7 - Classes & Object-oriented Programming1 Chapter 7 Classes and Object-Oriented Programming.
Chapter 5 Objects and Classes Inheritance. Solution Assignments 3 & 4 Review in class…..
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.
Object Oriented Programming
Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
Interfaces F What is an Interface? F Creating an Interface F Implementing an Interface F What is Marker Interface?
Object Oriented programming Instructor: Dr. Essam H. Houssein.
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.
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
Programming in java Packages Access Protection Importing packages Java program structure Interfaces Why interface Defining interface Accessing impln thru.
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.
1 Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
1 Object-Oriented Programming Inheritance. 2 Superclasses and Subclasses Superclasses and Subclasses  Superclasses and subclasses Object of one class.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
Author: DoanNX Time: 45’.  OOP concepts  OOP in Java.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Outline Creating Subclasses Overriding Methods Class Hierarchies Inheritance.
 Description of Inheritance  Base Class Object  Subclass, Subtype, and Substitutability  Forms of Inheritance  Modifiers and Inheritance  The Benefits.
COMPUTER SCIENCE & TECHNOLOGY DEGREE PROGRAMME FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UVA WELLASSA ‏ Properties of Object Oriented Programming.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Modern Programming Tools And Techniques-I
Lecture 12 Inheritance.
Inheritance-Basics.
Inheritance and Polymorphism
Object Oriented Programming
Extending Classes.
Java Programming Language
Packages and Interfaces
Java – Inheritance.
Inheritance Inheritance is a fundamental Object Oriented concept
Java Inheritance.
Inheritance.
Final and Abstract Classes
Presentation transcript:

Inheritance

Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and – you add new methods and fields to adapt your new class to new situations. Inheritance is a mechanism through which a class can be defined in terms of an existing class. It supports the concept of hierarchical classification Employee class :- manager is an employee – “is- a” r/ship is the hallmark of inheritance.

Superclass & subclass

An Example

In terminology of Java, a class that is inherited is called a superclass. The class that does the inheriting is called a subclass Superclass & subclass

An Example To define a subclass from a superclass – use extends keyword in Java. E.g class Manager extends Employee { Added methods and fields }

Types of inheritance Single Inheritance Multiple Inheritance Multilevel Inheritance Hybrid Inheritance

Inheritance – So far A fundamental mechanisms for code reuse in OOP- inheritance. It allows new classes to be derived from an existing class. The new class (subclass, subtype, derived class, child class) can inherit members from the old class (superclass, supertype, base class, parent class). The subclass can add new behavior and properties, and under certain circumstances, modify its inherited behavior.

Inheritance The keyword extends indicates that you are making a new class that derives from an existing class. Subclasses have more functionalities than their superclasses. Add new methods to subclasses. class Manager extends Employee { private double bonus; //added feature to Manager class public void setBonus(double b) { bonus = b; }

Member access Inheritance of members is closely tied to their declared accessibility. Private members of the superclass are not inherited by the subclass and can only be indirectly accessed. but they exist in the subclass object. the subclass can use the inherited members as if they were declared in its own class body.

Accessibility of data members A subclass in includes all of the members of its superclass, it cant access those members of the superclass that have been declared as private. All inheritance in Java is public inheritance. All the public members and protected members are inherited as public and protected respectively.

A Scenario class A { int a; int getA() { return a; } void setA(int v) { a=v; } class B extends A { int b; int getB() { return b; } void setB(int v) { b=v; }

A Scenario A objA=new A(); B objB= new B(); a getA() setA(int v) a b getA() setA(int v) getB() setB(int v)

Inheritance in Java A class in Java can only extend one other class; that is, it can only have one immediate superclass.

Method overriding When designing classes, you place the most general methods into the superclass, and more specialized methods in the subclass. The getSalary method should return the sum of the base salary and the bonus.

Method overriding You need to supply a new method to override the superclass method A subclass can add fields, and it can add or override methods of the superclass. However, inheritance can never take away any fields or methods

The getSalary method of the Manager class has no direct access to the private fields of the superclass public double getSalary() { return salary + bonus; // won't work } We need to indicate that we want to call the getSalary method of the Employee superclass, not the current class. You use the special keyword super for this purpose: The call super.getSalary() calls the getSalary method of the Employee class. Method overriding

Here is the correct version of the getSalary method for the Manager class: public double getSalary() { double baseSalary = super.getSalary(); return baseSalary + bonus; } Method overriding

The super keyword super is a special keyword that directs the compiler to invoke the superclass method.

Constructor call Constructors are called in order of derivation, from superclass to subclass

Super super has two general forms:- – the first calls the superclass’ constructor – the second is used to access a member of the superclass that has been hidden by a member of a subclass. The instruction super(n, s, year, month, day); is shorthand for “call the constructor of the Employee superclass with n, s, year, month, and day as parameters.”

super Since the Manager constructor cannot access the private fields of the Employee class, it must initialize them through a constructor. The constructor is invoked with the special super syntax. The call using super must be the first statement in the constructor for the subclass. the super keyword has two meanings: – to invoke a superclass method, and – to invoke a superclass constructor.

Preventing Inheritance Final Classes & Methods Classes that cannot be extended are called final classes, and you use the final modifier in the definition of the class to indicate this. You can also make a specific method in a class final. If you do this, then no subclass can override that method.

final keyword Using final with inheritance – final has three uses:- it can be used to create the equivalent of a named constant Using final to prevent overriding Using final to prevent inheritance

Abstract classes A superclass that declares the structure of a given abstraction without providing a complete implementation of every method. A superclass that only defines a generalized form that will be shared by all its subclasses, leaving it to each subclass to fill in the details. Such a class determines the nature of the methods that the subclasses must implement. Shape– area() ; Triangle – its own implementation of area(), square, circle…

Abstract classes To require that certain methods be overridden by the subclasses – use abstract type modifier Subclass’ responsibility – as no implementation in superclass. Any class that contains one or more abstract method must also be declared as abstract. You cannot declare abstract constructors or abstract static method. Although abstract classes cannot be used to instantiate objects, they can be used to create object references

Abstract Classes Abstract classes can have concrete data and methods. Abstract classes cannot be instantiated. That is, if a class is declared as abstract, no objects of that class can be created. Note that you can still create object variables of an abstract class, but such a variable must refer to an object of a non-abstract subclass and subclass of the latter.

Runtime Polymorphism – Dynamic method dispatch Method overriding forms the basis :- Dynamic method dispatch. It’s a mechanism by which a call to an overridden method is resolved at run-time, rather than compile time.

Runtime Polymorphism – Dynamic method dispatch Its important because this is how Java implements run-time polymorphism. When an overridden method is called through a superclass reference, Java determines which version of that method to execute based on the type of the object being referred to at the time the call occurs. Thus, this determination is made at run time Dynamic method lookup is the process of determining which method definition a method signature denotes during runtime, based on the type of the object.

Object: The Cosmic Superclass The Object class is the ultimate ancestor—every class in Java extends Object. The ultimate superclass Object is taken for granted if no superclass is explicitly mentioned. Because every class in Java extends Object, it is important to be familiar with the services provided by the Object class. In Java, only the primitive types (numbers, characters and boolean values) are not objects. All array types are class types that extend the Object class.

The equals and toString methods The equals method in the Object class tests whether or not one object is equal to another. If you want to test objects for equality, you will need to override equals for a more meaningful comparison. Similarly need to override toString() method. Another important method in Object is the toString method that returns a string that represents the value of this object. Almost any class will override this method to give you a printed representation of the object's current state.

finalize() protected void finalize() Called by the garbage collector on an object when garbage collection determines that there are no more references to the object. A subclass overrides the finalize method to dispose of system resources or to perform other cleanup.

Why no multiple inheritance in Java? instanceof keyword. Explore the Object class

Interfaces Using the keyword interface, you can fully abstract a class' interface from its implementation. Using interface you specify what a class must do, but not how it does. Syntactically similar to classes, but lack instance variables and methods are abstract and public. To implement an interface, a class must create the complete set of methods defined by the interface

Defining an interface interface name {..... }

Defining an interface default or public access specifier. public - it shud be the only public i/f - same file name Each class that includes an interface must implement all of the methods Variables can be declared inside interface declarations - but implicitly are final and static - they cannot be changed by the implementing class - they must also be initialized. All methods are implicity public.

Implementing an interface. One or more classes can implement an interface. class C implements i/fname,... { } The methods that implement an interface must be declared public. To make a class implement an interface, two steps:- – You declare that your class intends to implement the given interface – You supply the definitions for all the methods in the interface. Interfaces can be extended - using the extends keyword

More on interfaces Interface are not classes. You can never use new operator to instantiate an interface - cant construct interface objects, you can still declare interface variables. An interface variable must refer to an object of a class that implements the interface. Why interfaces why not abstract classes?

Packages

A package in Java is an encapsulation mechanism that can be used to group related classes, interfaces, and subpackages. A package hierarchy represents an organization of the Java classes and interfaces. It does not represent the source code organization of the classes and interfaces.

Packages Each Java source file (also called compilation unit) can contain zero or more definitions of classes and interfaces, but the compiler produces a separate class file containing the Java byte code for each of them. A class or interface can indicate that its Java byte code be placed in a particular package, using a package declaration. The package statement has the following syntax: package ; A class can use all classes from its own package and all public classes from other packages.

Two schemes First, all the classes and interfaces in a source file will be placed in the same package. Secondly, several source files can be used to specify the contents of a package.

Defining a package Simply include a package command as the first statement in a Java source file. The package statement defines a namespace in which classes are stored. If not mentioned – class names are put to default package. E.g:- package MyPackage (creates a package called MyPackage) Java uses file system directories to store packages. The package statement simply specifies to which package the classes defined belong to. A package hierarchy must reflect to the file system of Java development system.

CLASSPATH Packages are mirrored by directories How does Java run-time system know where to look for packages that you create? First the JRE – uses the current working directory. Then in the paths set in the CLASSPATH environment variable Or in the –classpath option in the javac tool.

CLASSPATH CLASSPATH Variables:- – Java compiler and interpreter searches the.class files in the path specified in the CLASSPATH variable. – The current working directory is by default included in this variable.

Access Protection