Java - 1cm Summer 2013 · 2013. 7. 18. · Static Instance Methods • non-static methods are...
Transcript of Java - 1cm Summer 2013 · 2013. 7. 18. · Static Instance Methods • non-static methods are...
Lecture 5
Java...
Summer 2013
M. Jason HinekCarleton University
Day 5
· a quick look back (day 4)
· assignments
· Java and oop· static· inheritance and constructors· final (again)· abstract
· a quick look ahead (day 6)
2 / 20
Last time...
The material in Day 4 touched on
· syntactic sugar
· array and String initialization· int[] intArray1 = new int[]{12, 1, 3, 7};· int[] intArray2 = {12, 1, 3, 7};· String s = "cat";
· mutable and immutable
· arrays are mutable· Strings are immutable· Java keyword final
· abstract data type
· a collection of data and a set of operations on that data
· Bunnies and static attributes
3 / 20
Last time...
The material in Day 4 touched on
· syntactic sugar· array and String initialization· int[] intArray1 = new int[]{12, 1, 3, 7};· int[] intArray2 = {12, 1, 3, 7};· String s = "cat";
· mutable and immutable
· arrays are mutable· Strings are immutable· Java keyword final
· abstract data type
· a collection of data and a set of operations on that data
· Bunnies and static attributes
3 / 20
Last time...
The material in Day 4 touched on
· syntactic sugar· array and String initialization· int[] intArray1 = new int[]{12, 1, 3, 7};· int[] intArray2 = {12, 1, 3, 7};· String s = "cat";
· mutable and immutable· arrays are mutable· Strings are immutable· Java keyword final
· abstract data type
· a collection of data and a set of operations on that data
· Bunnies and static attributes
3 / 20
Last time...
The material in Day 4 touched on
· syntactic sugar· array and String initialization· int[] intArray1 = new int[]{12, 1, 3, 7};· int[] intArray2 = {12, 1, 3, 7};· String s = "cat";
· mutable and immutable· arrays are mutable· Strings are immutable· Java keyword final
· abstract data type· a collection of data and a set of operations on that data
· Bunnies and static attributes
3 / 20
Assignments
· assignment 4· is done! yea!
· assignment 3· due this Friday at 6pm
· assignments 5 and 6· due next week· mess around with the due dates like this week
4 / 20
Assignments
· assignment 4· is done! yea!
· assignment 3· due this Friday at 6pm
· assignments 5 and 6· due next week· mess around with the due dates like this week
4 / 20
Assignments
· assignment 4· how many bugs did you �nd?
· what happens when b is false and y ≤ x ≤ 2y
5 / 20
Assignments
· assignment 4· how many bugs did you �nd?
· what happens when b is false and y ≤ x ≤ 2y
5 / 20
Static
let's look at the non-access modi�er static:
consider the following Bunny class
public class Bunny{
/* attributes */
private static int count = 0;
public String name;
public double life;
/* methods */
public static int countBunnies(){...}
public void eat(double foodAmount){...}
...
}
6 / 20
Static
Instance Methods
· non-static methods are instance methods
· they are bound to instances of the class (i.e., objects)
· can only be called by an instance of the class (objects)
"cat".toUpperCase() or bunny23.eat(1.2)
3
String.toUppercase() or Bunny.eat(0.3)
7
· must be called from an object using dot "." operator
Static Methods
· static methods belong to the class not any objects of the class
· Java needs to know which class the functions belongs to
String.valueOf(12), Math.cos(1), Bunny.countBunnies()
3
"cat".valueOf(12), bunny23.countBunnies()
3
warning: static methods cannot access instance attributes
7 / 20
Static
Instance Methods
· non-static methods are instance methods
· they are bound to instances of the class (i.e., objects)
· can only be called by an instance of the class (objects)
"cat".toUpperCase() or bunny23.eat(1.2)
3
String.toUppercase() or Bunny.eat(0.3)
7
· must be called from an object using dot "." operator
Static Methods
· static methods belong to the class not any objects of the class
· Java needs to know which class the functions belongs to
String.valueOf(12), Math.cos(1), Bunny.countBunnies()
3
"cat".valueOf(12), bunny23.countBunnies()
3
warning: static methods cannot access instance attributes
7 / 20
Static
Instance Methods
· non-static methods are instance methods
· they are bound to instances of the class (i.e., objects)
· can only be called by an instance of the class (objects)
"cat".toUpperCase() or bunny23.eat(1.2)
3
String.toUppercase() or Bunny.eat(0.3)
7
· must be called from an object using dot "." operator
Static Methods
· static methods belong to the class not any objects of the class
· Java needs to know which class the functions belongs to
String.valueOf(12), Math.cos(1), Bunny.countBunnies()
3
"cat".valueOf(12), bunny23.countBunnies()
3
warning: static methods cannot access instance attributes
7 / 20
Static
Instance Methods
· non-static methods are instance methods
· they are bound to instances of the class (i.e., objects)
· can only be called by an instance of the class (objects)
"cat".toUpperCase() or bunny23.eat(1.2)
3
String.toUppercase() or Bunny.eat(0.3)
7
· must be called from an object using dot "." operator
Static Methods
· static methods belong to the class not any objects of the class
· Java needs to know which class the functions belongs to
String.valueOf(12), Math.cos(1), Bunny.countBunnies()
3
"cat".valueOf(12), bunny23.countBunnies()
3
warning: static methods cannot access instance attributes
7 / 20
Static
Instance Methods
· non-static methods are instance methods
· they are bound to instances of the class (i.e., objects)
· can only be called by an instance of the class (objects)
"cat".toUpperCase() or bunny23.eat(1.2) 3
String.toUppercase() or Bunny.eat(0.3) 7
· must be called from an object using dot "." operator
Static Methods
· static methods belong to the class not any objects of the class
· Java needs to know which class the functions belongs to
String.valueOf(12), Math.cos(1), Bunny.countBunnies()
3
"cat".valueOf(12), bunny23.countBunnies()
3
warning: static methods cannot access instance attributes
7 / 20
Static
Instance Methods
· non-static methods are instance methods
· they are bound to instances of the class (i.e., objects)
· can only be called by an instance of the class (objects)
"cat".toUpperCase() or bunny23.eat(1.2) 3
String.toUppercase() or Bunny.eat(0.3) 7
· must be called from an object using dot "." operator
Static Methods
· static methods belong to the class not any objects of the class
· Java needs to know which class the functions belongs to
String.valueOf(12), Math.cos(1), Bunny.countBunnies()
3
"cat".valueOf(12), bunny23.countBunnies()
3
warning: static methods cannot access instance attributes
7 / 20
Static
Instance Methods
· non-static methods are instance methods
· they are bound to instances of the class (i.e., objects)
· can only be called by an instance of the class (objects)
"cat".toUpperCase() or bunny23.eat(1.2) 3
String.toUppercase() or Bunny.eat(0.3) 7
· must be called from an object using dot "." operator
Static Methods
· static methods belong to the class not any objects of the class
· Java needs to know which class the functions belongs to
String.valueOf(12), Math.cos(1), Bunny.countBunnies()
3
"cat".valueOf(12), bunny23.countBunnies()
3
warning: static methods cannot access instance attributes
7 / 20
Static
Instance Methods
· non-static methods are instance methods
· they are bound to instances of the class (i.e., objects)
· can only be called by an instance of the class (objects)
"cat".toUpperCase() or bunny23.eat(1.2) 3
String.toUppercase() or Bunny.eat(0.3) 7
· must be called from an object using dot "." operator
Static Methods
· static methods belong to the class not any objects of the class
· Java needs to know which class the functions belongs to
String.valueOf(12), Math.cos(1), Bunny.countBunnies()
3
"cat".valueOf(12), bunny23.countBunnies()
3
warning: static methods cannot access instance attributes
7 / 20
Static
Instance Methods
· non-static methods are instance methods
· they are bound to instances of the class (i.e., objects)
· can only be called by an instance of the class (objects)
"cat".toUpperCase() or bunny23.eat(1.2) 3
String.toUppercase() or Bunny.eat(0.3) 7
· must be called from an object using dot "." operator
Static Methods
· static methods belong to the class not any objects of the class
· Java needs to know which class the functions belongs to
String.valueOf(12), Math.cos(1), Bunny.countBunnies() 3
"cat".valueOf(12), bunny23.countBunnies() 3
warning: static methods cannot access instance attributes
7 / 20
Static
Instance Methods
· non-static methods are instance methods
· they are bound to instances of the class (i.e., objects)
· can only be called by an instance of the class (objects)
"cat".toUpperCase() or bunny23.eat(1.2) 3
String.toUppercase() or Bunny.eat(0.3) 7
· must be called from an object using dot "." operator
Static Methods
· static methods belong to the class not any objects of the class
· Java needs to know which class the functions belongs to
String.valueOf(12), Math.cos(1), Bunny.countBunnies() 3
"cat".valueOf(12), bunny23.countBunnies() 3
warning: static methods cannot access instance attributes
7 / 20
Static
Instance Attributes
· non-static attributes are instance attributes
· they are bound to instances of the class (i.e., objects)
· can only be accessed by an instance of the class (objects)
studentObject.name or bunny17.life
3
Student.name or Bunny.life
7
· must be accessed from an object using dot "." operator
Static Attributes
· static attributes belong to the class not any objects of the class
· Java needs to know which class the attribute belongs to
Bunny.count or Math.PI
3
bunny23.count
3
note: static attributes are initialized at compile time
8 / 20
Static
Instance Attributes
· non-static attributes are instance attributes
· they are bound to instances of the class (i.e., objects)
· can only be accessed by an instance of the class (objects)
studentObject.name or bunny17.life
3
Student.name or Bunny.life
7
· must be accessed from an object using dot "." operator
Static Attributes
· static attributes belong to the class not any objects of the class
· Java needs to know which class the attribute belongs to
Bunny.count or Math.PI
3
bunny23.count
3
note: static attributes are initialized at compile time
8 / 20
Static
Instance Attributes
· non-static attributes are instance attributes
· they are bound to instances of the class (i.e., objects)
· can only be accessed by an instance of the class (objects)
studentObject.name or bunny17.life
3
Student.name or Bunny.life
7
· must be accessed from an object using dot "." operator
Static Attributes
· static attributes belong to the class not any objects of the class
· Java needs to know which class the attribute belongs to
Bunny.count or Math.PI
3
bunny23.count
3
note: static attributes are initialized at compile time
8 / 20
Static
Instance Attributes
· non-static attributes are instance attributes
· they are bound to instances of the class (i.e., objects)
· can only be accessed by an instance of the class (objects)
studentObject.name or bunny17.life
3
Student.name or Bunny.life
7
· must be accessed from an object using dot "." operator
Static Attributes
· static attributes belong to the class not any objects of the class
· Java needs to know which class the attribute belongs to
Bunny.count or Math.PI
3
bunny23.count
3
note: static attributes are initialized at compile time
8 / 20
Static
Instance Attributes
· non-static attributes are instance attributes
· they are bound to instances of the class (i.e., objects)
· can only be accessed by an instance of the class (objects)
studentObject.name or bunny17.life 3
Student.name or Bunny.life 7
· must be accessed from an object using dot "." operator
Static Attributes
· static attributes belong to the class not any objects of the class
· Java needs to know which class the attribute belongs to
Bunny.count or Math.PI
3
bunny23.count
3
note: static attributes are initialized at compile time
8 / 20
Static
Instance Attributes
· non-static attributes are instance attributes
· they are bound to instances of the class (i.e., objects)
· can only be accessed by an instance of the class (objects)
studentObject.name or bunny17.life 3
Student.name or Bunny.life 7
· must be accessed from an object using dot "." operator
Static Attributes
· static attributes belong to the class not any objects of the class
· Java needs to know which class the attribute belongs to
Bunny.count or Math.PI
3
bunny23.count
3
note: static attributes are initialized at compile time
8 / 20
Static
Instance Attributes
· non-static attributes are instance attributes
· they are bound to instances of the class (i.e., objects)
· can only be accessed by an instance of the class (objects)
studentObject.name or bunny17.life 3
Student.name or Bunny.life 7
· must be accessed from an object using dot "." operator
Static Attributes
· static attributes belong to the class not any objects of the class
· Java needs to know which class the attribute belongs to
Bunny.count or Math.PI
3
bunny23.count
3
note: static attributes are initialized at compile time
8 / 20
Static
Instance Attributes
· non-static attributes are instance attributes
· they are bound to instances of the class (i.e., objects)
· can only be accessed by an instance of the class (objects)
studentObject.name or bunny17.life 3
Student.name or Bunny.life 7
· must be accessed from an object using dot "." operator
Static Attributes
· static attributes belong to the class not any objects of the class
· Java needs to know which class the attribute belongs to
Bunny.count or Math.PI
3
bunny23.count
3
note: static attributes are initialized at compile time
8 / 20
Static
Instance Attributes
· non-static attributes are instance attributes
· they are bound to instances of the class (i.e., objects)
· can only be accessed by an instance of the class (objects)
studentObject.name or bunny17.life 3
Student.name or Bunny.life 7
· must be accessed from an object using dot "." operator
Static Attributes
· static attributes belong to the class not any objects of the class
· Java needs to know which class the attribute belongs to
Bunny.count or Math.PI 3
bunny23.count 3
note: static attributes are initialized at compile time
8 / 20
Static
Instance Attributes
· non-static attributes are instance attributes
· they are bound to instances of the class (i.e., objects)
· can only be accessed by an instance of the class (objects)
studentObject.name or bunny17.life 3
Student.name or Bunny.life 7
· must be accessed from an object using dot "." operator
Static Attributes
· static attributes belong to the class not any objects of the class
· Java needs to know which class the attribute belongs to
Bunny.count or Math.PI 3
bunny23.count 3
note: static attributes are initialized at compile time
8 / 20
Static
consider the following Bunny class
public class Bunny{
/* attributes */
private static int count = 0;
public String name;
public double life;
/* methods */
public static int countBunnies(){...}
public void eat(double foodAmount){...}
...
}
9 / 20
Good Programming Practice in Java
public static final double PI = 3.14159;
private static int count = 0;
· public static attributes should all be final
· everyone has access to this attributes· the class has no control over changes to non-�nal public values· use for �constants� like Math.E
· non-final static attributes should be private
· the class controls changes of values· anyone can change
10 / 20
Good Programming Practice in Java
public static final double PI = 3.14159;
private static int count = 0;
· public static attributes should all be final
· everyone has access to this attributes· the class has no control over changes to non-�nal public values· use for �constants� like Math.E
· non-final static attributes should be private
· the class controls changes of values· anyone can change
10 / 20
Good Programming Practice in Java
public static final double PI = 3.14159;
private static int count = 0;
· public static attributes should all be final
· everyone has access to this attributes· the class has no control over changes to non-�nal public values· use for �constants� like Math.E
· non-final static attributes should be private
· the class controls changes of values· anyone can change
10 / 20
Good Programming Practice in Java
public static final double PI = 3.14159;
private static int count = 0;
· public static attributes should all be final
· everyone has access to this attributes· the class has no control over changes to non-�nal public values· use for �constants� like Math.E
· non-final static attributes should be private
· the class controls changes of values· anyone can change
10 / 20
Good Programming Practice in Java
public static final double PI = 3.14159;
private static int count = 0;
· public static attributes should all be final
· everyone has access to this attributes· the class has no control over changes to non-�nal public values· use for �constants� like Math.E
· non-final static attributes should be private
· the class controls changes of values· anyone can change
10 / 20
let's take a break...for 5 minutes
11 / 20
Inheritance and Constructors
public class A{
...
}
public class B extends A{
...
}
12 / 20
Inheritance and Constructors
public class A{
...
}
public class B extends A{
...
}
when a constructor is called:
· if �rst line is super(...)· Java calls speci�ed parent class constructor
· else if �rst line is this(...)· Java calls speci�ed constructor in current class
· otherwise· Java calls zero-argument constructor of parent class
· this recursively works it way up to Object
13 / 20
Constructors
a few things about constructors...
· constructor are not methods· we think of them as special methods for initialization (creation)
· constructors are never inherited
· access to constructors is governed by access modi�ers· they can be public, private and protected
· default zero-argument constructors provided i� none speci�ed
· parent's zero-argument constructor called by default(unless super() or this() is �rst line)
14 / 20
super and this
· this used in the body of an instance method· a reference to the current object· this.x, this.update(3)
· this() as the �rst line of a constructor· calls constructor in the current class based on the input arguments· this("cat", 12);
· super() as the �rst line of a constructor· calls parent class constructor based on the input arguments· super("cat", 12);
· super used in the body of a method· calls overridden parent class method· super.toString()
15 / 20
super and this
· this used in the body of an instance method· a reference to the current object· this.x, this.update(3)
· this() as the �rst line of a constructor· calls constructor in the current class based on the input arguments· this("cat", 12);
· super() as the �rst line of a constructor· calls parent class constructor based on the input arguments· super("cat", 12);
· super used in the body of a method· calls overridden parent class method· super.toString()
15 / 20
super and this
· this used in the body of an instance method· a reference to the current object· this.x, this.update(3)
· this() as the �rst line of a constructor· calls constructor in the current class based on the input arguments· this("cat", 12);
· super() as the �rst line of a constructor· calls parent class constructor based on the input arguments· super("cat", 12);
· super used in the body of a method· calls overridden parent class method· super.toString()
15 / 20
�nal
let's look at the non-access modi�er final:
public [final] class A{
...
[final] double PI = 3.1;
...
[final] int addMe(int x){...}
...
}
· final attributes· once de�ned, cannot change primitive value or reference· must be de�ned in program
· final methods· cannot be overridden in a subclass
· final classes· this class cannot be extended (it cannot have subclasses)
16 / 20
�nal
let's look at the non-access modi�er final:
public [final] class A{
...
[final] double PI = 3.1;
...
[final] int addMe(int x){...}
...
}
· final attributes· once de�ned, cannot change primitive value or reference· must be de�ned in program
· final methods· cannot be overridden in a subclass
· final classes· this class cannot be extended (it cannot have subclasses)
16 / 20
�nal
let's look at the non-access modi�er final:
public [final] class A{
...
[final] double PI = 3.1;
...
[final] int addMe(int x){...}
...
}
· final attributes· once de�ned, cannot change primitive value or reference· must be de�ned in program
· final methods· cannot be overridden in a subclass
· final classes· this class cannot be extended (it cannot have subclasses)
16 / 20
�nal
let's look at the non-access modi�er final:
public [final] class A{
...
[final] double PI = 3.1;
...
[final] int addMe(int x){...}
...
}
· final attributes· once de�ned, cannot change primitive value or reference· must be de�ned in program
· final methods· cannot be overridden in a subclass
· final classes· this class cannot be extended (it cannot have subclasses)
16 / 20
abstract
let's look at the non-access modi�er abstract:
the abstract Java keyword is used for methods and classes
· an abstract class cannot be instantiated(a concrete class can be instantiated)
· there can be no objects of this class· the intent is for some children classes to be non-abstract
· an abstract method cannot be final
public abstract class MyAbstractClass{
...
usefull stuff...
...
}
17 / 20
abstract
let's look at the non-access modi�er abstract:
the abstract Java keyword is used for methods and classes
· an abstract class cannot be instantiated(a concrete class can be instantiated)
· there can be no objects of this class· the intent is for some children classes to be non-abstract
· an abstract method cannot be final
public abstract class MyAbstractClass{
...
usefull stuff...
...
}
17 / 20
abstract
let's look at the non-access modi�er abstract:
the abstract Java keyword is used for methods and classes
· an abstract class cannot be instantiated(a concrete class can be instantiated)
· there can be no objects of this class· the intent is for some children classes to be non-abstract
· an abstract method cannot be final
public abstract class MyAbstractClass{
...
usefull stuff...
...
}
17 / 20
abstract
let's look at the non-access modi�er abstract:
the abstract Java keyword is used for methods and classes
· an abstract class cannot be instantiated(a concrete class can be instantiated)
· there can be no objects of this class· the intent is for some children classes to be non-abstract
· an abstract method cannot be final
public abstract class MyAbstractClass{
...
usefull stuff...
...
}
17 / 20
abstract
let's look at the non-access modi�er abstract:
the abstract Java keyword is used for methods and classes
· an abstract class cannot be instantiated(a concrete class can be instantiated)
· there can be no objects of this class· the intent is for some children classes to be non-abstract
· an abstract method cannot be final
public abstract class MyAbstractClass{
...
usefull stuff...
...
}
17 / 20
abstract
let's look at the non-access modi�er abstract:
the abstract Java keyword is used for methods and classes
· an abstract method has no de�nition· public abstract int foo(String in);
private abstract boolean bar();
(no curly braces and ends with a semicolon)
· method will be overridden in a (possibly) non-abstract child class
· an abstract method cannot be �nal
· a class must be abstract if there is an abstract method in it(an abstract class does not have to have an abstract method)
· a method can be one of abstract, static, native or none
18 / 20
abstract
let's look at the non-access modi�er abstract:
the abstract Java keyword is used for methods and classes
· an abstract method has no de�nition· public abstract int foo(String in);
private abstract boolean bar();
(no curly braces and ends with a semicolon)
· method will be overridden in a (possibly) non-abstract child class
· an abstract method cannot be �nal
· a class must be abstract if there is an abstract method in it(an abstract class does not have to have an abstract method)
· a method can be one of abstract, static, native or none
18 / 20
abstract
let's look at the non-access modi�er abstract:
the abstract Java keyword is used for methods and classes
· an abstract method has no de�nition· public abstract int foo(String in);
private abstract boolean bar();
(no curly braces and ends with a semicolon)
· method will be overridden in a (possibly) non-abstract child class
· an abstract method cannot be �nal
· a class must be abstract if there is an abstract method in it(an abstract class does not have to have an abstract method)
· a method can be one of abstract, static, native or none
18 / 20
abstract
let's look at the non-access modi�er abstract:
the abstract Java keyword is used for methods and classes
· an abstract method has no de�nition· public abstract int foo(String in);
private abstract boolean bar();
(no curly braces and ends with a semicolon)
· method will be overridden in a (possibly) non-abstract child class
· an abstract method cannot be �nal
· a class must be abstract if there is an abstract method in it(an abstract class does not have to have an abstract method)
· a method can be one of abstract, static, native or none
18 / 20
abstract
let's look at the non-access modi�er abstract:
the abstract Java keyword is used for methods and classes
· an abstract method has no de�nition· public abstract int foo(String in);
private abstract boolean bar();
(no curly braces and ends with a semicolon)
· method will be overridden in a (possibly) non-abstract child class
· an abstract method cannot be �nal
· a class must be abstract if there is an abstract method in it(an abstract class does not have to have an abstract method)
· a method can be one of abstract, static, native or none
18 / 20
abstract
let's look at the non-access modi�er abstract:
the abstract Java keyword is used for methods and classes
· an abstract method has no de�nition· public abstract int foo(String in);
private abstract boolean bar();
(no curly braces and ends with a semicolon)
· method will be overridden in a (possibly) non-abstract child class
· an abstract method cannot be �nal
· a class must be abstract if there is an abstract method in it(an abstract class does not have to have an abstract method)
· a method can be one of abstract, static, native or none
18 / 20
abstract
insert pic of rock, paper and scissors
19 / 20
let's take a break...for 10 minutes
20 / 20