1 Review of classes and subclasses M fast through this material, since by now all have seen it in CS100 or the Java bootcamp First packages Then classes.

Slides:



Advertisements
Similar presentations
Core Java Lecture 4-5. What We Will Cover Today What Are Methods Scope and Life Time of Variables Command Line Arguments Use of static keyword in Java.
Advertisements

INHERITANCE BASICS Reusability is achieved by INHERITANCE
METHOD OVERRIDING 1.Sub class can override the methods defined by the super class. 2.Overridden Methods in the sub classes should have same name, same.
PACKAGES. PACKAGES IN JAVA A package is a collection of related classes and interfaces in Java Packages help in logical grouping of classes and interfaces.
George Blank University Lecturer. CS 602 Java and the Web Object Oriented Software Development Using Java Chapter 4.
Road Map Introduction to object oriented programming. Classes
Access to Names Namespaces, Scopes, Access privileges.
Java boot camp1 Subclasses Concepts: The subclass and inheritance: subclass B of class A inherits fields and methods from A. A is a superclass of B. Keyword.
Evan Korth New York University Computer Science I Classes and Objects Professor: Evan Korth New York University.
June 1, 2000 Object Oriented Programming in Java (95-707) Java Language Basics 1 Lecture 3 Object Oriented Programming in Java Language Basics Classes,
1 What’s a class People are confused with nested classes and anonymous classes. Experiments in a recitation last week revealed that the problems were probably.
1 Executing Method Calls This lecture tells you precisely how method calls are executed (a few details will have to wait until we get to classes and objects).
1 Memory Model of A Program, Methods Overview l Closer Look at Methods l Memory Model of JVM »Method Area »Heap »Stack l Preview: Parameter Passing.
Terms and Rules Professor Evan Korth New York University (All rights reserved)
Unit 051 Packages What is a Package? Why use Packages? Creating a Package Naming a Package Using Package Members Managing Source and Class Files Visibility.
1 CS100J 05 February 2005 Today’s topic: Customizing a class (continued) Quiz 1 is today Quiz 2 is next Tuesday Quote for the day: There is no reason anyone.
Applying OO Concepts Using Java. In this class, we will cover: Overriding a method Overloading a method Constructors Mutator and accessor methods The.
CS 1110 Final Exam: Review Session 2 Part 1 : Inheriting classes 1. Inheritance Facts 2. Constructors in Subclasses BREAK : 10 sec. Part 2 : Working with.
OOP Languages: Java vs C++
Principles of Computer Programming (using Java) Review Haidong Xue Summer 2011, at GSU.
LESSON 2 CREATING A JAVA APPLICATION JAVA PROGRAMMING Compiled By: Edwin O. Okech [Tutor, Amoud University]
Cs3180 (Prasad)L8Packages1 Packages Organizing large programs => From monolithic programs to partitioning class name space Access control of names => Enforcing.
Dale Roberts Procedural Programming using Java Dale Roberts, Lecturer Computer Science, IUPUI Department of Computer and.
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.
1 Biggest issue!!! You can’t do questions on this topic correctly unless you draw variables, draw objects when they are created, and draw frames for method.
APCS Java AB 2004 Review of CS1 and CS2 Review for AP test #1 Sources: 2003 Workshop notes from Chris Nevison (Colgate University) AP Study Guide to go.
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.
CS/ENGRD 2110 SPRING 2012 Lecture 2: Objects and classes in Java 1.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
CS1110 lecture 5 8 Feb 2010 Testing; class Object; toString; static variables/methods Reading for this lecture: Testing with JUnit (Appendix I.2.4 & pp.
Programming in Java CSCI-2220 Object Oriented Programming.
JAVA COURSE 1 Computer Engineering Association. Compile your first program Public class Hello{ public class Hello(){ System.out.println(“Hello”); } puclic.
Summing Up Object Oriented Design. Four Major Components: Abstraction modeling real-life entities by essential information only Encapsulation clustering.
Methods: A Deeper Look. Template for Class Definition public class { } A.Import Statement B.Class Comments C.Class Name D.Data members E.Methods (inc.
Introduction to Java Chapter 7 - Classes & Object-oriented Programming1 Chapter 7 Classes and Object-Oriented Programming.
COP3502 Programming Fundamentals for CIS Majors 1 Instructor: Parisa Rashidi.
This recitation 1 An interesting point about A3: Using previous methods to avoid work in programming and debugging. How much time did you spend writing.
CMSC 341 Java Packages, Classes, Variables, Expressions, Flow Control, and Exceptions.
CS884 (Prasad)java11Extn1 Java 1.1 Extensions Nested classes static public/protected/private.
CS100A, Fall 1998 Key Concepts 1 These notes contain short definitions of the basic entities that make up a Java program, along with a description of the.
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,
1 CS100J 08 September 2005 Today’s topic: Customizing a class (continued) Quote for the day: There is no reason anyone would want a computer in their home.
Access Modifiers Control which classes use a feature Only class-level variables may be controlled by access modifiers Modifiers 1. public 2. protected.
Bank Account Example public class BankAccount { private double balance; public static int totalAccounts = 0; public BankAccount() { balance = 0; totalAccounts++;
Lecture 8: Advanced OOP Part 2. Overview Review of Subtypes Interfaces Packages Sorting.
Classes, Interfaces and Packages
Access Specifier. Anything declared public can be accessed from anywhere. Anything declared private cannot be seen outside of its class. When a member.
CS/ENGRD 2110 SPRING 2016 Lecture 2: Objects and classes in Java 1.
In this class, we will cover: Overriding a method Overloading a method Constructors Mutator and accessor methods The import statement and using prewritten.
 Static  Example for Static Field  Example for Static Method  Math class methods  Casting  Scope of Declaration  Method Overloading  Constructor.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
1 CS1110 Thursday, 10 Feb 2011 Discussion of Methods: Executing method calls. If-statements. The return statement in a function. Local variables. For this.
OOP Basics Classes & Methods (c) IDMS/SQL News
Creating Java Applications (Software Development Life Cycle) 1. specify the problem requirements - clarify 2. analyze the problem - Input? Processes? Output.
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.
CS100Lecture 61 Announcements Homework P1 due on Thursday Homework P2 handed out.
Objects and Classes. F OO Programming Concepts F Creating Objects and Object Reference Variables –Differences between primitive data type and object type.
CS/ENGRD 2110 Fall 2017 Lecture 2: Objects and classes in Java
Topic: Classes and Objects
Lecture 10 Review of classes
Inheritance-Basics.
Interfaces.
CS/ENGRD 2110 Spring 2018 Lecture 2: Objects and classes in Java
Java Programming Language
CS/ENGRD 2110 Spring 2016 Lecture 5: Local vars; Inside-out rule; constructors
CS/ENGRD 2110 Spring 2019 Lecture 2: Objects and classes in Java
CS/ENGRD 2110 Spring 2019 Lecture 2: Objects and classes in Java
Presentation transcript:

1 Review of classes and subclasses M fast through this material, since by now all have seen it in CS100 or the Java bootcamp First packages Then classes Then subclasses Goal: to give you complete understanding about how objects, methods, method calls are implement. Raise discussion of classes and subclasses above the level of the computer and talk instead about classes as drawers in a filing cabinet, objects as manila folders that go in file drawers, and references or pointers as labels or names placed on manila folders. This makes concepts easier to grasp, to talk about; loses nothing.

2 Packages Read Weiss, section 3.6, “packages”, for reference Package: collection of.java source files (and other packages and interfaces) that are grouped together in the same directory (folder). Package java.lang contains classes that you can automatically use: wrapper classes: Integer, Boolean, etc. Math: contains functions you can use, like abs and sqrt String and StringBuffer System Throwable, Error, Exception (discuss later)

3 Package java.io contains classes for doing input/ouput. We’ll discuss this a bit in recitations. To use these, you should “import” them. Put the command import java.io.*; at the top of a file that contains a class that will use a class in this package. import java.io.*; public class Ex { public void m(…) {... }

4 Other useful packages You have to import these packages. We’ll use many of these later in the course. java.applet: used for applets java.awt: used in constructing GUIs javax.swing: the more modern classes for constructing GUIs java.util: classes for dates, calendars, locales, random numbers. Class Vector. Classes for sets, lists, maps

5 You can make your own packages Default package, say classes C1, C2, C3 Package mine, say classes K1, K2 File structure: main directory: C1.java C2.java C3.java mypack (a directory) K1.java K2.java package mypack; public class K2 { } package mypack; public class K1 { } file K1.java file K2.java

6 Visibility levels public int w; private int x; protected int y; /* package */ int z; private: visible only in the class. /* package */: visible only in the package in which it appears. protected: visible in the package in which it appears and in subclasses. public: visible anywhere. Note: You cannot use the keyword package as a prefix on a declaration. That is why we have placed comments around it. Visibility “package” is the default, when no access modifier is given. Note: You can place these modifiers on fields, methods, and classes

7 Review of classes Why review? To make sure that you and I are using the same terminology and the same concepts of class and related issues. Use this example: public class C { public static final int ten= 10; private int y; // Constructor: instance with y = yp public C (int yp) { y= yp; } // specification of method p public static void p(int x) { // body of method goes here } // specification of function f public int f(int y) { // body of function f goes here }

8 Notes on the class on previous slide 1. A class is a drawer of a file cabinet. It contains (at least) the static entities defined in the class. In this case, ten and p. 2. A class is a template for objects of the class. Every object of C is a manila folder of the form given below. The manila folder contains all nonstatic entities declared in class C. The manila folder goes in C’s file drawer. Class C a0 C y f C name of object name of class nonstatic field, or instance variable nonstatic method, or instance method constructor

9 Draw objects as manila folders You MUST draw each object as shown below As a manila folder with: class name in box in upper right corner name of object on the tab of manila folder each nonstatic field as a variable in the folder the name of each nonstatic method in the folder a0 C y f C

10 The frame for a method call All method calls occur within methods, which are defined in classes. We now explain the use of the “scope box” in the frame: Scope box used during execution of method body to find variables and methods that are referenced Scope box contains, for a: nonstatic method: the name of the instance in which the method appears static method: the name of the class in which it is defined constructor: name of newly created object parameters and local variables appear here method name scope box return address

11 parameters and local variables appear here method name scope box return address Execution of procedure call 1. Evaluate args and push them onto call stack. 2. Push locations for the rest of the frame for the call onto the stack. 3. Put in frame: name of method, local vars, return address, and scope box --filled in correctly (see slide 10). 4. Execute method body --look in frame at top of stack for variable or method. If not there, use scope box to determine where to look next; if in an object, search from bottom to top. 5. Pop frame from call stack; continue execut- ing at the return address in popped frame.

12 Sample execution of proc call --do in class public class M { public static void main (String[] pars) { b= new C(5); c= new C(4); d= c.hits(); } // An instance maintains the number of walks // and hits of a baseball player public class C { private int y; // number of walks private int x= 0; // number of hits // Constructor: instance with yp walks, 2 hits public C (int yp) { y= yp; x= 2; } // = number of hits public int hits() { return x; } // = number of hits + number of errors public int hitErr() { return x;} }

13 Memorize for quiz on Tuesday, 18 Sept. It’s important for understanding how method calls and objects work that you memorize: (1) format of a frame (2) format of an object --manila folder (3) evaluation of a new expression new C(…) (a) create a new instance of class C (b) execute method call C(…), putting in the scope box of the frame the name of the newly created object (4) Steps in executing a method call (slide 11).

14 Drawing an instance of a subclass // Extends class C on slide 12 public class Sub extends C { private int z; // number of errors // Constructor: instance with yp walks, 2 hits, // and zp errors public Sub(int yp, int zp) { super(yp); z= zp; } // = number of hits + number of errors public int hitErr() { return hits() + z;} } a5 C y x C hits hitErr Sub z Sub hitErr superclass name superclass components subclass name subclass components

15 a5 C y 5 x 2 C hits hitErr Sub z 6 Sub hitErr Overriding a method Consider: Sub b= new Sub(5,6); b.hitErr(); Which method hitErr is called? Our rules for execution of calls (slide 11) say the one below the line --the one in subclass Sub. It overrides the other. In class, we execute this call. b a5

16 a5 C y 5 x 2 C hits hitErr Sub z 6 Sub hitErr Casting Consider: C c= new Sub(5,6); Instance a5 automatically cast to C, since c is of class-type C. Apparent type of a5 (using c) is C, real type is Sub. Legal Illegal c.yc.z c.x c.hits() c.hitErr() c a5 Using c, reference only names accessible in the class of C But our rules say that c.hitErr() refers to this!!!

17 a5 C y 5 x 2 C hits hitErr Sub z 6 Sub hitErr Casting Consider: C c= new Sub(5,6); Sub s= (Sub) c; Explicit cast of c to subclass s. Using s, one can reference everything in object a5. Legal IllegalLegal c.yc.zs.z c.xs.hits() c.hits()s.hitErr() c.hitErr() c a5 s