MELJUN CORTES JEDI Slides-Intro2-Chapter01-Review of Basic Concepts in Java

Embed Size (px)

Citation preview

  • 7/31/2019 MELJUN CORTES JEDI Slides-Intro2-Chapter01-Review of Basic Concepts in Java

    1/71

    Introduction to Programming 2 1

    1 Review of Basic Conceptsin Java

  • 7/31/2019 MELJUN CORTES JEDI Slides-Intro2-Chapter01-Review of Basic Concepts in Java

    2/71

    Introduction to Programming 2 2

    Topics

    Object-Oriented Concepts

    Java Program Structure

  • 7/31/2019 MELJUN CORTES JEDI Slides-Intro2-Chapter01-Review of Basic Concepts in Java

    3/71

    Introduction to Programming 2 3

    Object-Oriented Concepts Object-Oriented Design

    Focuses on object and classes based on real world scenarios

    Emphasizes state, behavior and interaction of objects

    Advantages:

    Faster development

    Increased quality

    Easier maintenance

    Enhanced modifiability

    Increase software reuse

    Class

    Allows you to define new data types

    Blueprint

  • 7/31/2019 MELJUN CORTES JEDI Slides-Intro2-Chapter01-Review of Basic Concepts in Java

    4/71

    Introduction to Programming 2 4

    Object-Oriented Concepts Object

    An entity that has a state, behavior and identity with a well-definedrole in problem space

    An actual instance of a class

    Created every time you instantiate a class using the newkeyword.

    Student registration system example

    Attribute

    Data element of an object

    Stores information about the object

    A.K.A. Data member, instance variable, property, data field

    Student registration system example

  • 7/31/2019 MELJUN CORTES JEDI Slides-Intro2-Chapter01-Review of Basic Concepts in Java

    5/71

    Introduction to Programming 2 5

    Object-Oriented Concepts

    Method

    Describes the behavior of an object

    Also called a function or a procedure

    Student registration system example

    Constructor

    Method-like

    For creating and initializing a new object Not members of an object

  • 7/31/2019 MELJUN CORTES JEDI Slides-Intro2-Chapter01-Review of Basic Concepts in Java

    6/71

    Introduction to Programming 2 6

    Object-Oriented Concepts

    Package

    Grouping of classes and/or subpackages

    Analogous to a directory

    Encapsulation

    Principle of hiding design or implementation information not relevantto the current object

    Abstraction

    Ignoring aspects of a subject not relevant to the current purpose tofocus on those that are

  • 7/31/2019 MELJUN CORTES JEDI Slides-Intro2-Chapter01-Review of Basic Concepts in Java

    7/71

    Introduction to Programming 2 7

    Object-Oriented Concepts

    Inheritance

    Relationship between classes wherein one class is the superclass orthe parent class of another

    Refers to the properties and behaviors received from an ancestor

    Also know as a "is-a" relationship

    SuperHero

    FlyingSuperHero UnderwaterSuperHero

  • 7/31/2019 MELJUN CORTES JEDI Slides-Intro2-Chapter01-Review of Basic Concepts in Java

    8/71

    Introduction to Programming 2 8

    Object-Oriented Concepts

    Polymorphism

    "poly" means many while "morph" means form

    Ability of an object to assume may different forms

    Interface

    Contract in the form of a collection of method and constantdeclarations

    Implementing class promises to follow the contract.

  • 7/31/2019 MELJUN CORTES JEDI Slides-Intro2-Chapter01-Review of Basic Concepts in Java

    9/71

    Introduction to Programming 2 9

    Java Program Structure:

    Declaring Classes Syntax ::=

    class {

    **

    *

    }

    where

    Modifier

    Refers to an access modifier or other types of modifiers

    Name

    Is an identifier for the name of the class

  • 7/31/2019 MELJUN CORTES JEDI Slides-Intro2-Chapter01-Review of Basic Concepts in Java

    10/71

    Introduction to Programming 2 10

    Java Program Structure:

    Declaring Classes1 class SuperHero {2 String superPowers[];

    3 void setSuperPowers(String superPowers[]) {

    4 this.superPowers = superPowers;5 }

    6 void printSuperPowers() {

    7 for (int i = 0; i < superPowers.length; i++) {

    8 System.out.println(superPowers[i]);

    9 }

    10 }

    11 }

  • 7/31/2019 MELJUN CORTES JEDI Slides-Intro2-Chapter01-Review of Basic Concepts in Java

    11/71

    Introduction to Programming 2 11

    Java Program Structure:

    Declaring Attributes Syntax:

    ::=

    [= ];

    ::=

    byte | short | int | long | char | float | double| boolean |

  • 7/31/2019 MELJUN CORTES JEDI Slides-Intro2-Chapter01-Review of Basic Concepts in Java

    12/71

    Introduction to Programming 2 12

    Java Program Structure:

    Declaring Attributes1 public class AttributeDemo {

    2 private String studNum;

    3 public boolean graduating = false;

    4 protected float unitsTaken = 0.0f;5 String college;

    6 }

  • 7/31/2019 MELJUN CORTES JEDI Slides-Intro2-Chapter01-Review of Basic Concepts in Java

    13/71

    Introduction to Programming 2 13

    Java Program Structure:

    Declaring Methods Syntax: ::=

    (*) {

    *}

    ::=

    [,]

  • 7/31/2019 MELJUN CORTES JEDI Slides-Intro2-Chapter01-Review of Basic Concepts in Java

    14/71

    Introduction to Programming 2 14

    Java Program Structure:

    Declaring Methods1 class MethodDemo {2 int data;

    3 int getData() {

    4 return data;

    5 }

    6 void setData(int data) {

    7 this.data = data;

    8 }

    9 void setMaxData(int data1, int data2) {

    10 data = (data1>data2)? data1 : data2;

    11 }

    12 }

  • 7/31/2019 MELJUN CORTES JEDI Slides-Intro2-Chapter01-Review of Basic Concepts in Java

    15/71

    Introduction to Programming 2 15

    Java Program Structure:Declaring a Constructor

    Syntax:

    ::=

    (*) {

    *

    }

    where

    Modifier

    Can be any access modifier but not other types of modifiers.

    Default constructor

    No arguments

    Empty body

  • 7/31/2019 MELJUN CORTES JEDI Slides-Intro2-Chapter01-Review of Basic Concepts in Java

    16/71

    Introduction to Programming 2 16

    Java Program Structure:

    Declaring a Constructor1 class ConstructorDemo {2 private int data;

    3 public ConstructorDemo() {

    4 data = 100;

    5 }

    6 ConstructorDemo(int data) {

    7 this.data = data;

    8 }

    9 }

  • 7/31/2019 MELJUN CORTES JEDI Slides-Intro2-Chapter01-Review of Basic Concepts in Java

    17/71

    Introduction to Programming 2 17

    Java Program Structure:Instantiating a Class

    Syntax:

    new ()

    Example:

    1 class ConstructObj {

    2 int data;

    3 ConstructObj() {

    4 /* initialize data */

    5 }6 public static void main(String args[]) {

    7 ConstructObj obj = new ConstructObj();

    8 }

    9 }

  • 7/31/2019 MELJUN CORTES JEDI Slides-Intro2-Chapter01-Review of Basic Concepts in Java

    18/71

    Introduction to Programming 2 18

    Java Program Structure:

    Accessing Object Members Dot notation:.

    An example:String myString = new String(My String);

    //Access length method

    System.out.println(Length: + myString.length());

    Output: Length: 9

  • 7/31/2019 MELJUN CORTES JEDI Slides-Intro2-Chapter01-Review of Basic Concepts in Java

    19/71

    Introduction to Programming 2 19

    Java Program Structure:

    Accessing Object Members Another exampleint intArr = {1, 2, 3, 4, 5};

    //Access length attribute

    System.out.println(Length: + intArr.length);

    Output: Length: 5

  • 7/31/2019 MELJUN CORTES JEDI Slides-Intro2-Chapter01-Review of Basic Concepts in Java

    20/71

    Introduction to Programming 2 20

    Java Program Structure:Accessing Object Members

    1 class ConstructObj {

    2 int data;

    3 ConstructObj() {

    4 /* initialize data */

    5 }6 void setData(int data) {

    7 this.data = data;

    8 }

    9 public static void main(String args[]) {

    10 ConstructObj obj = new ConstructObj();

    11 obj.setData(10); //access setData()

    12 System.out.println(obj.data); //access data

    13 }

    14 }

  • 7/31/2019 MELJUN CORTES JEDI Slides-Intro2-Chapter01-Review of Basic Concepts in Java

    21/71

    Introduction to Programming 2 21

    Java Program Structure:Accessing Object Members

    Output:

    10

  • 7/31/2019 MELJUN CORTES JEDI Slides-Intro2-Chapter01-Review of Basic Concepts in Java

    22/71

    Introduction to Programming 2 22

    Java Program Structure:Packages

    Syntax for indicating that the code belongs to a package:

    ::=

    package ;

    Syntax for importing other packages:

    ::=

    import ;

    Source code format:

    []

    *

    +

  • 7/31/2019 MELJUN CORTES JEDI Slides-Intro2-Chapter01-Review of Basic Concepts in Java

    23/71

    Introduction to Programming 2 23

    Java Program Structure:Packages

    1 package registration.reports;

    2 import registration.processing.*;

    3 import java.util.List;

    4 import java.lang.*; //imported by default

    5 class MyClass {

    6 /* details of MyClass */

    7 }

  • 7/31/2019 MELJUN CORTES JEDI Slides-Intro2-Chapter01-Review of Basic Concepts in Java

    24/71

    Introduction to Programming 2 24

    Java Program Structure:The Access Modifiers

    Java Program Structure:

  • 7/31/2019 MELJUN CORTES JEDI Slides-Intro2-Chapter01-Review of Basic Concepts in Java

    25/71

    Introduction to Programming 2 25

    Java Program Structure:Encapsulation

    Hide members by making themprivate Example

    1 class Encapsulation {

    2 private int secret;

    3

    public boolean setSecret(int secret) {4 if (secret < 1 || secret > 100)

    5 return false;

    6 this.secret = secret;

    7 return true;

    8 }

    9 public getSecret() {

    10 return secret;

    11 }

    12 }

    Java Program Structure:

  • 7/31/2019 MELJUN CORTES JEDI Slides-Intro2-Chapter01-Review of Basic Concepts in Java

    26/71

    Introduction to Programming 2 26

    Java Program Structure:Encapsulation

    What if I do not want to allow other classes to modify thevalue ofsecret?

    Answer: Make the setSecret() method private.

  • 7/31/2019 MELJUN CORTES JEDI Slides-Intro2-Chapter01-Review of Basic Concepts in Java

    27/71

    Introduction to Programming 2 27

    Java Program Structure:Inheritance

    Creating a child class or a subclass:

    Use extends in declaring the class

    Syntax:

    class extends

    A class can only extend one parent class

  • 7/31/2019 MELJUN CORTES JEDI Slides-Intro2-Chapter01-Review of Basic Concepts in Java

    28/71

    Introduction to Programming 2 28

    Java Program Structure:Inheritance

    1 import java.awt.*;

    2

    3 class Point {

    4 int x;

    5 int y;6 }

    7

    8 class ColoredPoint extends Point {

    9 Color color;10 }

    J P S

  • 7/31/2019 MELJUN CORTES JEDI Slides-Intro2-Chapter01-Review of Basic Concepts in Java

    29/71

    Introduction to Programming 2 29

    Java Program Structure:Overriding Methods

    Subclass defines a method whose signature is identical to amethod in the superclass

    Signature of a method

    Information found in the method header definition

    Return type

    Method name

    Parameter list of the method

    Different from method overloading!

    J P St t

  • 7/31/2019 MELJUN CORTES JEDI Slides-Intro2-Chapter01-Review of Basic Concepts in Java

    30/71

    Introduction to Programming 2 30

    Java Program Structure:Overriding Methods

    1 class Superclass {

    2 void display(int n) {

    3 System.out.println("super: " + n);

    4 }

    5 }6

    7 class Subclass extends Superclass {

    8 void display(int k) {

    9 System.out.println("sub: " + k);

    10 }

    11 }

    12

    13 // continued...

    J P St t

  • 7/31/2019 MELJUN CORTES JEDI Slides-Intro2-Chapter01-Review of Basic Concepts in Java

    31/71

    Introduction to Programming 2 31

    Java Program Structure:Overriding Methods

    14 class OverrideDemo {

    15 public static void main(String args[]) {

    16 Subclass SubObj = new Subclass();

    17 Superclass SuperObj = SubObj;

    18 SubObj.display(3);19 ((Superclass)SubObj).display(4);

    20 }

    21 }

    J P St t

  • 7/31/2019 MELJUN CORTES JEDI Slides-Intro2-Chapter01-Review of Basic Concepts in Java

    32/71

    Introduction to Programming 2 32

    Java Program Structure:Overriding Methods

    Output:

    sub: 3

    sub: 4

    J P St t

  • 7/31/2019 MELJUN CORTES JEDI Slides-Intro2-Chapter01-Review of Basic Concepts in Java

    33/71

    Introduction to Programming 233

    Java Program Structure:Overriding Methods

    Version of method called

    Based on actual data type of the object that invoked the method

    Access modifier for the methods need not be the same

    Access modifier of the overriding method

    Same access modifier as that of the overridden method

    Less restrictive access modifier

    J P St t

  • 7/31/2019 MELJUN CORTES JEDI Slides-Intro2-Chapter01-Review of Basic Concepts in Java

    34/71

    Introduction to Programming 234

    Java Program Structure:Overriding Methods

    1 class Superclass {

    2 void overriddenMethod() {

    3 }

    4 }

    5 class Subclass1 extends Superclass {6 public void overriddenMethod() {

    7 }

    8 }

    9 class Subclass2 extends Superclass {

    10 void overriddenMethod() {

    11 }

    12 }

    13 //continued...

    Ja a Program Str ct re

  • 7/31/2019 MELJUN CORTES JEDI Slides-Intro2-Chapter01-Review of Basic Concepts in Java

    35/71

    Introduction to Programming 235

    Java Program Structure:Overriding Methods

    14 /* class Superclass {

    15 void overriddenMethod() {

    16 }

    17 } */

    18 class Subclass3 extends Superclass {19 protected void overriddenMethod() {

    20 }

    21 }

    22 class Subclass4 extends Superclass {

    23 private void overriddenMethod() {

    24 }

    25 }

    Java Program Structure:

  • 7/31/2019 MELJUN CORTES JEDI Slides-Intro2-Chapter01-Review of Basic Concepts in Java

    36/71

    Introduction to Programming 236

    Java Program Structure:Overriding Methods

    Subclass4 causes a compile time error

    private access modifier is more restrictive compared topackage/default modifier

    Java Program Structure:

  • 7/31/2019 MELJUN CORTES JEDI Slides-Intro2-Chapter01-Review of Basic Concepts in Java

    37/71

    Introduction to Programming 237

    Java Program Structure:Abstract Classes and Methods Syntax:

    abstract (*);

    Class containing an abstractmethod should be declaredabstract

    abstract class {

    /* constructors, fields and methods */

    }

    Java Program Structure:

  • 7/31/2019 MELJUN CORTES JEDI Slides-Intro2-Chapter01-Review of Basic Concepts in Java

    38/71

    Introduction to Programming 2 38

    Java Program Structure:Abstract Classes and Methods

    abstractkeyword is not for:

    Constructor

    static method

    abstractclasses cannot be instantiated

    Classes that extends an abstract class:

    Should implement all abstractmethods

    Otherwise, the subclass itself should be declared abstract

    Java Program Structure:

  • 7/31/2019 MELJUN CORTES JEDI Slides-Intro2-Chapter01-Review of Basic Concepts in Java

    39/71

    Introduction to Programming 2 39

    1 abstract class SuperHero {

    2 String superPowers[];

    3 void setSuperPowers(String superPowers[]) {

    4 this.superPowers = superPowers;

    5 }6 void printSuperPowers() {

    7 for (int i = 0; i < superPowers.length; i++) {

    8 System.out.println(superPowers[i]);

    9 }

    10 }

    11 abstract void displayPower();

    12 }

    13 //continued...

    Java Program Structure:Abstract Classes and Methods

    Java Program Structure:

  • 7/31/2019 MELJUN CORTES JEDI Slides-Intro2-Chapter01-Review of Basic Concepts in Java

    40/71

    Introduction to Programming 2 40

    1 class FlyingSuperHero extends SuperHero {

    2 void displayPower() {

    3 System.out.println("Fly...");

    4 }

    5

    }6

    7 class Spiderman extends SuperHero {

    8 void displayPower() {

    9 System.out.println("Communicate with sea" +

    10 " creatures...");

    11 System.out.println("Fast swimming ability...");

    12 }

    13 }

    Java Program Structure:Abstract Classes and Methods

    Java Program Structure:

  • 7/31/2019 MELJUN CORTES JEDI Slides-Intro2-Chapter01-Review of Basic Concepts in Java

    41/71

    Introduction to Programming 2 41

    Java Program Structure:Interface

    Syntax:

    ::=

    interface {

    *

    [

    (*);]*

    }

    Members arepublicwhen the interface is declaredpublic

    Java Program Structure:

  • 7/31/2019 MELJUN CORTES JEDI Slides-Intro2-Chapter01-Review of Basic Concepts in Java

    42/71

    Introduction to Programming 2 42

    Java Program Structure:Interface

    Interface attributes: Implicitly staticand final

    Must be initialized

    Modifiers: Access modifiers:public, package

    Must be initialized

    Implementing an interface: Use implements keyword

    Should implement all the interface's methods

    A class can implement several interfaces

    Java Program Structure:

  • 7/31/2019 MELJUN CORTES JEDI Slides-Intro2-Chapter01-Review of Basic Concepts in Java

    43/71

    Introduction to Programming 2 43

    Java Program Structure:Interface

    1 interface MyInterface {

    2 void iMethod();

    3 }

    4

    5 class MyClass1 implements MyInterface {6 public void iMethod() {

    7 System.out.println("Interface method.");

    8 }

    9 void myMethod() {

    10 System.out.println("Another method.");

    11 }

    12 }

    13 //continued...

    Java Program Structure:

  • 7/31/2019 MELJUN CORTES JEDI Slides-Intro2-Chapter01-Review of Basic Concepts in Java

    44/71

    Introduction to Programming 2 44

    Java Program Structure:Interface

    14 class MyClass2 implements MyInterface {

    15 public void iMethod() {

    16 System.out.println("Another implementation.");

    17 }

    18 }

    19 class InterfaceDemo {

    20 public static void main(String args[]) {

    21 MyClass1 mc1 = new MyClass1();

    22 MyClass2 mc2 = new MyClass2();

    23 mc1.iMethod();24 mc1.myMethod();

    25 mc2.iMethod();

    26 }

    27 }

    Java Program Structure:

  • 7/31/2019 MELJUN CORTES JEDI Slides-Intro2-Chapter01-Review of Basic Concepts in Java

    45/71

    Introduction to Programming 2 45

    Java Program Structure:Interface

    Output:

    Interface method.

    Another method.Another implementation.

    Java Program Structure:

  • 7/31/2019 MELJUN CORTES JEDI Slides-Intro2-Chapter01-Review of Basic Concepts in Java

    46/71

    Introduction to Programming 2 46

    Java Program Structure:The this Keyword

    Why this?

    1. Disambiguate local attribute from a local variable

    2. Refer to the object that invoked the non-static method

    3. Refer to other constructors

    Java Program Structure:

  • 7/31/2019 MELJUN CORTES JEDI Slides-Intro2-Chapter01-Review of Basic Concepts in Java

    47/71

    Introduction to Programming 2 47

    Java Program Structure:The this Keyword

    Disambiguate local attribute from a local variable

    1 class ThisDemo1 {

    2 int data;

    3 void method(int data) {

    4 this.data = data;

    5 /*

    6 this.data refers to the attribute

    7 while data refers to the local variable

    8 */9 }

    10 }

    Java Program Structure:

  • 7/31/2019 MELJUN CORTES JEDI Slides-Intro2-Chapter01-Review of Basic Concepts in Java

    48/71

    Introduction to Programming 2 48

    Java Program Structure:The this Keyword

    Refer to the object that invoked the non-static method

    1 class ThisDemo2 {

    2 int data;

    3 void method() {

    4 System.out.println(data); //this.data

    5 }

    6 void method2() {

    7 method(); //this.method();

    8 }9 }

    Java Program Structure:

  • 7/31/2019 MELJUN CORTES JEDI Slides-Intro2-Chapter01-Review of Basic Concepts in Java

    49/71

    Introduction to Programming 2 49

    Java Program Structure:The this Keyword

    Method Overloading

    Different methods within a class sharing the same name

    Parameter lists should differ

    Number of parameters

    Type of parameters Constructors can also be overloaded

    An example:

    class MyClass {

    void myMeth() {}

    void myMeth(int i) {}

    void myMeth(int i, int j) {}

    }

    Java Program Structure:

  • 7/31/2019 MELJUN CORTES JEDI Slides-Intro2-Chapter01-Review of Basic Concepts in Java

    50/71

    Introduction to Programming 2 50

    Java Program Structure:The this Keyword

    Refer to other constructors1 class ThisDemo3 {

    2 int data;

    3 ThisDemo3() {

    4 this(100);

    5 }

    6 ThisDemo3(int data) {

    7 this.data = data;

    8 }9 }

    Call to this() should be the first statement in constructor

    Java Program Structure:

  • 7/31/2019 MELJUN CORTES JEDI Slides-Intro2-Chapter01-Review of Basic Concepts in Java

    51/71

    Introduction to Programming 2 51

    The superKeyword

    Related to inheritance Invoke superclass constructors

    Can be used like the this keyword to refer to members of thesuperclass

    Calling superclass constructors

    1 class Person {

    2 String firstName;

    3 String lastName;

    4 Person(String fname, String lname) {

    5 firstName = fname;

    6 lastName = lname;

    7 }

    8 }

    Java Program Structure:

  • 7/31/2019 MELJUN CORTES JEDI Slides-Intro2-Chapter01-Review of Basic Concepts in Java

    52/71

    Introduction to Programming 2 52

    gThe superKeyword

    9 //continuation...

    10 class Student extends Person {

    11 String studNum;

    12 Student(String fname, String lname, String sNum) {

    13 super(fname, lname);14 studNum = sNum;

    15 }

    16 }

    super()

    Refers to the immediate superclass

    Should be first statement in the subclass's constructor

    Java Program Structure:

  • 7/31/2019 MELJUN CORTES JEDI Slides-Intro2-Chapter01-Review of Basic Concepts in Java

    53/71

    Introduction to Programming 2 53

    gThe superKeyword

    Referring to superclass members1 class Superclass{

    2 int a;

    3 void display_a(){

    4 System.out.println("a = " + a);5 }

    6 }

    7

    8

    //continued...

    Java Program Structure:

  • 7/31/2019 MELJUN CORTES JEDI Slides-Intro2-Chapter01-Review of Basic Concepts in Java

    54/71

    Introduction to Programming 2 54

    gThe superKeyword

    9 class Subclass extends Superclass {

    10 int a;

    11 void display_a(){

    12 System.out.println("a = " + a);

    13 }14 void set_super_a(int n){

    15 super.a = n;

    16 }

    17 void display_super_a(){

    18 super.display_a();

    19 }

    20 }

    Java Program Structure:

  • 7/31/2019 MELJUN CORTES JEDI Slides-Intro2-Chapter01-Review of Basic Concepts in Java

    55/71

    Introduction to Programming 2 55

    gThe superKeyword

    21 class SuperDemo {

    22 public static void main(String args[]){

    23 Superclass SuperObj = new Superclass();

    24 Subclass SubObj = new Subclass();

    25

    SuperObj.a = 1;26 SubObj.a = 2;

    27 SubObj.set_super_a(3);

    28 SuperObj.display_a();

    29 SubObj.display_a();

    30 SubObj.display_super_a();

    31 System.out.println(SubObj.a);

    32 }

    33 }

    Java Program Structure:

  • 7/31/2019 MELJUN CORTES JEDI Slides-Intro2-Chapter01-Review of Basic Concepts in Java

    56/71

    Introduction to Programming 2 56

    gThe superKeyword

    Output:

    a = 1

    a = 2

    a = 32

    Java Program Structure:

  • 7/31/2019 MELJUN CORTES JEDI Slides-Intro2-Chapter01-Review of Basic Concepts in Java

    57/71

    Introduction to Programming 2 57

    gThe staticKeyword

    Applied to members of a class: Attributes

    Methods

    Inner classes

    Allows accessing of static or class members withoutinstantiation

    Class variables Behave like a global variable

    Can be accessed by all instances of the class

    Java Program Structure:

  • 7/31/2019 MELJUN CORTES JEDI Slides-Intro2-Chapter01-Review of Basic Concepts in Java

    58/71

    Introduction to Programming 2 58

    gThe staticKeyword

    Class methods May be invoked without creating an object of its class

    Can only access static members of the class

    Cannot refer to this orsuper

    staticblocks

    Executed only once, when the class is loaded

    For initializing class variables

    Java Program Structure:

  • 7/31/2019 MELJUN CORTES JEDI Slides-Intro2-Chapter01-Review of Basic Concepts in Java

    59/71

    Introduction to Programming 2 59

    The staticKeyword1 class Demo {2 static int a = 0;

    3 static void staticMethod(int i) {

    4 System.out.println(i);

    5

    }6 static { //static block

    7 System.out.println("static block");

    8 a += 1;

    9 }

    10 }

    11

    12 //continued...

    Java Program Structure:

  • 7/31/2019 MELJUN CORTES JEDI Slides-Intro2-Chapter01-Review of Basic Concepts in Java

    60/71

    Introduction to Programming 2 60

    The staticKeyword13 class StaticDemo {14 public static void main(String args[]) {

    15 System.out.println(Demo.a);

    16 Demo.staticMethod(5);

    17 Demo d = new Demo();

    18 System.out.println(d.a);

    19 d.staticMethod(0);

    20 Demo e = new Demo();

    21 System.out.println(e.a);

    22 d.a += 3;

    23 System.out.println(Demo.a+", "+d.a+", "+e.a);

    24 }

    25 }

    Java Program Structure:

  • 7/31/2019 MELJUN CORTES JEDI Slides-Intro2-Chapter01-Review of Basic Concepts in Java

    61/71

    Introduction to Programming 2 61

    The staticKeyword

    Output:

    This is a static block.

    1

    510

    14, 4, 4

    Java Program Structure:

  • 7/31/2019 MELJUN CORTES JEDI Slides-Intro2-Chapter01-Review of Basic Concepts in Java

    62/71

    Introduction to Programming 2 62

    The finalKeyword Applied to variables, methods and classes

    Restricts what we can do with the variables, methods andclasses

    finalvariable

    Cannot be modified once its value has been set

    Example:

    final int data = 10;data++;

    Java Program Structure:The final Ke ord

  • 7/31/2019 MELJUN CORTES JEDI Slides-Intro2-Chapter01-Review of Basic Concepts in Java

    63/71

    Introduction to Programming 2 63

    The finalKeyword finalmethod

    Cannot be overridden

    Example:

    final void myMethod() { //in a parent class

    }

    void myMethod() { //in a child class

    }

    finalclass

    Cannot be inherited

    Example:

    final public class MyClass {}

    class WrongClass extends MyClass {}

    Java Program Structure:f

  • 7/31/2019 MELJUN CORTES JEDI Slides-Intro2-Chapter01-Review of Basic Concepts in Java

    64/71

    Introduction to Programming 2 64

    The finalKeyword Keyword may be placed before after other modifiers

    public final static void meth() {} or

    final public static void meth() {} or ...

    //order of modifiers is not important

    Java Program Structure:I Cl

  • 7/31/2019 MELJUN CORTES JEDI Slides-Intro2-Chapter01-Review of Basic Concepts in Java

    65/71

    Introduction to Programming 2 65

    Inner Classes Class declared within another class

    Accessing the members of the inner class:

    Need to instatiate an inner class member first

    Example:

    innerObj.innerMember = 5;

    //innerObj is an instance of the inner class

    //innerMember is a member of the inner class

    Java Program Structure:I Cl

  • 7/31/2019 MELJUN CORTES JEDI Slides-Intro2-Chapter01-Review of Basic Concepts in Java

    66/71

    Introduction to Programming 2 66

    Inner Classes Methods of the inner class can directly access members of

    the outer class

    Example:

    1 class Out {

    2 int OutData;

    3 class In {

    4 void inMeth() {

    5 OutData = 10;

    6 }

    7 }8 }

    Java Program Structure:I Cl

  • 7/31/2019 MELJUN CORTES JEDI Slides-Intro2-Chapter01-Review of Basic Concepts in Java

    67/71

    Introduction to Programming 2 67

    Inner Classes1 class OuterClass {2 int data = 5;

    3 class InnerClass {

    4 int data2 = 10;

    5 void method() {

    6 System.out.println(data);

    7 System.out.println(data2);

    8 }

    9 }

    10

    11 //continued...

    Java Program Structure:

  • 7/31/2019 MELJUN CORTES JEDI Slides-Intro2-Chapter01-Review of Basic Concepts in Java

    68/71

    Introduction to Programming 2 68

    Java Program Structure:

    9 public static void main(String args[]) {10 OuterClass oc = new OuterClass();

    11 InnerClass ic = oc.new InnerClass();

    12 System.out.println(oc.data);

    13 System.out.println(ic.data2);

    14 ic.method();

    15 }

    16 }

    Java Program Structure:

  • 7/31/2019 MELJUN CORTES JEDI Slides-Intro2-Chapter01-Review of Basic Concepts in Java

    69/71

    Introduction to Programming 2 69

    Java Program Structure:

    Output:

    5

    10

    Summary

  • 7/31/2019 MELJUN CORTES JEDI Slides-Intro2-Chapter01-Review of Basic Concepts in Java

    70/71

    Introduction to Programming 2 70

    Summary

    Object-Oriented Concepts

    Object-Oriented Design

    Class

    Object

    Attribute

    Method

    Constructor

    Package

    Encapsulation

    Abstraction

    Inheritance

    Polymorphism

    Interface

    Summary

  • 7/31/2019 MELJUN CORTES JEDI Slides-Intro2-Chapter01-Review of Basic Concepts in Java

    71/71

    Introduction to Programming 2 71

    Summary

    Java Program Structure

    Declaring Java Classes

    Declaring Attributes

    Declaring Methods

    Declaring a Constructor

    Instantiating a Class

    Accessing Object Members

    Packages

    The Access Modifiers

    Encapsulation

    Inheritance

    Overriding Methods

    Abstract Classes and Methods

    Interface

    The this Keyword

    The superKeyword

    The staticKeyword

    The finalKeyword

    Inner Classes