Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSC 1601 Exam 1 Review. Topics  javadoc  Advanced Java I/O  Objects  References  Static variables and methods  Wrapper classes  Class parameters.

Similar presentations


Presentation on theme: "CSC 1601 Exam 1 Review. Topics  javadoc  Advanced Java I/O  Objects  References  Static variables and methods  Wrapper classes  Class parameters."— Presentation transcript:

1 CSC 1601 Exam 1 Review

2 Topics  javadoc  Advanced Java I/O  Objects  References  Static variables and methods  Wrapper classes  Class parameters  Inheritance  Encapsulation  Access control  Debugging  the Object class  Polymorphism  Late binding  Up/down casting  Abstract methods & classes  Interfaces  Exceptions

3 javadoc  What is the difference between a Java compiler and javadoc? What is the output of a compiler?What is the output of a compiler? What is the output of javadoc?What is the output of javadoc?  How do we control this output?

4 Advanced Java I/O  Output: print, println, printf  Input: the Scanner class

5 Objects  Instance (value) of the class = a particular object (a particular value of a class type)  A class is a type and you can declare variables of a class type.  Attributes, fields, or properties = data  Methods = function  Members = data + functions (or attributes + methods)  Defining a class vs. declaring an instance of a class

6 Objects  Method definition vs. method invocation  Class variables vs. local variables maskingmasking  this keyword and parameter  == and object

7 Information hiding & encapsulation  separate how to use the class from the implementation details  grouping software into a unit in such a way that it is easy to use because there is a well-defined simple interface  data and actions are combined into a single item (class object) and the details of the implementation are hidden

8 API  Application Programming Interface  description of how to use the class

9 ADT  Abstract Data Type data type that is written using good information-hiding techniquesdata type that is written using good information-hiding techniques

10 Access modifiers  public = no access restrictions at all  protected (to be discussed in the future)  private = the instance variable or method cannot be accessed outside of the class definition

11 Types of methods 1. Accessor – method that allows one to obtain (a copy of) the (often private or protected) data in a class 2. Mutator – method that allows one to change the data (often private or protected) in a class

12 Preconditions and postconditions  Precondition states what is assumed to be true when a method is invokedstates what is assumed to be true when a method is invoked  Postcondition Describes the effect of the method callDescribes the effect of the method call States what will be true after the method is invoked (assuming that the preconditions have been met)States what will be true after the method is invoked (assuming that the preconditions have been met)

13 Overloading  Simply methods w/ the same name but different parameters.  Rules: 1.Same name for each 2.Different number of parameters and/or parameter types. 3.All must have the exact same return type.  Note: Java only allows methods to be overloaded. (Some languages allow operators to be overloaded as well.)

14 Constructors (ctors)  Rules: 1.Method w/ same name as class name. 2.May have more than one ctor.  w/ 0 or more different arguments  You get classname() by default. 3.No return type may be specified (including void). 4.Invoked via new. Copy ctor. Copy ctor.

15 Copy constructor  Ctor w/ a single argument of the same type as the class.  Should create an object that is a separate, independent object that is an exact copy of the argument object.

16 References  Variables are stored in consecutive bytes of memory.  So variables can be referred to by their value (copy) or by their reference (address).  In Java, variables of object types are references (addresses); primitive variables are values (copies).

17 Static variables and methods  Does not have/require a calling object (class instance). Static members can’t refer to non static membersStatic members can’t refer to non static members Use class name to refer to the static members.Use class name to refer to the static members.

18 Static variables  belongs to the class as a whole – not just one object (not only one copy)  can be used to communicate between objects  one object changes it and it changes for every object  automatically initialized (to 0, false, null)  also useful for defining constants What keyword do we use for this?What keyword do we use for this?

19 Wrapper classes  Primitive types Not “first class” objectsNot “first class” objects int, double, float, etc.int, double, float, etc.  Wrapper classes Integer, Double, etc.Integer, Double, etc. Lack “no argument” ctorsLack “no argument” ctors May also have static membersMay also have static members

20 Wrapper classes  Boxing The process of going from a value of a primitive type to an object of its wrapper class.The process of going from a value of a primitive type to an object of its wrapper class. Integer iObj = new Integer( 42 );Integer iObj = new Integer( 42 );  Unboxing The process of going from an object of a wrapper class to the corresponding value of a primitive type.The process of going from an object of a wrapper class to the corresponding value of a primitive type. int I = iObj.intValue();int I = iObj.intValue();

21 Class parameters  Passing parameters to functions: Primitive types are passed (call) by value.Primitive types are passed (call) by value. Class types are passed (call) by reference.Class types are passed (call) by reference.

22 Privacy leaks  When private things become not so private!

23 Mutable and immutable classes  Immutable class = doesn’t contain any methods that change any of the data in an object of the class  Mutable class = contains public mutator methods or other public methods that can change the data in an object of the class Rule: Never return a reference to a mutable private object.Rule: Never return a reference to a mutable private object.

24 Deep copy vs. shallow copy  Deep copy Copy that, with one exception, has no references in common with the original objectCopy that, with one exception, has no references in common with the original object Exception: references to immutable objects are allowed to be sharedException: references to immutable objects are allowed to be shared  Shallow copy = a copy that is not deep

25 Inheritance  New class (derived class) is created from another class (base class).

26 Inheritance  Base class = parent class = ancestor = superclass  Derived class = child class = descendent = subclass  Public and protected attributes and methods in the base class are available to (inherited by) the derived class.  super ctor vs. this ctor

27 Public and private  Derived class can “loosen” access restrictions. Derived class can change private to public.Derived class can change private to public.  Derived class can’t “tighten” them. Derived class can’t change public to private.Derived class can’t change public to private.

28 Types  An object of a derived class has more than one type. Not only its type but also the type of every one of its ancestors (all the way back to Object).Not only its type but also the type of every one of its ancestors (all the way back to Object). Object is the base class for classes that don’t extend anything. Object is the ancestor of all classes.Object is the base class for classes that don’t extend anything. Object is the ancestor of all classes. instanceof operatorinstanceof operator

29 Encapsulation and inheritance  Private instance variables and methods in a base class cannot be directly accessed (by name) in a derived class.  They can be indirectly access (via accessors and mutators in the base class).  It’s exactly as if they don’t exist. (They can actually be defined and redefined in the derived class.)

30 Protected access Protected (rather than public or private) access allows: 1.Access by name inside its own class definition. 2.Access by name inside any class derived from it. 3.Access by name in the definition of any class in the same package (even if the class is not derived from it).

31 Protected access  Access between private and public  Very weak protection  Use is discouraged (use the following instead)

32 Package access  AKA default access or friendly access.  Can be access by name by anything in the package but nothing outside of the package.  This is what you get when you don’t specify either public, private, or protected (hence the name default access).  (If you don’t specify a package, you belong to the default package.)

33 Package access  More restricted than protected. Removes “Access by name inside any class derived from it.” if the derived class is NOT in the same package.Removes “Access by name inside any class derived from it.” if the derived class is NOT in the same package. Packages are analogous to directories (folder). If you control the directory, you control the package.Packages are analogous to directories (folder). If you control the directory, you control the package.

34 Debugging  Driver program  Inserting System.out.print statements  Using the debugger Set breakpointsSet breakpoints Execute program line by lineExecute program line by line Exam variables while runningExam variables while running Change variables while runningChange variables while running

35 Object class  All objects extend (inherit from) Object  The Object class has a number of especially interested methods (that are inherited by our classes) such as: cloneclone equalsequals getClassgetClass toStringtoString

36 The instanceof operator and the getClass method  Recall that the instanceof operator is true “up and down” the inheritance hierarchy.  We need something more specific.  getClass returns a representation of the class that was used with new to create the object Can be compared with == and !=Can be compared with == and != Ex.Ex. if (object1.getClass() == object2.getClass()) System.out.println( “same class.” ); else System.out.println( “not the same class.” );

37 A better equals method public boolean equals ( Object other ) { if (other==null) { return false; } else if (getClass() != other.getClass()) { return false; } else { Employee tmp = (Employee)other; return (name.equals( tmp.name ) &&hireDate.equals( tmp.hireDate ) ); }}

38 3 main programming mechanisms that constitute OOP: 1. Encapsulation 2. Inheritance 3. Polymorphism

39 Polymorphism  The ability to associate many meanings to one method name by means of a special mechanism known as late binding or dynamic binding.  Allows one to make changes in the method definition for the derived classes and have those changes apply to the software written in the base class.

40 Late binding  AKA dynamic binding  Binding – the process of associating a method definition with a method invocation  Early binding – the method definition is associated with the method invocation when the code is compiled; AKA static binding  Late binding – the method invocation is associated with the method invocation when the method is invoked (at run time) Java uses late binding except for a few cases.Java uses late binding except for a few cases.

41 Late binding exceptions  Java does not use late binding with: Private methodsPrivate methods Methods marked finalMethods marked final Static methodsStatic methods  Static binding is used instead.

42 Downcasting and upcasting  Upcast = assigning an object of a derived class to a variable of a base class (or any ancestor class) straightforwardstraightforward  Downcast = a type cast from a base class to a derived class (or from any ancestor class to any descendent class) troublesometroublesome

43 clone() method  defined in Object as: protected Object clone()  every object inherits a clone() method  (supposed to) return a deep copy of the calling object you are expected to override ityou are expected to override it  like a copy ctor but there are cases where clone() works but the copy ctor does not.

44 Abstract method  A placeholder for a method that will be fully defined in a subclass.  An abstract method has a complete method heading with the addition of the keyword abstract.  Cannot be private.

45 Abstract class  A class that has at least one abstract method is called an abstract class.  The class definition must have the keyword abstract.  Ex. abstract public class Feet { … abstract void doSomething ( int count ); …}  A class without any abstract methods is called a concrete class.

46 Points to remember  You cannot create an instance of an abstract class.  An abstract class is a type. Therefore you can use abstract classes as parameters to functions and as variable types for concrete classes derived from the abstract class.

47  Specifies a set of methods (i.e., method headings) that any class that implements that interface must have.  An interface is a type (but is not a class). Interface can be parameter type.Interface can be parameter type.  Java’s way of approximating multiple inheritance.

48  To implement an interface, a concrete class must do: 1.State “implements InterfaceName” or “implements InterfaceName 1, …, InterfaceName n ” 2.You must implement all of the method headings listed in the definition(s) of the interface(s).

49  To implement an interface, an abstract class must do: 1.State “implements InterfaceName” or “implements InterfaceName 1, …, InterfaceName n ” 2.You must either implement all of the method headings listed in the definition(s) of the interface(s) or you must define as abstract the method headings in the interface(s).

50  An interface may extend an interface and specify additional method headings.  Any concrete class that implements the derived interface must implement all of the methods in both interfaces.

51  Constants may be defined in interfaces. Not really in the spirit of an interfaceNot really in the spirit of an interface Must be public static final (and will be even if omitted)Must be public static final (and will be even if omitted) No instance variables in interfacesNo instance variables in interfaces

52 Important interfaces  Cloneable  ActionListener  MouseListener  MouseMotionListener

53 Exceptions & exception handling  Use sparingly.  Things you can do with exceptions: 1.Define a new exception class. 2.Create an exception instance. 3.Throw an exception. 4.Declare that an exception may be thrown (in a particular function). 5.Handle the possibility that an exception may be thrown.

54 Define a new exception class  extend Exception (or extend a subclass of Exception).  This creates a new type.  All have a ctor w/ a single String arg.  Each has an accessor method called getMessage() that returns the String from the ctor arg.

55 Create an exception instance  Ex. new Exception( “Uh oh!” );new Exception( “Uh oh!” ); Exception e = new Exception( “Rats!” );Exception e = new Exception( “Rats!” );

56 Throw an exception  Ex. throw new Exception( “Invalid value.” );throw new Exception( “Invalid value.” ); Exception e = new Exception( “Invalid age.” );Exception e = new Exception( “Invalid age.” ); throw e;throw e;

57 Declare (a method that indicates) that an exception may be thrown  Ex. public int f ( int x ) throws Exception { …}

58 Handle the possibility that an exception may be thrown  The try-catch blocks: try { … } catch (Exception e) { …}


Download ppt "CSC 1601 Exam 1 Review. Topics  javadoc  Advanced Java I/O  Objects  References  Static variables and methods  Wrapper classes  Class parameters."

Similar presentations


Ads by Google