OOP in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.

Slides:



Advertisements
Similar presentations
Final and Abstract Classes
Advertisements

Java Programming 2 Dr. Priti Srinivas Sajja Introductory concepts of java programming as specified in PGDCA 203:Object Technology, S P University.
Module 8 “Polymorphism and Inheritance”. Outline Understanding Inheritance Inheritance Diagrams Constructors in Derived Classes Type Compatibility Polymorphism.
Inheritance Inheritance Reserved word protected Reserved word super
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 8.1 – 8.5.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Evan Korth New York University Computer Science I Classes and Objects Professor: Evan Korth New York University.
Aalborg Media Lab 23-Jun-15 Inheritance Lecture 10 Chapter 8.
Introduction to Data Structures, Spring 2007 Slide- 1 California State University, Fresno Introduction to Data Structure Object Oriented Programming Concepts.
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.
Inheritance. © 2004 Pearson Addison-Wesley. All rights reserved 8-2 Inheritance Inheritance is a fundamental object-oriented design technique used to.
OOP Languages: Java vs C++
Object Based Programming. Summary Slide  Instantiating An Object  Encapsulation  Inheritance  Polymorphism –Overriding Methods –Overloading vs. Overriding.
Inheritance using Java
Programming Languages and Paradigms Object-Oriented Programming.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
“is a”  Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class.
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.
Inheritance and Class Hierarchies Ellen Walker CPSC 201 Data Structures Hiram College.
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.
CS200 Algorithms and Data StructuresColorado State University Part 4. Advanced Java Topics Instructor: Sangmi Pallickara
CSE 501N Fall ‘09 14: Inheritance 20 October 2009 Nick Leidenfrost.
Object Based Programming Chapter 8. 2 In This Chapter We will learn about classes Garbage Collection Data Abstraction and encapsulation.
1 Chapter 8 – Classes and Object: A Deeper Look Outline 1 Introduction 2 Implementing a Time Abstract Data Type with a Class 3 Class Scope 4 Controlling.
Programming in Java CSCI-2220 Object Oriented Programming.
Chapter 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
Summing Up Object Oriented Design. Four Major Components: Abstraction modeling real-life entities by essential information only Encapsulation clustering.
8. Inheritance “Is-a” Relationship. Topics Creating Subclasses Overriding Methods Class Hierarchies Abstract Class Inheritance and GUIs The Timer Class.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Inheritance. Inheritance is a fundamental object-oriented design technique used to create and organize reusable classes Chapter 8 focuses on: deriving.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 26 - Java Object-Based Programming Outline 26.1Introduction.
Lecture 10 Concepts of Programming Languages Arne Kutzner Hanyang University / Seoul Korea.
Introduction to Java Chapter 7 - Classes & Object-oriented Programming1 Chapter 7 Classes and Object-Oriented Programming.
CS451 - Lecture 2 1 CS451 Lecture 2: Introduction to Object Orientation Yugi Lee STB #555 (816) * Acknowledgement:
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
Classes. Constructor A constructor is a special method whose purpose is to construct and initialize objects. Constructor name must be the same as the.
Object-Oriented Programming Chapter Chapter
Programming With Java ICS201 University Of Ha’il1 Chapter 7 Inheritance.
Object Oriented Programming
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 9 Java Fundamentals Objects/ClassesMethods Mon.
ISBN Object-Oriented Programming Chapter Chapter
Inheritance CSI 1101 Nour El Kadri. OOP  We have seen that object-oriented programming (OOP) helps organizing and maintaining large software systems.
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
Coming up: Inheritance
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
YG - CS Concept of Encapsulation What is encapsulation? - data and functions/methods are packaged together in the class normally.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
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.
SUBCLASSES - JAVA. The Purpose of Subclasses Class Farm String getOwner() void setOwner(String s) int getSize() void setSize(int s) Class DairyFarm String.
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.
Object Based Programming Chapter 8. 2 Contrast ____________________ Languages –Action oriented –Concentrate on writing ________________ –Data supports.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Object-Oriented Design
Inheritance ITI1121 Nour El Kadri.
Objects as a programming concept
Final and Abstract Classes
Inheritance and Polymorphism
Final and Abstract Classes
Lecture 10 Concepts of Programming Languages
Presentation transcript:

OOP in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park

Object Oriented Programming (OOP) OO Principles Abstraction Encapsulation Abstract Data Type (ADT) Implementation independent interfaces Data and operations on data Java Many language features supporting OOP

Overview Objects & class References & alias “this” & “super” reference Constructor Garbage collection & destructor Modifiers Public, Private, Protected Static Final

Object & Class Object Abstracts away (data, algorithm) details Encapsulates data Instances exist at run time Class Blueprint for objects (of same type) Exists at compile time

References & Aliases Reference A way to get to an object, not the object itself All variables in Java are references to objects Alias Multiple references to same object “X == Y“ operator tests for alias X.equals(Y) tests contents of object (potentially) Object Z Reference X Reference Y

References & Aliases – Issues Copying References X = new Object(); Y = X;// Y refers to same object as X Objects X = new Object(); Y = X.clone();// Y refers to different object Modifying objects X = new Object(); Y = X; X.change();// modifies object for Y

“this” Reference Description Reserved keyword Refers to object through which method was invoked Allows object to refer to itself Use to refer to instance variables of object

“this” Reference – Example class Node { value val1; value val2; void foo(value val2) { … = val1;// same as this.val1 (implicit this) … = val2;// parameter to method … = this.val2;// instance variable for object bar( this );// passes reference to object }

Inheritance Definition Relationship between classes when state and behavior of one class is a subset of another class Terminology Superclass / parent  More general class Subclass  More specialized class Forms a class hierarchy Helps promote code reuse

“super” Reference Description Reserved keyword Refers to superclass Allows object to refer to methods / variables in superclass Examples super.x// accesses variable x in superclass super()// invokes constructor in superclass super.foo()// invokes method foo() in superclass

Constructor Description Method invoked when object is instantiated Helps initialize object Method with same name as class w/o return type Implicitly invokes constructor for superclass If not explicitly included

Constructor – Example class foo { foo() { … }// constructor for foo } class bar extends foo { bar() {// constructor for bar // implicitly invokes foo() here … } class bar2 extends foo { bar2() {// constructor for bar super(); // explicitly invokes foo() here }

Garbage Collection Concepts All interactions with objects occurs through reference variables If no reference to object exists, object becomes garbage (useless, no longer affects program) Garbage collection Reclaiming memory used by unreferenced objects Periodically performed by Java Not guaranteed to occur Only needed if running low on memory

Destructor Description Method with name finalize() Returns void Contains action performed when object is freed Invoked automatically by garbage collector Not invoked if garbage collection does not occur Usually needed only for non-Java methods Example class foo { void finalize() { … }// destructor for foo }

Method Overloading Description Same name refers to multiple methods Sources of overloading Multiple methods with different parameters Constructors frequently overloaded Redefine method in subclass Example class foo { foo() { … }// constructor for foo foo(int n) { … }// 2 nd constructor for foo }

Modifier Description Java keyword (added to definition) Specifies characteristics of a language construct (Partial) list of modifiers Public / private / protected Static Final Abstract

Modifier Example public class foo { private static int count; private final int increment = 5; protected void finalize { … } } public abstract class bar { abstract int go() { … } }

Visibility Modifier Properties Controls access to class members Applied to instance variables & methods Types Public May be directly referenced outside object Private Referenced only within class definition Protected Referenced within class definition & by subclasses

Visibility Modifier For instance variables Should usually be private to enforce encapsulation Sometimes may be protected for subclass access For methods Public methods – provide services to clients Private methods – provide support other methods Protected methods – provide support for subclass

Modifier – Static Static variable Single copy for class Shared among all objects of class Static method Can be invoked through class name Does not need to be invoked through object Can be used even if no objects of class exist Can not reference instance variables

Modifier – Final Final variable Value can not be changed Must be initialized in every constructor Attempts to modify final are caught at compile time Final static variable Used for constants Example final static int Increment = 5;

Modifier – Final Final method Method can not be overloaded by subclass Private methods are implicitly final Final class Class can not be a superclass (extended) Methods in final class are implicitly final

Modifier – Final Using final classes Prevents inheritance / polymorphism May be useful for Security Object oriented design Example – class String is final Programs can depend on properties specified in Java library API Prevents subclass that may bypass security restrictions

Modifier – Abstract Description Represents generic concept Can not be instantiated Abstract class Placeholder in class hierarchy Can be partial description of class Can contain non-abstract methods Required if any method in class is abstract Example abstract class foo {// abstract class abstract void bar() { … }// abstract method

Interface Description Collection of Constants Abstract methods Can not be instantiated Classes can implement interface Must implement all methods in interface Example class foo implements bar { … } // interface bar