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.

Slides:



Advertisements
Similar presentations
Final and Abstract Classes
Advertisements

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.
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.
Inheritance Java permits you to use your user defined classes to create programs using inheritance.
Advanced Programming in Java
Sadegh Aliakbary Sharif University of Technology Fall 2010.
Interface & Abstract Class. Interface Definition All method in an interface are abstract methods. Methods are declared without the implementation part.
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.
Inheritance and Polymorphism Recitation – 10/(16,17)/2008 CS 180 Department of Computer Science, Purdue University.
Chapter 10 Classes Continued
8.1 Classes & Inheritance Inheritance Objects are created to model ‘things’ Sometimes, ‘things’ may be different, but still have many attributes.
OOP in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
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.
Object Oriented Programming: Java Edition By: Samuel Robinson.
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 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.
8. Inheritance “Is-a” Relationship. Topics Creating Subclasses Overriding Methods Class Hierarchies Abstract Class Inheritance and GUIs The Timer Class.
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.
Interfaces Chapter 9. 9 Creating Interfaces An interface is a contract. Every class that implements the interface must provide the interface’s defined.
Java Programming, Second Edition Chapter Twelve Advanced Inheritance Concepts.
CS 5JA Introduction to Java Pop Quiz Define/Explain encapsulation. In object-oriented programming, encapsulation refers to the grouping of data and the.
Classes, Interfaces and Packages
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
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.
1 C# - Inheritance and Polymorphism. 2 1.Inheritance 2.Implementing Inheritance in C# 3.Constructor calls in Inheritance 4.Protected Access Modifier 5.The.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
Author: DoanNX Time: 45’.  OOP concepts  OOP in Java.
POLYMORPHISM Chapter 6. Chapter Polymorphism  Polymorphism concept  Abstract classes and methods  Method overriding  Concrete sub classes and.
COP INTERMEDIATE JAVA Inheritance, Polymorphism, Interfaces.
COMPUTER SCIENCE & TECHNOLOGY DEGREE PROGRAMME FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UVA WELLASSA ‏ Properties of Object Oriented Programming.
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: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Java Programming Fifth Edition Chapter 9 Introduction to Inheritance.
Polymorphism in Methods
Programming in Java: lecture 7
Modern Programming Tools And Techniques-I
Advanced Programming in Java
Advanced Programming in Java
Sections Inheritance and Abstract Classes
Programming in Java, 2e Sachin Malhotra Saurabh Choudhary.
Inheritance-Basics.
Final and Abstract Classes
Inheritance and Polymorphism
Inheritance in Java.
INHERITANCE IN JAVA.
Object Oriented Programming
Modern Programming Tools And Techniques-I Inheritance
CS240: Advanced Programming Concepts
Java Programming Language
Abstract Classes AKEEL AHMED.
Interfaces.
Advanced Java Programming
METHOD OVERRIDING in JAVA
Abstract Classes Page
Java – Inheritance.
Inheritance Cse 3rd year.
Java Inheritance.
Advanced Programming in Java
Inheritance and Polymorphism
OOP Aga private institute for computer science 5th grade
Final and Abstract Classes
INTERFACES Explained By: Sarbjit Kaur. Lecturer, Department of Computer Application, PGG.C.G., Sector: 42, Chandigarh.
Presentation transcript:

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 as well as has some features its own. Syntax of inheritance of a class : Class class Name extends base class name { Additional methods Or Additional data members } Note: Inheritance provide code reusability and runtime polymorphism.

Note: constructor of a class is never inherited,in its subclass. Use of super keywords in Inheritance it is used to perform following function:- 1) To invoke super class constructor from a subclass constructor. Syntax: super(args if any); Note: super must be the first statement in a subclass constructor. 2) To invoke a super class method from a subclass method. Syntax: super.method(args if any); 3) To identify super class data members in case of name conflict between super and subclass data members.

If a subclass defines a method of the same signature as a method of its base class then subclass is said to be overriding then method of its super class method overriding and one of the means to implements polymorphism. methods are overridden to extends their functionality. (overriding is the powerful features of inheritance.) it reflect the runtime polymorphism.

Final Keyword It is used to perform following functions 1) to define naming constant as in C # define A 10; Final type identifier =expression; Or Final type identifier ; Identifier=expression; 2) to prevent the overriding of a method. Syntax: final return type method name(type args) { } 3) To prevent the inheritance from a class.

This keyword It is used to represents current object in the current method. Usage of this keyword 1) to identify data members of the object in case of a name conflict between object data members and local variable of the method. Syntax: this. member. 2) to pass current object in a method as arguments from the current method. 3)To invoke another constructor of the same class on the current object from a constructor i.e. to chain constructor of the class. Syntax: this(Args if any) Note: if this is used to invoke a constructor of the current class on current current object, then it must be the first statement in the class.

Reference variable of a base class can contains the reference of its subclass object. If a method call is made using the reference variable of base class and method is overridden in subclasses then method call is dynamically resolve on the basis of the type of object being reference resulting in runtime polymorphism r.p.m. is a facility to invoke different method from a single method call at different time.

Note: when reference variable of a base class is used to refer sub class object then only those member of subclasses can be referred by the super class reference variable which are part of its own class.

Abstract keyword It is used to define abstract class & method. An abstract method is a method without implementation it represents, what is to be done,without specifying,how it would be done. If a class contains an abstract method then class is being abstract. An abstract class is a class that has the following characteristic 1) it can’t be instantiated. 2) an abstract class imposes the responsibility of providing implementation of all its abstract method on its subclasses if any of its subclasses fails to provide implementation over a single method of its super class then sub class is also made abstract.

Interface An interface is a collection of implicit abstract method and static final data members. Concept of interface in java is used to abstract the interface of a class from its implementation. Syntax of defining an interface: Interface identifier { Method declarations; Static final data members ; } Note: interfaces are implemented in classes.

Syntax of implementing an interface Class classname implements interface { Public definition of all the methods of interface Additional data members of the class. } Note : if a class implements an interface then it has to provide public definition of all the methods of the interface. If a class fails to do so then class is declared as abstract. Interfaces support run time polymorphism i.e. reference variable of an interface can be used to refer objects of those classes that implements the interface.

An interface can not be instantiated (i.e. we can not create object) Difference between abstract class and interface 1)Degree of abstraction i) in case of abstract class may contain non abstract method and non static and non final data members. i) An interface can only contain abstract method and static final data members. 2) Type of inheritance i) classes(abstract and non abstract ) support implementation inheritance. In java multiple inheritance is not supported. i) interface support multiple interface inheritance.

3) classing environment i)classes( abstract and non abstract) support static classing environment i)interface support dynamic classing environment. Type of inheritance: 1) implementation inheritance 2) interface inheritance Purpose of inheritance : i) code reusability ii) run time polymorphism