GoodOO Programming Practice in Java © Allan C. Milne v15.1.22.

Slides:



Advertisements
Similar presentations
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.
Advertisements

Coding Standard: General Rules 1.Always be consistent with existing code. 2.Adopt naming conventions consistent with selected framework. 3.Use the same.
Java™ How to Program, 9/e Presented by: Dr. José M. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
CS 106 Introduction to Computer Science I 04 / 11 / 2008 Instructor: Michael Eckmann.
CSE 1302 Lecture 8 Inheritance Richard Gesick Figures from Deitel, “Visual C#”, Pearson.
Object-Oriented Application Development Using VB.NET 1 Chapter 8 Understanding Inheritance and Interfaces.
CS 106 Introduction to Computer Science I 11 / 26 / 2007 Instructor: Michael Eckmann.
 In inheritance the child (subclass) chooses its parent (superclass)  Remember - only public or “protected” methods and variables are inherited  Should.
CS 106 Introduction to Computer Science I 04 / 16 / 2010 Instructor: Michael Eckmann.
June 1, 2000 Object Oriented Programming in Java (95-707) Java Language Basics 1 Lecture 3 Object Oriented Programming in Java Language Basics Classes,
CS 106 Introduction to Computer Science I 11 / 15 / 2006 Instructor: Michael Eckmann.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 9 Objects and Classes.
1 Fall 2007ACS-1903 Chapter 6: Classes Classes and Objects Instance Fields and Methods Constructors Overloading of Methods and Constructors Scope of Instance.
Chapter Objectives You should be able to describe: Object-Based Programming Classes Constructors Examples Common Programming Errors.
CS 106 Introduction to Computer Science I 04 / 21 / 2010 Instructor: Michael Eckmann.
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.
Object-Oriented Application Development Using VB.NET 1 Chapter 8 Understanding Inheritance and Interfaces.
Inheritance. Types of Inheritance Implementation inheritance means that a type derives from a base type, taking all the base type’s member fields and.
Creating a class. Fields (instance variables) Fields are the data defined in the class and belonging to each instantiation (object). Fields can change.
Lecture 8 Inheritance Richard Gesick. 2 OBJECTIVES How inheritance promotes software reusability. The concepts of base classes and derived classes. To.
Chapter 5 - Writing a Problem Domain Class Definition1 Chapter 5 Writing a Problem Domain Class Definition.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
Java Language and SW Dev’t
Names Variables Type Checking Strong Typing Type Compatibility 1.
Values, variables and types © Allan C. Milne v
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.
Java Implementation: Part 3 Software Construction Lecture 8.
07 Coding Conventions. 2 Demonstrate Developing Local Variables Describe Separating Public and Private Members during Declaration Explore Using System.exit.
CSCI-383 Object-Oriented Programming & Design Lecture 13.
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.
Lecture Set 11 Creating and Using Classes Part B – Class Features – Constructors, Methods, Fields, Properties, Shared Data.
C# D1 CSC 298 Elements of C# code (part 2). C# D2 Writing a class (or a struct)  Similarly to Java or C++  Fields: to hold the class data  Methods:
Best Practices. Contents Bad Practices Good Practices.
Object Based Programming Chapter 8. 2 In This Chapter We will learn about classes Garbage Collection Data Abstraction and encapsulation.
Android How to Program, 2/e © Copyright by Pearson Education, Inc. All Rights Reserved.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
Object Oriented Software Development
Visual C# 2012 for Programmers © by Pearson Education, Inc. All Rights Reserved.
1 Chapter Four Creating and Using Classes. 2 Objectives Learn about class concepts How to create a class from which objects can be instantiated Learn.
Chapter 3 Part I. 3.1 Introduction Programs written in C ◦ All statements were located in function main Programs written in C++ ◦ Programs will consist.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Java Class Structure. Class Structure package declaration import statements class declaration class (static) variable declarations* instance variable.
CIS 270—Application Development II Chapter 8—Classes and Objects: A Deeper Look.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter 9 Objects and Classes.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
More about Java Classes Writing your own Java Classes More about constructors and creating objects.
Chapter 11: Advanced Inheritance Concepts. Objectives Create and use abstract classes Use dynamic method binding Create arrays of subclass objects Use.
Topic 8Classes, Objects and Methods 1 Topic 8 l Class and Method Definitions l Information Hiding and Encapsulation l Objects and Reference Classes, Objects,
Classes, Interfaces and Packages
FIT Objectives By the end of this lecture, students should: understand the role of constructors understand how non-default constructors are.
Chapter 4: More Object Concepts. Objectives Understand blocks and scope Overload a method Avoid ambiguity Create and call constructors with parameters.
Jozef Goetz Credits: Copyright  Pearson Education, Inc. All rights reserved. expanded by J. Goetz, 2016.
Programming Fundamentals Enumerations and Functions.
Class Fundamentals BCIS 3680 Enterprise Programming.
Lecture 5:Interfaces and Abstract Classes Michael Hsu CSULA.
Mr H Kandjimi 2016/01/03Mr Kandjimi1 Week 3 –Modularity in C++
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
Lecture 6:Interfaces and Abstract Classes Michael Hsu CSULA.
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 Programming. Constructors  Constructors are like special methods that are called implicitly as soon as an object is instantiated (i.e.
Classes (Part 1) Lecture 3
Java Programming Language
Lecture 22 Inheritance Richard Gesick.
Object Oriented Programming in java
Chapter 14 Abstract Classes and Interfaces
Creating and Using Classes
Presentation transcript:

GoodOO Programming Practice in Java © Allan C. Milne v

Agenda.  Good programming practice.  State and constructors.  Behaviour and methods.  The final keyword.  Enum types.

Review source code. This presentation will ask questions that require you to review the source code for the TiledMap3_0 class. This will be handed out in the lecture, or you can download it from the link in the practical page. Good practice is based around treating a source code file as a readable entity in its own right; –not just looking at the bits and pieces of code you might be interested in at some particular point.

Header commentary. Should be able to write this before you start coding. Keep up to date as your code is modified. Why is each piece of information included?

Layout. An IDE (e.g. Eclipse) should handle the indentation for you in an intelligent way; –why is indentation important? You are responsible for how much you put on each line and where and how many blank lines there are; –these can be useful in partitioning your code but can also be very distracting if over-used.

Naming convention. You are responsible for naming variables, fields, enumerations, methods, etc. Use meaningful names. Use a consistent naming convention. Consider how the names are used, not just where they are declared. What conventions have I used?

Comments. Comments are the internal documentation of your system. Don't overdo the comments; –so don't state the obvious; –but do explain the unusual. Explain things that are problem-oriented (rather than programming). comments can be used to automatically generate external documentation; –e.g. javadoc.

Read and appraise. Always read over your own source code, not just for the coding but also the comments, layout and naming. Compare the source handed out in this lecture with the TiledMap2_0 class code presented previously; –I trust you know which one is better; –but why?

Literal constants. What are they? It is bad practice to embed these within method body code. –Why? –What is the alternative? Think about the maintainability and robustness of your code.

State. An object has an internal state. –Normally Defined by fields. Always make fields private; –If a client needs access then expose a public getter and/or setter method. –Why is it bad practice to have public fields? Differentiate between fields that are –fundamental to the object being modelled; and –Part of the internal implementation.

Constructor methods. Constructors define how objects of the class can be created (or instantiated). Overloaded constructors allow alternative client usage. The role of a constructor is normally to initialise the state of the object; –i.e. the values of the fields. The TiledMap3_0 constructor fully defines the initial state of an object.

What About TiledMap3_0() ? Would initialise an “empty” object. Consider the implications of this: –Does an empty object have a meaning in the problem domain? –fields now require setter properties. –Compromises security with respect to read-only fields. Often a sign of laziness in design.

Get or Set? The combination of get and set for a field relates to whether it is –read-only, –write-only, or –read-write. Choose the get/set for good reasons; –Think about how it is used; –What attribute of the entity being modelled does it represent? –Does the access reflect how this attribute is exposed by the real entity?

Consider validation. A setter method should validate that the value being set is valid. If invalid then what should the setter do?. –Do nothing. –Set the field to some default value. –Throw an exception. What about the set(x,y,value) setter in TiledMap3_0?

Behaviour and methods. Public methods define the behaviour that objects of the class exhibit; –the public API. These should reflect the behaviour of the entity being modelled. Think first about WHAT the method does; –This defines the method signature. Only then think about HOW the method will do it. –This is the implementation of the method body.

We need to know … what a method does; –reflected in the name. what information it requires; –defined by the formal parameters. what the method returns; –its return value type. This information forms the method signature.

Document this. It is good practice to document this for each method via comments. Remember the reader is assumed to be able to read the Java code so document –the role of the method; –what the parameters represent; –what the return value means. –all this in the context of the problem domain, not Java code.

The final keyword. Use liberally in your code to –document your menaing; –allow the Java compiler to optimise. –The use of this keyword has different meanings for different Java components.

final classes. The class cannot be subclassed. All methods are implicitly final. Makes a secure class that you know cannot be compromised by a subclass overriding its state and behaviour. Allows for efficient representation by the compiler.

final methods. A final method cannot be overridden or hidden in a subclass. Use this to ensure that required implementation of behaviour is not compromised by a subclass.

final variables and fields. Their values cannot be changed after initialization: –in their declaration; –by an assignment statement. final fields must be initialized by all constructors. If they are of a reference type then it is the reference that is final; –the object referred to can still be changed.

Enum types. An enum is a user-defined type that defines a set of named constant values for that type; –variables or fields of an enum type can only contain these defined enum values. Why are enumerations useful? –Readability, –security, –maintainability.

Enumeration surprises. Java provides a different structure to enumerations than in other languages. Each enumeration value can also be associated with additional state; –private constructor; –private field(s); –public getter(s) for the state. The enum value is as before but there will now be additional getter(s) that can access the additional state for a specific value.

public enum Gender { MALE (“Mr.”), FEMALE (“Ms.”); private final String mPrefix; private Gender (String aPrefix) { mPrefix = aPrefix; } public String prefix() { return mPrefix; } } // end Gender enum type.

Enum in the tiled map. Representing a map as a 2D array of booleans only represents whether or not a tile is passable or impassable. Define enum Terraintype to represent tiles of grass, water, tree, etc. Represent the map by a 2D array of Terraintype values: –private final TerrainType[][] mMap;

Remember … An enumeration is a type; –It also defines valid values for that type. The enum type can then be used to declare the types of variables, fields, parameters, return values, etc. So make sure you differentiate between –The enum type; TerrainType –A value of this type. E.g. TerrainType.GRASS

You are now expected to… … include appropriate commentary in your source code; … layout your source code in a readable manner; … use a meaningful naming convention; … avoid the use of literal constants in method code; … provide appropriate constructor methods; … use enum types where appropriate.