Download presentation
Presentation is loading. Please wait.
1
Computer Science II 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 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 masking 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 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 package (default)
11
Types of methods Accessor – method that allows one to obtain (a copy of) the (often private or protected) data in a class Mutator – method that allows one to change the data (often private or protected) in a class
12
Preconditions and postconditions
states what is assumed to be true when a method is invoked Postcondition Describes the effect of the method call 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: Same name for each Different number of parameters and/or parameter types. 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: Copy ctor.
Method w/ same name as class name. May have more than one ctor. w/ 0 or more different arguments You get classname() by default. No return type may be specified (including void). Invoked via new. 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 members Use class name to refer to the static members.
18
Static variables belong 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?
19
Wrapper classes Primitive types Wrapper classes
not “first class” objects int, double, float, etc. Wrapper classes Integer, Double, etc. lack “no argument” ctors may also have static members
20
Wrapper classes Boxing Unboxing
The process of going from a value of a primitive type to an object of its wrapper class. 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. int I = iObj.intValue();
21
Class parameters Passing parameters to functions:
Primitive types are passed (call) by value. 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.
24
Deep copy vs. shallow copy
Copy that, with one exception, has no references in common with the original object Exception: 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’t “tighten” them. 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). Object is the base class for classes that don’t extend anything. Object is the ancestor of all classes. instanceof 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: Access by name inside its own class definition. Access by name inside any class derived from it. 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. 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 breakpoints execute program line by line exam variables while running change 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: clone equals getClass toString
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 != 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()) { } else { Employee tmp = (Employee)other; return ( name.equals( tmp.name ) && hireDate.equals( tmp.hireDate ) ); }
38
3 main programming mechanisms that constitute OOP:
Encapsulation Inheritance 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.
41
Late binding exceptions
Java does not use late binding with: private methods methods marked final static 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) straightforward Downcast = a type cast from a base class to a derived class (or from any ancestor class to any descendent class) troublesome
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 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
Interfaces 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. Java’s way of approximating multiple inheritance.
48
Interfaces To implement an interface, a concrete class must do:
State “implements InterfaceName” or “implements InterfaceName1, …, InterfaceNamen” You must implement all of the method headings listed in the definition(s) of the interface(s).
49
Interfaces To implement an interface, an abstract class must do:
State “implements InterfaceName” or “implements InterfaceName1, …, InterfaceNamen” 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
Interfaces and interfaces
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 and interfaces
Constants may be defined in interfaces. Not really in the spirit of an interface Must be public static final (and will be even if omitted) No instance variables in interfaces
52
Important interfaces Cloneable ActionListener MouseListener
MouseMotionListener
53
Exceptions & exception handling
Use sparingly. Things you can do with exceptions: Define a new exception class. Create an exception instance. Throw an exception. Declare that an exception may be thrown (in a particular function). 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
new Exception( “Uh oh!” ); Exception e = new Exception( “Rats!” );
56
Throw an exception Ex. throw new Exception( “Invalid value.” );
Exception e = new Exception( “Invalid age.” ); throw e;
57
Declare (a method that indicates) that an exception may be thrown
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) { }
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.