CS200 Algorithms and Data StructuresColorado State University Part 4. Advanced Java Topics Instructor: Sangmi Pallickara

Slides:



Advertisements
Similar presentations
OO Programming in Java Objectives for today: Overriding the toString() method Polymorphism & Dynamic Binding Interfaces Packages and Class Path.
Advertisements

Based on Java Software Development, 5th Ed. By Lewis &Loftus
CS 211 Inheritance AAA.
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.
ITEC200 – Week03 Inheritance and Class Hierarchies.
CS 106 Introduction to Computer Science I 11 / 26 / 2007 Instructor: Michael Eckmann.
Creating Classes from Other Classes Chapter 2 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Fall 2007CS 2251 Inheritance and Class Hierarchies Chapter 3.
Encapsulation, Inheritance & Interfaces CSE 115 Spring 2006 February 27, March 1 & 3, 2006.
CS 106 Introduction to Computer Science I 11 / 28 / 2007 Instructor: Michael Eckmann.
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
CS 106 Introduction to Computer Science I 04 / 16 / 2010 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 11 / 15 / 2006 Instructor: Michael Eckmann.
Data Abstraction and Object- Oriented Programming CS351 – Programming Paradigms.
CS 2511 Fall  Abstraction Abstract class Interfaces  Encapsulation Access Specifiers Data Hiding  Inheritance  Polymorphism.
OOP in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
© 2006 Pearson Addison-Wesley. All rights reserved9 A-1 Chapter 9 Advanced Java Topics CS102 Sections 51 and 52 Marc Smith and Jim Ten Eyck Spring 2007.
Chapter 7 - Generalization/Specialization and Inheritance1 Chapter 7 Generalization/Specialization and Inheritance.
Inheritance using Java
C++ Object Oriented 1. Class and Object The main purpose of C++ programming is to add object orientation to the C programming language and classes are.
“is a”  Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class.
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.
Recap (önemli noktaları yinelemek) from last week Paradigm Kay’s Description Intro to Objects Messages / Interconnections Information Hiding Classes Inheritance.
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.
Features of Object Oriented Programming Lec.4. ABSTRACTION AND ENCAPSULATION Computer programs can be very complex, perhaps the most complicated artifact.
Advanced Object- Oriented Programming Programming Right from the Start with Visual Basic.NET 1/e 14.
Object-Oriented Programming. An object is anything that can be represented by data in a computer’s memory and manipulated by a computer program.
OOP: Encapsulation,Abstraction & Polymorphism. What is Encapsulation Described as a protective barrier that prevents the code and data being randomly.
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.
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
Object Oriented Programming
1 COSC2007 Data Structures II Chapter 9 Class Relationships.
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
Introduction to Object-Oriented Programming Lesson 2.
CS 106 Introduction to Computer Science I 04 / 18 / 2008 Instructor: Michael Eckmann.
JAVA Programming (Session 4) “When you are willing to make sacrifices for a great cause, you will never be alone.” Instructor: รัฐภูมิ เถื่อนถนอม
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 9.1 – 9.4.
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
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.
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.
Object-oriented programming (OOP) is a programming paradigm using "objects" – data structures consisting of data fields and methods together with their.
Object-Oriented Design
Sections Inheritance and Abstract Classes
OOP: Encapsulation &Abstraction
Inheritance and Polymorphism
One class is an extension of another.
Testing Object-Oriented Software Concepts and Definitions
Table of Contents Class Objects.
INTRODUCTION TO OBJECT-ORIENTED PROGRAMMING (OOP) & CONCEPTS
12 Data abstraction Packages and encapsulation
Object Oriented Programming
OBJECT ORIENTED PROGRAMMING II LECTURE 8 GEORGE KOUTSOGIANNAKIS
Inheritance, Polymorphism, and Interfaces. Oh My
Advanced Java Topics Chapter 9
Interfaces.
Chapter 9 Carrano Chapter 10 Small Java
CIS 199 Final Review.
Final and Abstract Classes
Chapter 11 Inheritance and Encapsulation and Polymorphism
C++ Object Oriented 1.
Programming in C# CHAPTER 5 & 6
Presentation transcript:

CS200 Algorithms and Data StructuresColorado State University Part 4. Advanced Java Topics Instructor: Sangmi Pallickara Department of Computer Science Colorado State University 1

CS200 Algorithms and Data StructuresColorado State University Outline Object Oriented Programming Data Encapsulation Inheritance Polymorphism Using Abstract and Interface 2

CS200 Algorithms and Data StructuresColorado State University Object Oriented Programming Programming paradigm using “Objects” : data structures consisting of data fields and methods together with their interaction. Object? Class? Interface? Package? 3

CS200 Algorithms and Data StructuresColorado State University Basic Components a software bundle of related states (properties, or variables) and behavior (method) –State is stored in fields (variables in some programming languages) –Method exposes object’s behavior. 4 Time Alarm time Time Alarm time Set the time Set the alarm Enable the alarm Sound the alarm Silence the alarm Show the time Properties Methods

CS200 Algorithms and Data StructuresColorado State University Basic Components Class: Blueprint from which objects are created –Multiple Instances created from a class Interface: A Contract between classes and the outside the world. –When a class implements an interface, it promises to provide the behavior published by that interface. 5

CS200 Algorithms and Data StructuresColorado State University Basic Components Package: a namespace for organizing classes and interfaces 6

CS200 Algorithms and Data StructuresColorado State University Outline Object Oriented Programming Data Encapsulation Inheritance Polymorphism Using Abstract and Interface 7

CS200 Algorithms and Data StructuresColorado State University Data Encapsulation An ability of an object to be a container (or capsule) for related properties and methods. –Preventing unexpected change or reuse of the content Data hiding –Object can shield variables from external access. Private variables Public accessor and mutator methods 8

CS200 Algorithms and Data StructuresColorado State University Data Encapsulation public class Clock { private long time, alarm_time; private String serialNo; public void setTime(long _time){ time = _time; } public void setAlarmTime(long_time){ alarm_time = _time; } public long getTime(){return time} public long getAlarmTime(){return alarm_time} public void noticeAlarm(){ ring alarm } protected void set serialNo(String _serialNo){…} } 9

CS200 Algorithms and Data StructuresColorado State University Outline Object Oriented Programming Data Encapsulation Inheritance Polymorphism Using Abstract and Interface 10

CS200 Algorithms and Data StructuresColorado State University Inheritance The ability of a class to derive properties from a previously defined class. Relationship among classes. Enables to reuse software components –E.g. java.lang.Object() –toString(), notifyAll(), equals(), etc. 11

CS200 Algorithms and Data StructuresColorado State University Example: Inheritance clock Sports Watch Radio Clock 12

CS200 Algorithms and Data StructuresColorado State University Example: Inheritance – cont. 13 Public class SportsWatch extends Clock { private long start_time; private long end_time; public long getDuration() { return end_time - start_time; } }

CS200 Algorithms and Data StructuresColorado State University Overriding Methods 14 public class RadioClock public void noticeAlarm(){ ring alarm turn_on_the_Radio } }

CS200 Algorithms and Data StructuresColorado State University Java Access Modifiers Keywords: public, private,and protected Control the visibility of the members of a class – Public members can be used by anyone – Members declared without an access modifier are available to methods of the class and methods of other classes in the same package – Private members can be used only by methods of the class – Protected members can be used only by methods of the class, methods of other classes in the same package, and methods of the subclasses. 15

CS200 Algorithms and Data StructuresColorado State University Outline Object Oriented Programming Data Encapsulation Inheritance Polymorphism Using Abstract and Interface 16

CS200 Algorithms and Data StructuresColorado State University Polymorphism “Having multiple forms” Ability to create a variable, or an object that has more than one form. 17

CS200 Algorithms and Data StructuresColorado State University Polymorphic method RadioClock myRadioClock = new RadioClock(); Clock myClock = myRadioClock; myClock.notifyAlarm(); 18 Code of this room: DD A: Clock B. RadioClock

CS200 Algorithms and Data StructuresColorado State University Dynamic Binding myClock actually references an instance of RadioClock – It will turn on the radio. The version of a method “notifyAlarm()” is decided at execution time. (not at compilation time) 19

CS200 Algorithms and Data StructuresColorado State University Outline Object Oriented Programming Data Encapsulation Inheritance Polymorphism Using Abstract and Interface 20

CS200 Algorithms and Data StructuresColorado State University Abstract A special kind of class that cannot be instantiated. It allows only other classes to inherit from it. It enforces certain hierarchies for all the subclasses 21

CS200 Algorithms and Data StructuresColorado State University Interface An Interface is NOT a class. An Interface has NO implementation inside. –Definitions of methods without body. 22

CS200 Algorithms and Data StructuresColorado State University Comparison-1 23 FeatureInterfaceAbstract Class Multiple inheritance A class may inherit several interfaces Only one Default implementation Cannot provide any code Can provide complete, default code and/or just the details that have to be overridden. Access ModifierCannot have access modifiers. (everything is assumed as public) Can have it.

CS200 Algorithms and Data StructuresColorado State University Comparison-2 24 FeatureInterfaceAbstract Class Adding functionality ( Versioning) For a new method, we have to track down all the implementations of the interface and define implementation for the new method For a new method, we can provide default implementation and all the existing code might work properly. Fields and Constants No fields can be defined in interfaces Fields and constants can be defined