170 likes | 286 Views
Classes and Objects Classes Methods Scope References. Agenda. public class BankAccount { private String myPassword; private double myBalance; public static final double OVERDRAWN_PENALTY=20.0 ; public BankAccount(){…} public BankAccount(String pswd, double bal){..}
E N D
Classes and Objects • Classes • Methods • Scope • References Agenda
public class BankAccount { private String myPassword; private double myBalance; public static final double OVERDRAWN_PENALTY=20.0; public BankAccount(){…} public BankAccount(String pswd, double bal){..} public double getBalance(){..} public void deposit(String pswd, double bal){..} public void withdraw(String pswd, double bal){..} }
Objects • By what is an object characterized? • What is a vehicle’s state and behavior? • What is an object reference?
Classes • What is a class? • What is an object related to the class? • Can a class have more than one instance? • What will a state be represented by in Java? • What will a behavior be presented by in Java? • What is encapsulation? • Look at the bank account example. List the state variables and methods.
public, private and static • What is information hiding? • Can private methods and variables in a class be accessed outside the class? • What is a static variable? • Does a static variable have to be constant? • Can the value of a static variable be changed? • Can the value of a static final variable be changed? • If there is a state(instance) variable public static final double OVERDRAWN_PENALTY; how do we call this method inside the class? how do we call this method in an instance?
Methods • List the method headers(signature). • What 4 types of methods are mentioned in the book? • What is a constructor? • Does default constructors have arguments? • What does a constructor with parameter(s) do? • BankAccount aAcct = new BankAccount(); what is the object variables for above stmt? • What does an object variable store?
Accessor methods • What is an accessor method? • Does an accessor return any information about the object? • Which is the accessor method in the BankAccount class(p86)?
Mutator methods • What is a mutator method? • Which ones in the BankAccount class are mutator methods(p86)? • What does these mutator methods change? • How does a mutator metod got invoked in the main program?
Static methods • What is the differences between static method and instance method? • Suppose there is a class called BankAccount, one public static method called getInterestRate(), one public nonstatic method called getCDRate(), how do you invoke these 2 methods in main()? • What is Drive class? • Is it correct? All the methods in the driver class must be static?
Method overloading • What are overloaded methods? • Can the overloaded methods have different return type? • Are the two methods below overloaded? Why? public int product(int n){return n*n; } public double product(int n){return n*n; }
Scope of a variable or method • What is the scope of a variable or method? • What does a block mean in Java? • What is the scope of instance variables and methods of a class? • Within the class all instance variables and methods are accessed simply by name(no dot operator), correct? • Can state variables and methods of a class be accessed directly even if they are private?
Look at the next slide. Is it allowed that “pswd” is a state variable and also in the parameter list of methods? • Consider the “pswd” variable in constructor and in deposit and withdraw, what type of variables are they? What scope do they have? • How do you distinguish the state variable “pswd” and the local variable “pswd” inside the deposit() method? • If we have this.pswd inside deposit() method, which pswd does it refer to?
public class BankAccount { private String pswd; private double bal; public static final double OVERDRAWN_PENALTY=20.0; public BankAccount(){…} public BankAccount(String pswd, double bal){..} public double getBalance(){..} public void deposit(String pswd, double bal){..} public void withdraw(String pswd, double bal){..} }
References • What are the two data types? • What are reference data types? Name some examples. • What is the value of the object aAcct if we have BancAccount aAcct = new BankAccout(); • If we change any state variable like “pswd” in bAcct, will aAcct’s “pswd” also get changed? Why? • What do you call an uninitialized object variable?
What would happen for the following statement? BankAcct aAcct; double balance = aAcct.getBalance(); • If you fail to initialize a variable before you use it, you get a compile-time error. But if you fail to initialize an instance variable, what happens?
Method parameters • What are formal and actual parameters? • How are the parameters of primitive types passed to a method? • What does passed by value mean?
Homework From the Chapter 2 multiple choice questions you can find some classes without implementaions. Finish the implementations for the following classes: • Time • Date • Rational(skip the reduce() method) • Temperature