Advanced Java Programming CS 537 – Data Structures and Algorithms.

Slides:



Advertisements
Similar presentations
Chapter 22 Implementing lists: linked implementations.
Advertisements

Lecture 5: Interfaces.
Python Objects and Classes
Chapter 1 Object-Oriented Concepts. A class consists of variables called fields together with functions called methods that act on those fields.
Written by: Dr. JJ Shepherd
Road Map Introduction to object oriented programming. Classes
CSE 2501 Review Declaring a variable allocates space for the type of datum it is to store int x; // allocates space for an int int *px; // allocates space.
1 More on Inheritance Overview l Object: The father of all classes l Casting and Classes l Object Cloning l Importance of Cloning.
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Interfaces. In this class, we will cover: What an interface is Why you would use an interface Creating an interface Using an interface Cloning an object.
1 Java Object Model Part 2: the Object class. 2 Object class Superclass for all Java classes Any class without explicit extends clause is a direct subclass.
Reference Types. 2 Objectives Introduce reference types –class –array Discuss details of use –declaration –allocation –assignment –null –parameter –aggregation.
OOP in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
16-Aug-15 Air Force Institute of Technology Electrical and Computer Engineering Object-Oriented Programming in Java Topic : Interfaces, Copying/Cloning,
OOP Languages: Java vs C++
3.1 Documentation & Java Language Elements Purpose of documentation Assist the programmer with developing the program Assist other programers who.
Programming Languages and Paradigms Object-Oriented Programming.
Pointer Data Type and Pointer Variables
“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.
Cs3180 (Prasad)L8Packages1 Packages Organizing large programs => From monolithic programs to partitioning class name space Access control of names => Enforcing.
1 Chapter 10: Data Abstraction and Object Orientation Aaron Bloomfield CS 415 Fall 2005.
Object Oriented Programming: Java Edition By: Samuel Robinson.
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.
Programming Languages and Paradigms Object-Oriented Programming (Part II)
Algorithm Programming Bar-Ilan University תשס"ח by Moshe Fresko.
CS212: Object Oriented Analysis and Design Lecture 6: Friends, Constructor and destructors.
Programming Languages and Paradigms Object-Oriented Programming.
Overloading Binary Operators Two ways to overload –As a member function of a class –As a friend function As member functions –General syntax Data Structures.
Objects and Classes Chapter 6 CSCI CSCI 1302 – Objects and Classes2 Outline Introduction Defining Classes for Objects Constructing Objects Accessing.
1 Data Structures - CSCI 102 CS102 C++ Pointers & Dynamic Objects Prof Tejada.
Pointers OVERVIEW.
CS 2130 Lecture 5 Storage Classes Scope. C Programming C is not just another programming language C was designed for systems programming like writing.
Does not implement clone() method! public class Bar { … public Object clone() { … } Does not implement Cloneable interface!
Programming in Java CSCI-2220 Object Oriented Programming.
CS 376b Introduction to Computer Vision 01 / 23 / 2008 Instructor: Michael Eckmann.
C# Classes and Inheritance CNS 3260 C#.NET Software Development.
CPSC 252 The Big Three Page 1 The “Big Three” Every class that has data members pointing to dynamically allocated memory must implement these three methods:
Lecture 10 Concepts of Programming Languages Arne Kutzner Hanyang University / Seoul Korea.
COP 2800 Lake Sumter State College Mark Wilson, Instructor.
Classes. Constructor A constructor is a special method whose purpose is to construct and initialize objects. Constructor name must be the same as the.
Java for C++ Programmers A Brief Tutorial. Overview Classes and Objects Simple Program Constructors Arrays Strings Inheritance and Interfaces Exceptions.
OOP in C++ CS 124. Program Structure C++ Program: collection of files Source (.cpp) files be compiled separately to be linked into an executable Files.
Computer Organization and Design Pointers, Arrays and Strings in C Montek Singh Sep 18, 2015 Lab 5 supplement.
CS-1030 Dr. Mark L. Hornick 1 Basic C++ State the difference between a function/class declaration and a function/class definition. Explain the purpose.
Objects and Variables Local variables – Confined to single context: allocated on stack – Primitive types such as int or object references – Must be initialized.
COM S 228 Introduction to Data Structures Instructor: Ying Cai Department of Computer Science Iowa State University Office: Atanasoff.
1 Interface Design. 2 concept An interface is a way to describe what classes should do, without specifying how they should do it. It’s not a class but.
Chapter 11: Advanced Inheritance Concepts. Objectives Create and use abstract classes Use dynamic method binding Create arrays of subclass objects Use.
Access Specifier. Anything declared public can be accessed from anywhere. Anything declared private cannot be seen outside of its class. When a member.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
(c) University of Washington06-1 CSC 143 Java Inheritance Tidbits.
Chapter 1 C++ Basics Review (Section 1.4). Classes Defines the organization of a data user-defined type. Members can be  Data  Functions/Methods Information.
Java & C++ Comparisons How important are classes and objects?? What mechanisms exist for input and output?? Are references and pointers the same thing??
Array Size Arrays use static allocation of space. That is, when the array is created, we must specify the size of the array, e.g., int[] grades = new int[100];
1 C# - Inheritance and Polymorphism. 2 1.Inheritance 2.Implementing Inheritance in C# 3.Constructor calls in Inheritance 4.Protected Access Modifier 5.The.
5.1 Basics of defining and using classes A review of class and object definitions A class is a template or blueprint for an object A class defines.
Reference Types CSE301 University of Sunderland Harry R Erwin, PhD.
Internet Computing Module II. Syllabus Creating & Using classes in Java – Methods and Classes – Inheritance – Super Class – Method Overriding – Packages.
C Tutorial - Pointers CS 537 – Introduction to Operating Systems.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 10 Java Fundamentals Objects/ClassesMethods.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
Value Types. 2 Objectives Discuss concept of value types –efficiency –memory management –value semantics –boxing –unboxing –simple types Introduce struct.
Object Based Programming Chapter 8. 2 Contrast ____________________ Languages –Action oriented –Concentrate on writing ________________ –Data supports.
C11, Implications of Inheritance
OOP: Encapsulation &Abstraction
Andy Wang Object Oriented Programming in C++ COP 3330
understanding memory usage by a c++ program
Interface.
Extending Classes.
Presentation transcript:

Advanced Java Programming CS 537 – Data Structures and Algorithms

The Stack The stack is the place where all local variables are stored –a local variable is declared in some scope –Example int x; // creates the variable x on the stack As soon as the scope ends, all local variables declared in that scope end –the variable name and its space are gone –this happens implicitly – the user has no control over it

The Heap The heap is an area of memory that the user handles explicitly –user requests memory through new operator –java does garbage collection to reclaim unused memory A user maintains a handle on memory allocated in the heap with a reference variable

Creating Objects All objects are created on the heap A reference to object is stored on the stack –simply declaring an object does not create it automatically set to null –new operator allocates space on the heap

Creating Objects Example Object obj1 = new Object(); Object obj2; main x = 3 func1 obj1 = 200 obj2 = null 200 Heap Stack Object

Assigning Object References Reference can be assigned through new operator obj2 = new Object(); Reference can assigned to another reference obj2 = obj1; WARNING –when assigning to another reference, both references now refer to the same object

Assigning Object References Example Object obj1 = new Object(); Object obj2 = obj1; main x = 3 func1 obj1 = 200 obj2 = Heap Stack Object

Simple Class class Foo implements Cloneable { private int num; public void Foo(int num) { this.num = num; } public void setNum(int num) { this.num = num; } public int getNum() { return num; } }

Copying an Object Want to create and modify copy of object –remember, simple assignment not enough Foo f1 = new Foo(5); Foo f2 = f1; // still only one object – 2 references f2.setNum(10); System.out.println(“f1’s num = “ + f1.getNum()); // prints 10 System.out.println(“f2’s num = “ + f2.getNum()); // prints 10 –need to use the clone() method

clone() Method To use clone() must implement Cloneable Object.clone() is automatically inherited by every class –by default, it creates a new object and copies all fields Example Foo f1 = new Foo(5); Foo f2 = f1.clone(); f2.setNum(10); System.out.println(“f1’s num = “ + f1.getNum()); // prints 5 System.out.println(“f2’s num = “ + f2.getNum()); // prints 10

Shallow Clone Only copies the fields –does not copy what the fields reference Doesn’t work well for sophisticated objects Example: Class Foo { private int [] nums; public void Foo(int size) { nums = new int[size]; } … } Foo f1 = new Foo(5); Foo f2 = f1.clone();

Shallow Clone Foo f1 = new Foo(5); Foo f2 = f1.clone(); func1 f1 = 200 f2 = Heap Stack nums = Array nums =

Deep Clone Copies fields and what they refer to Must reimplement the clone() method class Foo { … public Object clone() { try { Foo fobj = (Foo)super.clone(); // copies fields fobj.nums = (int)nums.clone(); // arrays implement clone return fobj; } catch(CloneNotSupportedException e) { } }

Inheritance lets one class inherit fields and methods from another class use keyword extends to explicitly inherit another classes public and protected fields/methods can only explicitly extend from one class all classes implicitly extend the Object class

Inheritance overriding a method –must have the same signature as original –declaring a method final means future derived classes cannot override the method overloading a method –method has same name but different signature –they are actually different methods

Inheritance abstract classes and methods –declaring a class abstract must have an abstract method class cannot be directly used to create an object class must be inherited to be used –declaring a method abstract method must be defined in derived class

Abstract Class abstract class Pixel {... public abstract void refresh(); } class ColorPixel extends Pixel {... public void refresh() { do some work } Note: signature of method in derived class must be identical to parent declaration of the method

Interface basically an abstract class where all methods are abstract cannot use an interface to create an object class that uses an interface must implement all of the interfaces methods use the implements keyword a class can implement more than one interface

Interface simple example class Tester implements Foo, Bar {... } Foo and Bar are interfaces Tester must define all methods declared in Foo and Bar

Array Review Consecutive blocks of memory Creation: int [] grades = new int[25]; __.length: holds size of array __.clone(): makes copy of array data out-of-bounds exception: trying to access data outside of array bounds generates an exception Array size is fixed at creation

Vector Class Very similar to an array Major difference: vectors can grow beyond original size –if a certain capacity is exceeded, a new, larger memory region is allocated for vector –copy all data to the new area See Java documentation on-line for complete details