COP 3331 Object Oriented Analysis and Design Chapter 5 – Classes and Inheritance Jean Muhammad.

Slides:



Advertisements
Similar presentations
Final and Abstract Classes
Advertisements

Object Oriented Programming with Java
Chapter 1 Object-Oriented Concepts. A class consists of variables called fields together with functions called methods that act on those fields.
Chapter 1 Inheritance University Of Ha’il.
OOP: Inheritance By: Lamiaa Said.
George Blank University Lecturer.
Comp 249 Programming Methodology Chapter 7 - Inheritance – Part A Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia University,
ITEC200 – Week03 Inheritance and Class Hierarchies.
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.
Encapsulation, Inheritance & Interfaces CSE 115 Spring 2006 February 27, March 1 & 3, 2006.
1 Chapter 6 Inheritance, Interfaces, and Abstract Classes.
Evan Korth New York University Computer Science I Classes and Objects Professor: Evan Korth New York University.
Inheritance and Polymorphism Recitation – 10/(16,17)/2008 CS 180 Department of Computer Science, Purdue University.
1 Further OO Concepts II – Java Program at run-time Overview l Steps in Executing a Java Program. l Loading l Linking l Initialization l Creation of Objects.
1 Inheritance in Java CS 3331 Fall Outline  Overloading  Inheritance and object initialization  Subtyping  Overriding  Hiding.
OOPs Object oriented programming. Based on ADT principles  Representation of type and operations in a single unit  Available for other units to create.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
Specialization and Inheritance Chapter 8. 8 Specialization Specialized classes inherit the properties and methods of the parent or base class. A dog is.
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.
Object Oriented Programming Lecture 5: BallWorld.
Inheritance - Polymorphism ITI 1121 Nour El Kadri.
28-Dec-04polymorhism.ppt1 Polymorphism. 28-Dec-04polymorhism.ppt2 signatures in any programming language, a signature is what distinguishes one function.
Summing Up Object Oriented Design. Four Major Components: Abstraction modeling real-life entities by essential information only Encapsulation clustering.
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
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.
Chapter 10: Introduction to Inheritance. Objectives Learn about the concept of inheritance Extend classes Override superclass methods Call constructors.
1 COSC2007 Data Structures II Chapter 9 Class Relationships.
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,
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
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
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
Inheritance Chapter 11 in Gaddis. Is a relationships in ‘real’ life Exist when one object is a specialized version of another one –Examples An english.
1 C# - Inheritance and Polymorphism. 2 1.Inheritance 2.Implementing Inheritance in C# 3.Constructor calls in Inheritance 4.Protected Access Modifier 5.The.
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.
CS 3180 (Prasad)L67Classes1 Classes and Interfaces Inheritance Polymorphism.
COP INTERMEDIATE JAVA Inheritance, Polymorphism, Interfaces.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 10 Java Fundamentals Objects/ClassesMethods.
Geoff Holmes and Bernhard Pfahringer COMP206-08S General Programming 2.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Java Programming Fifth Edition Chapter 9 Introduction to Inheritance.
Modern Programming Tools And Techniques-I
Object-oriented Programming in Java
Inheritance and Polymorphism
Object Oriented Programming
Modern Programming Tools And Techniques-I Inheritance
CSC 143 Inheritance.
Overloading and Constructors
Inheritance Basics Programming with Inheritance
Comp 249 Programming Methodology
Extending Classes.
Java Programming Language
Computer Programming with JAVA
Java – Inheritance.
Java Programming, Second Edition
Chapter 10: Method Overriding and method Overloading
Inheritance.
Chapter 14 Abstract Classes and Interfaces
Inheritance in Java CS 3331 Fall 2009.
Chapter 11 Inheritance and Polymorphism
Review of Previous Lesson
Method Overriding and method Overloading
Chapter 11 Inheritance and Polymorphism Part 1
Final and Abstract Classes
Chapter 11 Inheritance and Encapsulation and Polymorphism
Overloading Each method has a signature: its name together with the number and types of its parameters Methods Signatures String toString()
Presentation transcript:

COP 3331 Object Oriented Analysis and Design Chapter 5 – Classes and Inheritance Jean Muhammad

Slides by David A. Gaitros Overview Overloading Extending Classes Hiding Fields and Class Methods Slides by David A. Gaitros

Slides by David A. Gaitros Overloading Overloading refers to the ability to allow different methods or constructors of a class to share the same name. The name is said to be overloaded with multiple implementations. Commonly know as the feature of polymorphism in the object oriented paradigm. Slides by David A. Gaitros

Slides by David A. Gaitros Overloading The language can tell if two methods with the same name are different by the “signature”. The signature consists of the name of the method and the data type of the parameters. Note the return type, name of parameters, and final designations of parameters are not part of the signature. Slides by David A. Gaitros

Slides by David A. Gaitros Overloading Rules for overloading: Two methods in the same class my have the same name provided that they: Have different number of parameters, or Same number of parameters but of different type. Slides by David A. Gaitros

Slides by David A. Gaitros Overloading Example: Public class Point{ protected double x,y; public Point(){ x=0.0; y=0.0; } public double distance (Point other){ double dx = this.x – other.x; double dy = this.y – other.y; return Math.sqrt(dx * dy + dy * dy); Slides by David A. Gaitros

Slides by David A. Gaitros Overloading public double distance (double x, double y) { double dx = this.x –x; double dy = this.y – y; return Math.sqrt(dx * dx + dy * dy); } public double distance (int x, int y) { double dx = this.x – (double) x; double dy = this.y – (double) y; return Math.sqrt (dx * dx + dy *dy); public double distance () { return Math.sqrt (x*x + y *y); Slides by David A. Gaitros

Slides by David A. Gaitros Overloading Two situations to overload When there is a general, nondiscriminative description of the functionality that fits all the overloaded methods. When all the overloaded methods offer the same functionality, with some of them providing default arguments. Slides by David A. Gaitros

Slides by David A. Gaitros Extending Classes Inheritance (example) – When class Hourly_employee inherits from, or extends the class Employee, then Hourly_employee is called a subclass of the class Employee. In Java, each class my only inherit properties from another class. Slides by David A. Gaitros

Slides by David A. Gaitros Extending Classes Syntax of the extend clause: [ClassModifiers] class ClassName [extends SuperClass] [implements Interace1, Interface2, etc] { ClassMemberDeclarations } Slides by David A. Gaitros

Slides by David A. Gaitros Extending Classes import java.awt.Color; public class ColoredPoint extends Point { public Color color; public ColorPoint (final double x, final double y, final Color color) { super (x,y); this.color = color; } public ColoredPoint (final double x, final double y) { this (x,y, Color.black); // default color public ColoredPoint () { color = Color.black; Slides by David A. Gaitros

Slides by David A. Gaitros Extending Classes Note in the constructor. The invocation of super (…) must be the first statement in the constructor of the extended class Note: The second constructor of the ColoredPoint class supplies a default value for the color field. Note: The third constructor has not explicit invocation using super or this is made. This is also an example of overloading. Slides by David A. Gaitros

Slides by David A. Gaitros Extending Classes Subtype Subtypes are a specialized version of a specific type. For instance, let us say that you are storing integers for test scores. Since your test scores do not go below 0 or above 100, you could have a specialized subtype of integers that only had a valid range of 0 to 100. Slides by David A. Gaitros

Slides by David A. Gaitros Extending Classes Overriding Overriding is different from overloading. In overriding, methods share the same name, signature, and return type in contrast to overloading. class A { public void m1() { … method .. } public void m1( int i) { … method . .} // Note class A contains two overaloaded methods. Slides by David A. Gaitros

Slides by David A. Gaitros Extending Classes Now assumer we have the two classes: class B { public void m2() { . . } } class C extends B { public coid m2 () { . . } B b = new B(); C c = new C(); b.m2(); // invoke the m2() in class B c.m2(); // invoke the m2() in class C Slides by David A. Gaitros

Hiding Fields and Class Methods Hiding refers to the introduction of a field or a class method in a subclass that has the same name as a field or class method in a superclass. Overriding and hiding are different concepts Instance methods can only be overriden. Class methods and fields can only be hiden. Slides by David A. Gaitros

Hiding Fields and Class Methods Example: public class A { int x; void y() { … method . . } static void z() { … method . .} } public class B extends A { float x; // hiding void y () { … method . .} // overriding static int z () { … method . . } // hiding Slides by David A. Gaitros

Hiding Fields and Class Methods There is a crucial distinction between overriding and hiding. When an overridden method is invoked, the implementation that will be executed is chosen at run time. When the hidden method or field is invoked or accessed, the copy that will be used is determined at compile time. Class methods and fields are statically bound. Slides by David A. Gaitros