61

Java - 1cm Summer 2013 · 2013. 7. 18. · Static Instance Methods • non-static methods are instance methods • they are bound to instances of the class (i.e., objects) • can

  • Upload
    others

  • View
    3

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Java - 1cm Summer 2013 · 2013. 7. 18. · Static Instance Methods • non-static methods are instance methods • they are bound to instances of the class (i.e., objects) • can

Lecture 5

Java...

Summer 2013

M. Jason HinekCarleton University

Page 2: Java - 1cm Summer 2013 · 2013. 7. 18. · Static Instance Methods • non-static methods are instance methods • they are bound to instances of the class (i.e., objects) • can

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

Page 3: Java - 1cm Summer 2013 · 2013. 7. 18. · Static Instance Methods • non-static methods are instance methods • they are bound to instances of the class (i.e., objects) • can

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

Page 4: Java - 1cm Summer 2013 · 2013. 7. 18. · Static Instance Methods • non-static methods are instance methods • they are bound to instances of the class (i.e., objects) • can

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

Page 5: Java - 1cm Summer 2013 · 2013. 7. 18. · Static Instance Methods • non-static methods are instance methods • they are bound to instances of the class (i.e., objects) • can

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

Page 6: Java - 1cm Summer 2013 · 2013. 7. 18. · Static Instance Methods • non-static methods are instance methods • they are bound to instances of the class (i.e., objects) • can

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

Page 7: Java - 1cm Summer 2013 · 2013. 7. 18. · Static Instance Methods • non-static methods are instance methods • they are bound to instances of the class (i.e., objects) • can

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

Page 8: Java - 1cm Summer 2013 · 2013. 7. 18. · Static Instance Methods • non-static methods are instance methods • they are bound to instances of the class (i.e., objects) • can

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

Page 9: Java - 1cm Summer 2013 · 2013. 7. 18. · Static Instance Methods • non-static methods are instance methods • they are bound to instances of the class (i.e., objects) • can

Assignments

· assignment 4· how many bugs did you �nd?

· what happens when b is false and y ≤ x ≤ 2y

5 / 20

Page 10: Java - 1cm Summer 2013 · 2013. 7. 18. · Static Instance Methods • non-static methods are instance methods • they are bound to instances of the class (i.e., objects) • can

Assignments

· assignment 4· how many bugs did you �nd?

· what happens when b is false and y ≤ x ≤ 2y

5 / 20

Page 11: Java - 1cm Summer 2013 · 2013. 7. 18. · Static Instance Methods • non-static methods are instance methods • they are bound to instances of the class (i.e., objects) • can

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

Page 12: Java - 1cm Summer 2013 · 2013. 7. 18. · Static Instance Methods • non-static methods are instance methods • they are bound to instances of the class (i.e., objects) • can

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

Page 13: Java - 1cm Summer 2013 · 2013. 7. 18. · Static Instance Methods • non-static methods are instance methods • they are bound to instances of the class (i.e., objects) • can

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

Page 14: Java - 1cm Summer 2013 · 2013. 7. 18. · Static Instance Methods • non-static methods are instance methods • they are bound to instances of the class (i.e., objects) • can

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

Page 15: Java - 1cm Summer 2013 · 2013. 7. 18. · Static Instance Methods • non-static methods are instance methods • they are bound to instances of the class (i.e., objects) • can

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

Page 16: Java - 1cm Summer 2013 · 2013. 7. 18. · Static Instance Methods • non-static methods are instance methods • they are bound to instances of the class (i.e., objects) • can

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

Page 17: Java - 1cm Summer 2013 · 2013. 7. 18. · Static Instance Methods • non-static methods are instance methods • they are bound to instances of the class (i.e., objects) • can

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

Page 18: Java - 1cm Summer 2013 · 2013. 7. 18. · Static Instance Methods • non-static methods are instance methods • they are bound to instances of the class (i.e., objects) • can

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

Page 19: Java - 1cm Summer 2013 · 2013. 7. 18. · Static Instance Methods • non-static methods are instance methods • they are bound to instances of the class (i.e., objects) • can

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

Page 20: Java - 1cm Summer 2013 · 2013. 7. 18. · Static Instance Methods • non-static methods are instance methods • they are bound to instances of the class (i.e., objects) • can

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

Page 21: Java - 1cm Summer 2013 · 2013. 7. 18. · Static Instance Methods • non-static methods are instance methods • they are bound to instances of the class (i.e., objects) • can

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

Page 22: Java - 1cm Summer 2013 · 2013. 7. 18. · Static Instance Methods • non-static methods are instance methods • they are bound to instances of the class (i.e., objects) • can

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

Page 23: Java - 1cm Summer 2013 · 2013. 7. 18. · Static Instance Methods • non-static methods are instance methods • they are bound to instances of the class (i.e., objects) • can

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

Page 24: Java - 1cm Summer 2013 · 2013. 7. 18. · Static Instance Methods • non-static methods are instance methods • they are bound to instances of the class (i.e., objects) • can

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

Page 25: Java - 1cm Summer 2013 · 2013. 7. 18. · Static Instance Methods • non-static methods are instance methods • they are bound to instances of the class (i.e., objects) • can

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

Page 26: Java - 1cm Summer 2013 · 2013. 7. 18. · Static Instance Methods • non-static methods are instance methods • they are bound to instances of the class (i.e., objects) • can

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

Page 27: Java - 1cm Summer 2013 · 2013. 7. 18. · Static Instance Methods • non-static methods are instance methods • they are bound to instances of the class (i.e., objects) • can

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

Page 28: Java - 1cm Summer 2013 · 2013. 7. 18. · Static Instance Methods • non-static methods are instance methods • they are bound to instances of the class (i.e., objects) • can

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

Page 29: Java - 1cm Summer 2013 · 2013. 7. 18. · Static Instance Methods • non-static methods are instance methods • they are bound to instances of the class (i.e., objects) • can

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

Page 30: Java - 1cm Summer 2013 · 2013. 7. 18. · Static Instance Methods • non-static methods are instance methods • they are bound to instances of the class (i.e., objects) • can

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

Page 31: Java - 1cm Summer 2013 · 2013. 7. 18. · Static Instance Methods • non-static methods are instance methods • they are bound to instances of the class (i.e., objects) • can

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

Page 32: Java - 1cm Summer 2013 · 2013. 7. 18. · Static Instance Methods • non-static methods are instance methods • they are bound to instances of the class (i.e., objects) • can

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

Page 33: Java - 1cm Summer 2013 · 2013. 7. 18. · Static Instance Methods • non-static methods are instance methods • they are bound to instances of the class (i.e., objects) • can

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

Page 34: Java - 1cm Summer 2013 · 2013. 7. 18. · Static Instance Methods • non-static methods are instance methods • they are bound to instances of the class (i.e., objects) • can

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

Page 35: Java - 1cm Summer 2013 · 2013. 7. 18. · Static Instance Methods • non-static methods are instance methods • they are bound to instances of the class (i.e., objects) • can

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

Page 36: Java - 1cm Summer 2013 · 2013. 7. 18. · Static Instance Methods • non-static methods are instance methods • they are bound to instances of the class (i.e., objects) • can

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

Page 37: Java - 1cm Summer 2013 · 2013. 7. 18. · Static Instance Methods • non-static methods are instance methods • they are bound to instances of the class (i.e., objects) • can

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

Page 38: Java - 1cm Summer 2013 · 2013. 7. 18. · Static Instance Methods • non-static methods are instance methods • they are bound to instances of the class (i.e., objects) • can

let's take a break...for 5 minutes

11 / 20

Page 39: Java - 1cm Summer 2013 · 2013. 7. 18. · Static Instance Methods • non-static methods are instance methods • they are bound to instances of the class (i.e., objects) • can

Inheritance and Constructors

public class A{

...

}

public class B extends A{

...

}

12 / 20

Page 40: Java - 1cm Summer 2013 · 2013. 7. 18. · Static Instance Methods • non-static methods are instance methods • they are bound to instances of the class (i.e., objects) • can

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

Page 41: Java - 1cm Summer 2013 · 2013. 7. 18. · Static Instance Methods • non-static methods are instance methods • they are bound to instances of the class (i.e., objects) • can

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

Page 42: Java - 1cm Summer 2013 · 2013. 7. 18. · Static Instance Methods • non-static methods are instance methods • they are bound to instances of the class (i.e., objects) • can

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

Page 43: Java - 1cm Summer 2013 · 2013. 7. 18. · Static Instance Methods • non-static methods are instance methods • they are bound to instances of the class (i.e., objects) • can

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

Page 44: Java - 1cm Summer 2013 · 2013. 7. 18. · Static Instance Methods • non-static methods are instance methods • they are bound to instances of the class (i.e., objects) • can

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

Page 45: Java - 1cm Summer 2013 · 2013. 7. 18. · Static Instance Methods • non-static methods are instance methods • they are bound to instances of the class (i.e., objects) • can

�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

Page 46: Java - 1cm Summer 2013 · 2013. 7. 18. · Static Instance Methods • non-static methods are instance methods • they are bound to instances of the class (i.e., objects) • can

�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

Page 47: Java - 1cm Summer 2013 · 2013. 7. 18. · Static Instance Methods • non-static methods are instance methods • they are bound to instances of the class (i.e., objects) • can

�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

Page 48: Java - 1cm Summer 2013 · 2013. 7. 18. · Static Instance Methods • non-static methods are instance methods • they are bound to instances of the class (i.e., objects) • can

�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

Page 49: Java - 1cm Summer 2013 · 2013. 7. 18. · Static Instance Methods • non-static methods are instance methods • they are bound to instances of the class (i.e., objects) • can

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

Page 50: Java - 1cm Summer 2013 · 2013. 7. 18. · Static Instance Methods • non-static methods are instance methods • they are bound to instances of the class (i.e., objects) • can

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

Page 51: Java - 1cm Summer 2013 · 2013. 7. 18. · Static Instance Methods • non-static methods are instance methods • they are bound to instances of the class (i.e., objects) • can

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

Page 52: Java - 1cm Summer 2013 · 2013. 7. 18. · Static Instance Methods • non-static methods are instance methods • they are bound to instances of the class (i.e., objects) • can

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

Page 53: Java - 1cm Summer 2013 · 2013. 7. 18. · Static Instance Methods • non-static methods are instance methods • they are bound to instances of the class (i.e., objects) • can

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

Page 54: Java - 1cm Summer 2013 · 2013. 7. 18. · Static Instance Methods • non-static methods are instance methods • they are bound to instances of the class (i.e., objects) • can

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

Page 55: Java - 1cm Summer 2013 · 2013. 7. 18. · Static Instance Methods • non-static methods are instance methods • they are bound to instances of the class (i.e., objects) • can

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

Page 56: Java - 1cm Summer 2013 · 2013. 7. 18. · Static Instance Methods • non-static methods are instance methods • they are bound to instances of the class (i.e., objects) • can

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

Page 57: Java - 1cm Summer 2013 · 2013. 7. 18. · Static Instance Methods • non-static methods are instance methods • they are bound to instances of the class (i.e., objects) • can

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

Page 58: Java - 1cm Summer 2013 · 2013. 7. 18. · Static Instance Methods • non-static methods are instance methods • they are bound to instances of the class (i.e., objects) • can

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

Page 59: Java - 1cm Summer 2013 · 2013. 7. 18. · Static Instance Methods • non-static methods are instance methods • they are bound to instances of the class (i.e., objects) • can

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

Page 60: Java - 1cm Summer 2013 · 2013. 7. 18. · Static Instance Methods • non-static methods are instance methods • they are bound to instances of the class (i.e., objects) • can

abstract

insert pic of rock, paper and scissors

19 / 20

Page 61: Java - 1cm Summer 2013 · 2013. 7. 18. · Static Instance Methods • non-static methods are instance methods • they are bound to instances of the class (i.e., objects) • can

let's take a break...for 10 minutes

20 / 20