C OMP 401 C LASS S TATE Instructor: Prasun Dewan.

Slides:



Advertisements
Similar presentations
Final and Abstract Classes
Advertisements

Object-Oriented programming in C++ Classes as units of encapsulation Information Hiding Inheritance polymorphism and dynamic dispatching Storage management.
INHERITANCE BASICS Reusability is achieved by INHERITANCE
INTERFACES IN JAVA 1.Java Does not support Multiple Inheritance directly. Multiple inheritance can be achieved in java by the use of interfaces. 2.We need.
Java Software Solutions
C OMP 110 S TATE Instructor: Jason Carter. 2 O UTLINE Instance Variables Procedures Properties Print Statements Println vs. Print Overloading.
C OMP 110 F UNCTIONS Instructor: Jason Carter. 2 O UTLINE Programmatic instantiation of objects Functions calling other functions Algorithm and stepwise.
C OMP 401 M EMORY R EPRESENTATION OF P RIMITIVE V ALUES AND O BJECTS Instructor: Prasun Dewan.
C OMP 110 R EPRESENTATION Instructor: Jason Carter.
C OMP 110 S TYLE Instructor: Jason Carter. 2 I NTERFACES AND M ORE S TYLE Define contracts between our users and implementers Optional – they may not.
C OMP 401 O BJECTS Instructor: Prasun Dewan 2 C OMPUTER VS. P ROGRAM M ODEL Processor Compiler Program (source code)
C OMP 110 L OAN C ASE S TUDY Instructor: Jason Carter.
C OMP 110 O BJECTS Instructor: Jason Carter. 2 C OMPUTER VS. P ROGRAM M ODEL Processor Compiler Program (source code)
Road Map Introduction to object oriented programming. Classes
1 Classes Overview l Classes as Types l Declaring Instance Variables l Implementing Methods l Constructors l Accessor and Mutator Methods.
Midterm Exam 75 points 1 min per point Allocate time proportionate to points Closed book Chapters 1-5 (except char) PDF/PS with index and corrections coming.
Program Style Identifier Names Comments Named Constants Avoiding Code Repetition Giving Least Privilege Efficiency Interfaces.
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
ECE122 L16: Class Relationships April 3, 2007 ECE 122 Engineering Problem Solving with Java Lecture 16 Class Relationships.
Programmer-Defined Types Object Types as Programmer-defined Types Two-way Dependencies Representation Errors Primitive Vs Object Properties Constructors.
Evan Korth New York University Computer Science I Classes and Objects Professor: Evan Korth New York University.
Object Types Primitive types Primitive Vs Object Types Types Classes ABMISpreadsheet AnotherBMISpreadsheet Interfaces BMISpreadsheet Loan Stringdoubleint.
Terms and Rules Professor Evan Korth New York University (All rights reserved)
Static Class Members Wrapper Classes Autoboxing Unboxing.
OOP in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Chapter 6 Class Inheritance F Superclasses and Subclasses F Keywords: super F Overriding methods F The Object Class F Modifiers: protected, final and abstract.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
C OMP 401 C LASS S TATE Instructor: Prasun Dewan.
An Introduction to Java Chapter 11 Object-Oriented Application Development: Part I.
Chapter 6 Object-Oriented Java Script JavaScript, Third Edition.
C OMP 401: C ONSTRUCTORS AND P OINTERS Instructor: Prasun Dewan (FB 150,
CORE JAVA C LASS Class is a template for multiple objects with similar features and It is a blue print for objects. It defines.
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.
C OMP 110/401 D OCUMENTATION : C OMMENTS Instructor: Prasun Dewan.
C OMP 110/401 E NCAPSULATION AND L EAST P RIVILEGE Instructor: Prasun Dewan.
Introduction to Java Chapter 7 - Classes & Object-oriented Programming1 Chapter 7 Classes and Object-Oriented Programming.
Object Oriented Programming
Chapter 7: Class Inheritance F Superclasses and Subclasses F Keywords: super and this F Overriding methods F The Object Class F Modifiers: protected, final.
Chapter 3: Developing Class Methods Object-Oriented Program Development Using Java: A Class-Centered Approach.
C OMP 401 R EFLECTION AND A CTION O BJECTS Instructor: Prasun Dewan.
Chapter 5 Defining Classes II Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
1 Static Variable and Method Lecture 9 by Dr. Norazah Yusof.
1 Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
A DVANCED P ROGRAMMING C HAPTER 4: I NTRODUCTION TO I NTRODUCTION TO C LASSES, O BJECTS, M ETHODS AND STRINGS Dr Shahriar Bijani Winter 2016.
C OMP 401 E XCEPTIONS -R EMAINDER Instructor: Prasun Dewan.
This In Java, the keyword this allows an object to refer to itself. Or, in other words, this refers to the current object – the object whose method or.
C OMP 110/401 D OCUMENTATION : A NNOTATIONS Instructor: Prasun Dewan.
C OMP 401 C OPY : S HALLOW AND D EEP Instructor: Prasun Dewan.
(C) 2010 Pearson Education, Inc. All rights reserved.  Best way to develop and maintain a large program is to construct it from small, simple pieces,
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 10 Java Fundamentals Objects/ClassesMethods.
Object-Oriented Design
Classes and OOP.
Chapter 16 UML Class Diagrams.
CompSci 230 Software Construction
UML Class Diagram: class Rectangle
Class Variables We’ve seen how to add extra subroutines into our programs Can be accessed from within one another We can also add variables that are “global”
Instructor: Prasun Dewan
Object Oriented Programming
METHODS AND BEHAVIORS AKEEL AHMED.
Comp 401 Metamorphosis: Casting vs. Conversion
Encapsulation and Constructors
Group Status Project Status.
Interfaces.
Static is one of the modifiers that determine variable and method characteristics. The static modifier associates a variable or method with its class.
Chapter 8 Class Inheritance and Interfaces
Dr. R Z Khan Handout-3 Classes
Final and Abstract Classes
Visibilities and Static-ness
Object Oriented Programming (OOP) Lecture No. 12
Chapter 7 Objects and Classes
Presentation transcript:

C OMP 401 C LASS S TATE Instructor: Prasun Dewan

2 C OUNTING I NSTANCES OF AC ARTESIAN P OINT

3 I NCREMENTED N UMBER OF I NSTANCES

4 C REATING M ID P OINT

5 S PECIFYING T WO E ND P OINTS

6 T YPE OF M ETHODS Difference between method returning number of instances and other methods seen so far? Difference between mid and other methods seen so far?

7 R EAL -W ORLD A NALOGY new ACorvette(silver) new ACorvette(red) getMileage()64000 blend(silverCar, redCar); numCarsProduced();

8 O-O W ORLD ACartesianPoint Point1 Point2 new ACartesianPoint( 25, 50) new ACartesianPoint( 125, 150) getRadius() 59 mid(25,50, 125,150); mid(point1x,point1y, point2x,point2y); numInstances() 3

9 C LASS M ETHODS Methods can be invoked on class itself Called class or static methods Declared in class on which they are invoked Keyword static in header Accesses no instance variable

10 P ROGRAMMER -D EFINED M ID public getRadius (){ return Math.sqrt(x*x + y*y); } public static Point mid ( int x1, int y1, int x2, int y2 ) { return new ACartesianPoint(x1 + (x2 - x1)/2, y1 + (y2 - y1)/2); } Instance Method Class Method Access instance variables Access no instance variable

11 E XTERNAL C ALL OF C LASS M ETHOD ACartesianPoint. mid(x1, y1, x2, y2) Math.sqrt(x*x + y*y); Class as target

12 NUM I NSTANCES A LGORITHM Declare variable, numInstances initialized to zero Increment numInstances each time a new instance is created getNumInstances() returns numInstances

13 R ETURNING NUM I NSTANCES public static int getNumInstances() { return numInstances; } numInstances() returns numInstances Class property

14 I NCREMENTING NUM I NSTANCES Increment numInstances each time a new instance is created

15 I NCREMENTING NUM I NSTANCES (S OLUTION ) public ACartesianPoint( int theX, int theY) { x = theX; y = theY; numInstances++; } Increment numInstances each time a new instance is created

16 D ECLARING NUM I NSTANCES static int numInstances = 0; Declare variable, numInstances initialized to zero // class variable

17 T RACING E XECUTION variablesmemory 0 0 numInstances; x public ACartesianPoint( int theX, int theY) { x = theX; y = theY; numInstances++; } 0y x 0y

18 I NSTANCE VS. C LASS M EMBERS Class Members Class method Class variables Instance Members Instance methods Instance variables

19 S COPE OF C LASS AND I NSTANCE M EMBERS Class Members visible to other class members visible to all instance members class & instance methods can access class variables class and instance methods can call class methods Instance Members visible to other instance members not visible to class members which of (zero to many) copies of an instance variable should a class member refer to?

20 L EGAL & I LLEGAL A CCESS public static int getNumInstances() { System.out.println(x); return numInstances; } public ACartesianPoint( int theX, int theY) { x = theX; y = theY; numInstances = numInstances + 1; System.out.println(getNumInstances()); } static int numInstances = 0; int x, y; public double getRadius() { return Math.sqrt(x*x + y*y); }

21 I NSTANCE VS. C LASS N AMED C ONSTANT variablesmemory 2.2LBS_IN_KGS public class AnotherBMISpreadsheet implements BMISpreadsheet{ double height, weight, bmi;... public static final double LBS_IN_KG = 2.2; public static final double CMS_IN_INCH = 2.54; double calculateBMI() { return (weight/LBS_IN_KG) / (height*CMS_IN_INCH/100*height*CMS_IN_INCH/10 0); } new AnotherBMISpreadsheet() AnotherBMISpreadsheet.LBS_IN_KGS;

22 I NSTANCE VS. C LASS N AMED C ONSTANT variablesmemory 2.2LBS_IN_KGS public class AnotherBMISpreadsheet implements BMISpreadsheet{ double height, weight, bmi;... final double LBS_IN_KG = 2.2; public static final double CMS_IN_INCH = 2.54; double calculateBMI() { return (weight/LBS_IN_KG) / (height*CMS_IN_INCH/100*height*CMS_IN_INCH/10 0); } new AnotherBMISpreadsheet() (new AnotherBMISpreadsheet()).LBS_IN_KGS; public final 2.2LBS_IN_KGS

23 C LASS VS. I NSTANCE C ONSTANT Should be class constant one copy easy to refer (require no instance creation) Unless some good reason for hiding named constants from static methods

24 C LASS VS. I NSTANCE M ETHODS public static Point mid ( int x1, int y1, int x2, int y2 ) { return new ACartesianPoint(x1 + (x2 - x1)/2, y1 + (y2 - y1)/2); } public Point mid ( int x1, int y1, int x2, int y2 ) { return new ACartesianPoint(x1 + (x2 - x1)/2, y1 + (y2 - y1)/2); } ACartesianPoint.mid(25, 50, 125, 150) Math.round(5.7) (new ACartesianPoint(25, 50)).mid(25, 50, 125, 150) (new Math()).round(5.7) Class Method Instance Method Accesses no instance variable

25 C LASS VS. I NSTANCE M ETHOD Instance method has all the privileges of a class method Any class method can be made an instance method Bad style to have instance method that does not access any instance variable They belong to the class Violate least privilege principle Require needless instantiation

26 C LASS VS. I NSTANCE C ONSTANT /M ETHOD Named constants should be static Unless some good reason for hiding named constants from static methods Methods not accessing instance variables should be static Unless need to be listed in interface

27 C LASS M EMBER R ESTRICTIONS Cannot define interfaces for classes No conceptual reason why not Non object oriented languages supported them Class members go against the idea of OO programming (not instantiated) so treated as second class. Cannot use super in class methods Can name super class directly Non dynamic dispatch (discussed later)

28 R EAL -W ORLD A NALOGY (O-O P ROGRAMMING ) new ACorvette(silver) new ACorvette(red) getMileage()64000 blend(silverCar, redCar); numCarsProduced(); O-O programming with class methods and variables Blueprints as classes would not have modelled class state and operations

29 O-O W ORLD ACartesianPoint Point1 Point2 new ACartesianPoint( 25, 50) new ACartesianPoint( 125, 150) getRadius() 59 mid(25,50, 125,150); mid(point1x,point1y, point2x,point2y); numInstances() 3 O-O programming with class methods and variables

30 G ENERAL C LASS S TRUCTURE Class Var 1 Class Class Method 1 Class Var 2 Instance Var 2 Class Method 2 Instance Method 1 Instance Method 2

31 E XAMPLE OF C LASS W ITH I NSTANCE AND C LASS M ETHODS static int MAX_VALUE Integer static parseInt(String) int value int intValue() Example of Java Class?

32 C LASS WITH NO I NSTANCE M EMBERS ? Class Var 1 Class Class Method 1 Class Var 2 Class Method 2 Creating instances meaningless Example of Java class?

33 M ATH public static final double E Math public static double log (double a) public static final double PI public static double abs (double a)

34 S YSTEM public static final InputStream in System public static void exit (int status) public static final PrintStream out public static long currentTimeMillis () Real-world analogy?

35 R EAL L IFE A NALOGY Warehouse, dealership

36 C LASS WITH O NLY C LASS M ETHODS ? Class Class Method 1 Class Method 2 Example?

37 M ATH public static final double E Math public static double log (double a) public static final double PI public static double abs (double a)

38 M ODIFIED M ATH Math public static double abs (double a) Real-world analogy?

39 R EAL L IFE A NALOGY : P URE S ERVICE Assume nothing stored

40 C LASS WITH ONLY C LASS V ARIABLES ? Class Var 1 Class Class Var 2 Example?

41 S YSTEM public static final InputStream in InputStream public static final InputStream in InputStream System public static void exit (int status) public static final PrintStream out PrintStream public static final PrintStream out PrintStream public static long currentTimeMillis ()

42 M ODIFIED S YSTEM public static final InputStream in InputStream public static final InputStream in InputStream System public static final PrintStream out PrintStream public static final PrintStream out PrintStream Real-world analogy?

43 U NGUARDED A IR H OSE

44 C LASS VS. I NSTANCE O BJECT Should some state and associated methods be accessed as a class or instance object Multiple independent instances of that state will not occur Do not need dynamic dispatch or interfaces to that state A class with only one instance, useful when dynamic dispatch or interface needed As class object E.g: System.in Singleton class

45 C LASS OR I NSTANCE ? Franchise: Multiple franchisees exist, so corresponds to an instance object

46 C LASS OR I NSTANCE ? One of, corresponds to a class object Mom and pop store can later become franchise as it becomes popular and evolves Class objects often converted to instance objects as program becomes popular and evolves When in doubt make it an instance object