Methods Constructors Instance methods Class methods 30 - - PowerPoint PPT Presentation

methods
SMART_READER_LITE
LIVE PREVIEW

Methods Constructors Instance methods Class methods 30 - - PowerPoint PPT Presentation

Methods Constructors Instance methods Class methods 30 Constructors have the same name as the class do not return anything they begin with a call to the constructor from the parent class (super(...)) public class MyClass{


slide-1
SLIDE 1

Methods

  • Constructors
  • Instance methods
  • Class methods

30

slide-2
SLIDE 2

Constructors

  • have the same name as the class
  • do not return anything
  • they begin with a call to the constructor

from the parent class (super(...)) public class MyClass{ public MyClass(){} ... MyClass mc = MyClass(); }

31

slide-3
SLIDE 3

Instance Method

  • are called on a reference to an instance
  • can return values
  • C-style declaration and value return

public class MyClass{ public boolean test(){ return true;} } ... MyClass mc=new MyClass(); mc.test();

32

slide-4
SLIDE 4

Class Method

  • Are called on the class name, instance names
  • declared static

public class MyClass{ public static boolean test(){ return true;} } ... MyClass.test();

33

slide-5
SLIDE 5

The main method

  • static, returns nothing, public, String[] as

parameters public class MyClass{ public static void main(String[] args){...} }

34

slide-6
SLIDE 6

Inheritance

  • Methods and fields are inherited
  • They can be used in a child class without any

redeclaration

  • They can be overriden
  • Overloading is ok

35

slide-7
SLIDE 7

Exercises

  • final & volatile?
  • static and multithreaded?
  • local variable usable from another class?
  • declare a String that would be visible from
  • utside the package, shared by all instances

and can be accessed concurrently.

36

slide-8
SLIDE 8

Why doesn’t it compile?

public class HelloWorld { public String message="Hello World!"; public HelloWorld(String s){ super(); message=s; } } public class HelloWorld2 extends HelloWorld{ public HelloWorld2(String s){ message=s; } }

37

slide-9
SLIDE 9

Why doesn’t it compile?

public class HelloWorld { public String message="Hello World!"; private HelloWorld(String s){ super(); message=s; } } public class HelloWorld2 extends HelloWorld{ public HelloWorld2(String s){ super(s); } }

38

slide-10
SLIDE 10

What happens here?

public class Parent { public Parent wave(Parent p){ System.out.println("in the parent"); return null; } } public class Child extends Parent{ public Child wave(Object o){ System.out.println("in the child"); return null; } public static void main(String []args){ Parent p=new Child(); p.wave(p); } }

39

slide-11
SLIDE 11

...and here?

public class Parent { public Parent wave(Parent p){ System.out.println("in the parent"); return null; } } public class Child extends Parent{ public Child wave(Parent p){ System.out.println("in the child"); return null; } public static void main(String []args){ Parent p=new Child(); p.wave(p); } }

40

slide-12
SLIDE 12

What is invalid?

public class HelloWorld{ String s="Hello World"; public static void main(String []args){ System.out.println(s); } }

41

slide-13
SLIDE 13

Expressions

  • Method Invocation
  • instanceof
  • Casts
  • blocks
  • control-flow structures

42

slide-14
SLIDE 14

Invoking Methods

  • Already saw that:

MyClass mc = new MyClass(); mc.test(); MyClass.testStatic(); this.test(); test();

43

slide-15
SLIDE 15

instanceof

  • instruction from the language
  • Returns a boolean

mc instanceof MyClass

44

slide-16
SLIDE 16

Casts

  • Used to assign an instance of a super class

to a variable of a subclass

  • May fail at runtime!

String S = (String)myObject;

45

slide-17
SLIDE 17

Blocks

  • As in many languages blocks can be declared

in place of any instruction and define a name space: { ... }

46

slide-18
SLIDE 18

if

  • if (test) block/instruction;
  • if (test) block/instruction; else block/instruction;

if (ms instanceof MyClass) if (!(ms instanceof String)) System.out.println(”MyClass not String”); else System.out.println(”not MyClass”);

47

slide-19
SLIDE 19

switch

switch(expression){ case constant: {} ... default: {} }

48

slide-20
SLIDE 20

while

while (test){ ... }

49

slide-21
SLIDE 21

do...while

do{ ... } while (test)

50

slide-22
SLIDE 22

for

for (init;test;loopInst){ ... }

51

slide-23
SLIDE 23

Enhanced for

  • if the expression is Iterable (arrays are...)

for(Type varName: expression){ ... }

52

slide-24
SLIDE 24

break/continue

  • break breaks the innermost loop
  • continue goes back to its start
  • with labels: act on the labeled structure

53