ICS 201 Inheritance Introduction to Computer Science

Slides:



Advertisements
Similar presentations
Final and Abstract Classes
Advertisements

Introduction to Java 2 Programming
Chapter 6 Objects and Classes F OO Programming Concepts F Creating Objects and Object Reference Variables –Differences between primitive data type and.
Based on Java Software Development, 5th Ed. By Lewis &Loftus
Chapter 1 Object-Oriented Concepts. A class consists of variables called fields together with functions called methods that act on those fields.
Chapter 4&5 Defining Classes Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Module 8 “Polymorphism and Inheritance”. Outline Understanding Inheritance Inheritance Diagrams Constructors in Derived Classes Type Compatibility Polymorphism.
CS 106 Introduction to Computer Science I 04 / 11 / 2008 Instructor: Michael Eckmann.
Inheritance Inheritance Reserved word protected Reserved word super
Objectives Introduction to Inheritance and Composition (Subclasses and SuperClasses) Overriding (and extending), and inheriting methods and constructors.
Chapter 7 Inheritance Slides prepared by Rose Williams, Binghamton University Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Immutable Objects and Classes.
CS 106 Introduction to Computer Science I 04 / 16 / 2010 Instructor: Michael Eckmann.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
Aalborg Media Lab 23-Jun-15 Inheritance Lecture 10 Chapter 8.
ECE122 L6: Problem Definition and Implementation February 15, 2007 ECE 122 Engineering Problem Solving with Java Lecture 6 Problem Definition and Implementation.
Chapter 7 Inheritance Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
Chapter 8: User-Defined Classes and ADTs J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
Slides prepared by Rose Williams, Binghamton University Chapter 7 Inheritance.
Chapter 11: Inheritance and Polymorphism Java Programming: Program Design Including Data Structures Program Design Including Data Structures.
COP 2800 Lake Sumter State College Mark Wilson, Instructor.
Inheritance using Java
Chapter 6 Class Inheritance F Superclasses and Subclasses F Keywords: super F Overriding methods F The Object Class F Modifiers: protected, final and abstract.
Chapter 12 Inheritance and Exceptions Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin, D. Mickunas,
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 12 Object-Oriented.
Chapter 11 Inheritance and Composition. Chapter Objectives Learn about inheritance Learn about subclasses and superclasses Explore how to override the.
Programming With Java ICS201 University Of Ha’il1 Chapter 8 Polymorphism and Abstract Classes.
Java Classes Appendix C © 2015 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
CS200 Algorithms and Data StructuresColorado State University Part 4. Advanced Java Topics Instructor: Sangmi Pallickara
Sun Certified Java Programmer, ©2004 Gary Lance, Chapter 5, page 1 Sun Certified Java 1.4 Programmer Chapter 5 Notes Gary Lance
More on Hierarchies 1. When an object of a subclass is instantiated, is memory allocated for only the data members of the subclass or also for the members.
Chapter 8: User-Defined Classes and ADTs
Chapter 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 26 - Java Object-Based Programming Outline 26.1Introduction.
Chapter 4&5 Defining Classes Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Programming With Java ICS201 University Of Ha’il1 Chapter 7 Inheritance.
Chapter 7: Class Inheritance F Superclasses and Subclasses F Keywords: super and this F Overriding methods F The Object Class F Modifiers: protected, final.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 11 Object-Oriented.
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
Creating Classes from Other Classes Appendix D © 2015 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
JAVA INTRODUCTION. What is Java? 1. Java is a Pure Object – Oriented language 2. Java is developing by existing languages like C and C++. How Java Differs.
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 9.1 – 9.4.
Attribute - CIS 1068 Program Design and Abstraction Zhen Jiang CIS Dept. Temple University SERC 347, Main Campus 12/24/2016.
1 Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
Creating Java Applications (Software Development Life Cycle) 1. specify the problem requirements - clarify 2. analyze the problem - Input? Processes? Output.
CMSC 202 Polymorphism 2 nd Lecture. Aug 6, Topics Constructors and polymorphism The clone method Abstract methods Abstract classes.
COMPUTER SCIENCE & TECHNOLOGY DEGREE PROGRAMME FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UVA WELLASSA ‏ Properties of Object Oriented Programming.
Slides prepared by Rose Williams, Binghamton University ICS201 Lecture 7 : Interfaces King Fahd University of Petroleum & Minerals College of Computer.
Chapter 5 Introduction to Defining Classes Fundamentals of Java.
Geoff Holmes and Bernhard Pfahringer COMP206-08S General Programming 2.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Objects and Classes. F OO Programming Concepts F Creating Objects and Object Reference Variables –Differences between primitive data type and object type.
Programming With Java ICS201 University Of Ha’il1 ICS 201 Introduction to Computer Science Inheritance.
OOP: Encapsulation &Abstraction
Advanced Java Topics Chapter 9
Lecture 12 Inheritance.
Objects as a programming concept
Polymorphism 2nd Lecture
Polymorphism 2nd Lecture
Polymorphism 2nd Lecture
Inheritance, Polymorphism, and Interfaces. Oh My
Comp 249 Programming Methodology
Chapter 8: User-Defined Classes and ADTs
CSE 1030: Implementing GUI Mark Shtern.
User-Defined Classes and ADTs
Information Hiding and Encapsulation Section 4.2
Presentation transcript:

ICS 201 Inheritance Introduction to Computer Science University Of Ha’il

Inheritance Hierarchy

Encapsulation Data Hiding Abstraction Security ؟

Encapsulation (data hiding) Encapsulation allows the programmer to group data and the methods that operate on them together in one place, and to hide details from the user. In Java, hiding details is done by marking them private withDraw Deposit Transfer Print AccountNo AccountValue Name Address withDraw any value Change Address

Encapsulation Sell (1 SR, Pepsi) Person Vending Machine Sell Buy Pepsi

- public and private Modifiers … Illegal because we try to access a private member (age) from outside the class Employee

Problem .. It is considered good programming practice to make all instance variables private Question: how to access and modify the instance variables of Employee objects e1, e2, e3 and e4? .. answer .. Use accessor and mutaor methods …. next slide ..

- Accessor and Mutator Methods … The methods that retrieve the data of fields are called accessors. The data can be accessed but not changed The name of an accessor method starts with the word get Example: public String getName() { return name; } The methods that modify the data of fields are called mutators. The name of a mutator method starts with the word set Example: public void setName(String n) name = n;

- Accessor and Mutator Methods (Example) Accessor method for instance variable name Mutator method for instance variable name Modifying the name of e1 using a mutator method

Encapsulation and Inheritance Pitfall: Use of Private Instance Variables from the Base Class An instance variable that is private in a base class is not accessible by name in the definition of a method in any other class, not even in a method definition of a derived class

Encapsulation and Inheritance Pitfall: Use of Private Instance Variables from the Base Class Instead, a private instance variable of the base class can only be accessed by the public accessor and mutator methods defined in that class

Access Modifiers public private Protected Can be used in any Java program without restriction private may only be used by the instance of the class that declares the variable or method Protected available to all classes in the same package available to all subclasses( even those in different packages )

What is Package ? Include all the classes in package Way to group a related class into one unit To resolve the name conflicts between class names import packageName.className ; Include all the classes in package import packageName.*;

Some Predefined Java Packages Package Name Contents java.applet Classes for implementing applets java.awt Classes for graphics, windows, and GUI’s java.awt.event Classes supporting AWT event handling java.awt.image Classes for image handling java.io Classes for input and output java.lang Basic language classes like Math (always available in any Java program) java.net Classes for networking java.util Useful auxiliary classes like Date

Visibility and Inheritance Public default protected private Clients in same package C None Clients in different packages Subclass in same package Subclass in different package Note: C; Can access default: If the access modifier is omitted

Access Modifiers © 2006 Pearson Addison-Wesley. All rights reserved

The Object Class is the Superclass of Every Java Class The class Object All classes defined without an explicit extends clause automatically extend Object The Object Class is the Superclass of Every Java Class

The Class Object The class Object is in the package java.lang which is always imported automatically public class Circle Equivalent public class Circle extends Object Most useful methods: String toString() boolean equals(Object otherObject) Object clone() Good idea to override these methods in your own classes