Objectives Introduction to Inheritance and Composition (Subclasses and SuperClasses) Overriding (and extending), and inheriting methods and constructors.

Slides:



Advertisements
Similar presentations
Final and Abstract Classes
Advertisements

Introduction to Java 2 Programming
Object Oriented Programming
INHERITANCE BASICS Reusability is achieved by INHERITANCE
Module 8 “Polymorphism and Inheritance”. Outline Understanding Inheritance Inheritance Diagrams Constructors in Derived Classes Type Compatibility Polymorphism.
OOP: Inheritance By: Lamiaa Said.
Week 8 Recap CSE 115 Spring Composite Revisited Once we create a composite object, it can itself refer to composites. Once we create a composite.
Inheritance Java permits you to use your user defined classes to create programs using inheritance.
ITEC200 – Week03 Inheritance and Class Hierarchies.
CS 106 Introduction to Computer Science I 11 / 26 / 2007 Instructor: Michael Eckmann.
Object-Oriented PHP (1)
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Encapsulation, Inheritance & Interfaces CSE 115 Spring 2006 February 27, March 1 & 3, 2006.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
UML Class Diagram: class Rectangle
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter N - 1 Chapter 13 Polymorphism is-a relationships Interfaces.
Inheritance Review/Recap. ClassA extends ClassB ClassA now inherits (can access and use) all public and protected elements of ClassB We can expect the.
8.1 Classes & Inheritance Inheritance Objects are created to model ‘things’ Sometimes, ‘things’ may be different, but still have many attributes.
Chapter 11: Inheritance and Polymorphism Java Programming: Program Design Including Data Structures Program Design Including Data Structures.
Inheritance using Java
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.
Chapter 11 Inheritance and Composition. Chapter Objectives Learn about inheritance Learn about subclasses and superclasses Explore how to override the.
Inheritance and Class Hierarchies Ellen Walker CPSC 201 Data Structures Hiram College.
1 Java Inheritance. 2 Inheritance On the surface, inheritance is a code re-use issue. –we can extend code that is already written in a manageable manner.
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.
Specialization and Inheritance Chapter 8. 8 Specialization Specialized classes inherit the properties and methods of the parent or base class. A dog is.
Chris Kiekintveld CS 2401 (Fall 2010) Elementary Data Structures and Algorithms Inheritance and Polymorphism.
Structured Programming Good for programming in the small Often doesn't scale up Limitations –Changes at top may affect lower-level algorithms –Code reuse.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Inheritance and Access Control CS 162 (Summer 2009)
Inheritance Objectives: Creating new classes from existing classes The protected modifier Creating class hierarchies Abstract classes Indirect visibility.
Programming With Java ICS201 University Of Ha’il1 Chapter 7 Inheritance.
Object Oriented Programming
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,
Coming up: Inheritance
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.
Classes, Interfaces and Packages
AD Lecture #1 Object Oriented Programming Three Main Principles 1 Inheritance Encapsulation Polymorphism.
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.
COMPUTER SCIENCE & TECHNOLOGY DEGREE PROGRAMME FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UVA WELLASSA ‏ Properties of Object Oriented Programming.
SUBCLASSES - JAVA. The Purpose of Subclasses Class Farm String getOwner() void setOwner(String s) int getSize() void setSize(int s) Class DairyFarm String.
1 Inheritance One of the goals of object oriented programming is code reuse. Inheritance is one mechanism for accomplishing code reuse. It allows us to.
Geoff Holmes and Bernhard Pfahringer COMP206-08S General Programming 2.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Java Interfaces CS 21a: Introduction to Computing I Department of Information Systems and Computer Science Ateneo de Manila University (see Chapter 9 of.
These materials where developed by Martin Schray
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Modern Programming Tools And Techniques-I
OOP: Encapsulation &Abstraction
7.1 What Is An Object Object-oriented program - Description or simulation of application Object-oriented programming is done by adopting or extending an.
Final and Abstract Classes
Chapter 11: Inheritance and Polymorphism
Road Map Inheritance Class hierarchy Overriding methods Constructors
UML Class Diagram: class Rectangle
Object Oriented Analysis and Design
Inheritance, Polymorphism, and Interfaces. Oh My
OBJECT ORIENTED PROGRAMMING II LECTURE 8 GEORGE KOUTSOGIANNAKIS
Java Programming Language
Inheritance, Polymorphism, and Interfaces. Oh My
Interfaces.
Advanced Java Programming
Java Inheritance.
Final and Abstract Classes
Chapter 11 Inheritance and Encapsulation and Polymorphism
INTERFACES Explained By: Sarbjit Kaur. Lecturer, Department of Computer Application, PGG.C.G., Sector: 42, Chandigarh.
Presentation transcript:

Objectives Introduction to Inheritance and Composition (Subclasses and SuperClasses) Overriding (and extending), and inheriting methods and constructors from the SuperClass of an inherited class Understand what an interface class is and how to use it Introduction to abstract classes

Encapsulation Combine data and operations in a single unit known as a class The class is a self-contained entity. The internal state of an object cannot be manipulated directly

Inheritance and Composition When a new class is defined it may be a special case, or a generalization of an existing class Inheritance (is a) and composition (has a) can then be used to build a class that has the properties ( elements, methods) of the existing class with additions and/or changes.

Inheritance The object created by and Inherited class “IS A” object that could be considered as an incidence of the super class (or base class) A dog “IS A” pet A square “IS A” rectangle BUT a wall “HAS A” rectangle (composition, we will revisit this later)

Inheritance Heirarchy Superclass or base class Subclass or derived class Shape Square RectangleCircle

Inheritance in Java To implement inheritance public class Square extends Rectangle { // implements class Square }

Why Inherit? The base class already has elements constructors and methods to work with the object created by the class The inherited class will automatically have the default constructor, and the public or protected elements and methods of the super class NOTE: private elements and methods are not inherited. The class can then be extended by adding or overriding methods or adding variable elements

protected What is the meaning of protected Protected –Protected elements can be accessed by inherited classes –Protected elements cannot be accessed outside the base class and it’s inherited classes

Types of Inheritance Java supports single inheritance, that is inheritance from one class Some other languages also support multiple inheritance (inheriting from more than one class)

Inheritance: constructors Your derived class will automatically inherit the default constructor from your base class Other constructors must be included in your derived class You can efficiently add to the constructors (including the default constructor) in the base class by implementing them in your derived class using super()

Using super() to extend Constructors If you want the constructor in your derived class to do everything the constructor of your base class does, and a few extras, the constructor in your derived class should look like public derivedClassName( parameter list ) { super( parameter list ); // added functionality if any } For the default constructor the parameter list will be empty

super() and parameters When we build a constructor in an inherited class it can have –The same number and kind of parameters as a constructor in the base class. Extend using super(parameter list) and the same parameter list as the constructor in the base class –A different number or kind of parameters as a constructor in the base class This is a case of overloading. We have increased the overloading options for the constructors. The new overloaded constructor will be available to the derived class and not the base class

super() and private members Inheritance does not allow direct access to private members of the base class private members of the base class need to be initialized by the constructors of the derived classes super() allows the base class to initialize its private members, without providing the derived class direct access to them

Inheritance and Methods The public methods of the base class can be directly used on objects of the derived class. Any of the public methods in the base class can be extended, overridden, or overloaded in the derived class Additional methods can be added to the derived class that are not part of the base class

Methods and the use of super In the derived class a method to be overridden or overloaded has the following syntax public methodtype methodname( parameter list1 ) { super.methodname( parameter list2 ); // added functionality if any } Parameter list2 must always match the parameter list for an instance of this method in the base class To override (extend) a method from the base class the parameter list1 must match a parameter list of the method in the superclass. To overload parameter list1 does not match the parameter list in any occurrence of the method in the base class

Abstract Methods An abstract method is a method that –Specifies a method name type and parameter list –Provides no code to implement the method –For example: public void abstract print() Public abstract insert(int insertItem)

Abstract Classes Can contain –instance variables –Constructors – abstract and non abstract methods Any class containing an abstract method must be an abstract class You cannot declare an instance of an abstract class You can declare an instance of a class inherited from an abstract class ONLY if all the abstract methods in the abstract class are implemented in the derived class

Interfaces An interface is a class that contains only abstract methods and/or named constants Java does not support multiple inheritance, A derived class can inherit from only one base class A class can use more than one interface. A class can inherit from a single class and use interfaces

Use of an interface In Java a class implements an interface Within the class implementing the interface all methods in the interface must be defined public class ClassName implements InterfaceName { //Define variable elements of class // Constructors for class ClassName //code defining each of the methods in the interface // Any other methods for class ClassName }