Module 5 - Creating a Class Using Encapsulation

Embed Size (px)

Citation preview

  • 8/13/2019 Module 5 - Creating a Class Using Encapsulation

    1/20

    Java Programming Language

    Module 5

    Creating a Class: Using Encapsulation

  • 8/13/2019 Module 5 - Creating a Class Using Encapsulation

    2/20

    Java Programming Language Module 5, slide 2 of 20Copyright 2007 Sun Microsystems, Inc. All Rights Reserved. Sun Services, Revision G

    Objectives

    Understand the concept of encapsulation

    Implement encapsulation in the Java language Use the statickeyword

    Examine staticimports

  • 8/13/2019 Module 5 - Creating a Class Using Encapsulation

    3/20

    Java Programming Language Module 5, slide 3 of 20Copyright 2007 Sun Microsystems, Inc. All Rights Reserved. Sun Services, Revision G

    Examining the Concept of Encapsulation

    Unencapsulated data

    year:int

    day:int

    month:int

    symbol:String

    name:String

    price:double

  • 8/13/2019 Module 5 - Creating a Class Using Encapsulation

    4/20

    Java Programming Language Module 5, slide 4 of 20Copyright 2007 Sun Microsystems, Inc. All Rights Reserved. Sun Services, Revision G

    Encapsulation Steps

    Encapsulation Step 1: Group-Related Data

    MyDate

    day : int

    month : int

    year : int

    Stock

    symbol : String

    name : String

    price : double

  • 8/13/2019 Module 5 - Creating a Class Using Encapsulation

    5/20

    Java Programming Language Module 5, slide 5 of 20Copyright 2007 Sun Microsystems, Inc. All Rights Reserved. Sun Services, Revision G

    Encapsulation Steps

    Group Data With Behavior

    setDay(int) : boolean

    setYear(int) : boolean

    setMonth(int) : boolean

    getDay() : int

    getMonth() : int

    getYear() : int

    MyDate

    day : int

    month : int

    year : int

    Verify days in month

    setName()

    setPrice(double)

    Stock(symbol : String)

    getName() : String

    getPrice() : double

    Stock

    symbol : String

    name : String

    price : double

    getSymbol() : String

  • 8/13/2019 Module 5 - Creating a Class Using Encapsulation

    6/20

    Java Programming Language Module 5, slide 6 of 20Copyright 2007 Sun Microsystems, Inc. All Rights Reserved. Sun Services, Revision G

    Encapsulation Steps

    Implement Access Control

    +setDay(int) : boolean

    +setYear(int) : boolean

    +setMonth(int) : boolean

    +getDay()

    +getMonth()

    +getYear()

    MyDate-day : int

    -month : int

    -year : int

    +setName()

    +setPrice(double)

    +Stock(symbol : String)

    +getName() : String

    +getPrice() : double

    -symbol : String

    -name : String

    -price : double

    +getSymbol() : String

    + symbol represents external or public access- symbol represents internal or private access

  • 8/13/2019 Module 5 - Creating a Class Using Encapsulation

    7/20

    Java Programming Language Module 5, slide 7 of 20Copyright 2007 Sun Microsystems, Inc. All Rights Reserved. Sun Services, Revision G

    The Benefits of Encapsulation

    Protecting data integrity

    Application maintainability

  • 8/13/2019 Module 5 - Creating a Class Using Encapsulation

    8/20

    Java Programming Language Module 5, slide 8 of 20Copyright 2007 Sun Microsystems, Inc. All Rights Reserved. Sun Services, Revision G

    Implementing Encapsulation in JavaTechnology

    The package statement: A class in a package is visible and therefore

    accessible to all other classes in the same package.

    A class marked public is visible to classes in other

    packages. A class not marked public is hidden to classes in

    other packages.

    The class statement encapsulates attributes,constructors, and methods into a single unit that can becompiled.

  • 8/13/2019 Module 5 - Creating a Class Using Encapsulation

    9/20

    Java Programming Language Module 5, slide 9 of 20Copyright 2007 Sun Microsystems, Inc. All Rights Reserved. Sun Services, Revision G

    Implementing Encapsulation in JavaTechnology

    Access modifiers: Private

    Default

    Protected Public

    Modifier Same Class Same Package Subclass Universe

    private Yesdefault Yes Yes

    protected Yes Yes Yes

    public Yes Yes Yes Yes

  • 8/13/2019 Module 5 - Creating a Class Using Encapsulation

    10/20

    Java Programming Language Module 5, slide 10 of 20Copyright 2007 Sun Microsystems, Inc. All Rights Reserved. Sun Services, Revision G

    Encapsulation Examples

    1 package com.abc.util;2

    3 public class Date {4 private int day;56 public Date() {//... }78 public void addDays(int days) { }

    9 int getDaysInMonth(int month) { }10 }

  • 8/13/2019 Module 5 - Creating a Class Using Encapsulation

    11/20

    Java Programming Language Module 5, slide 11 of 20Copyright 2007 Sun Microsystems, Inc. All Rights Reserved. Sun Services, Revision G

    Encapsulation Examples

    1 package com.abc.brokerage;2

    3 public class Stock {4 private String symbol;5 public Stock(String symbol, double price) { }67 public String getSymbol() { }8 public void setSymbol(String symbol) { }

    9 }

  • 8/13/2019 Module 5 - Creating a Class Using Encapsulation

    12/20

    Java Programming Language Module 5, slide 12 of 20Copyright 2007 Sun Microsystems, Inc. All Rights Reserved. Sun Services, Revision G

    Encapsulation Examples

    1 package com.abc.brokerage;2 importabc.util.Date;

    34 class StockAnalyzer {5 private MyDate date;67 double sell(Stock stock, int quantity) { }8 public double buy(Stock stock, int quantity) { }

    9 }

  • 8/13/2019 Module 5 - Creating a Class Using Encapsulation

    13/20

    Java Programming Language Module 5, slide 13 of 20Copyright 2007 Sun Microsystems, Inc. All Rights Reserved. Sun Services, Revision G

    Using the staticKeyword

    Class attributes

    Class methods

  • 8/13/2019 Module 5 - Creating a Class Using Encapsulation

    14/20

    Java Programming Language Module 5, slide 14 of 20Copyright 2007 Sun Microsystems, Inc. All Rights Reserved. Sun Services, Revision G

    Class Attributes

    1 public class Count {2 private int serialNumber;3 publicstaticintcounter= 0;

    45 public Count() {6 counter++;7 serialNumber = counter;8 }9 }

    c1 : Count

    Count

    serialNumber=1

    -serialNumber : int

    +counter : int = 0

    c2 : Count

    serialNumber=2

    instanceOfinstanceOf

  • 8/13/2019 Module 5 - Creating a Class Using Encapsulation

    15/20

    Java Programming Language Module 5, slide 15 of 20Copyright 2007 Sun Microsystems, Inc. All Rights Reserved. Sun Services, Revision G

    Class Attributes

    Accessing a static field1 public class OtherClass {2 public void incrementNumber() {3 Count.counter++;4 }5 }

  • 8/13/2019 Module 5 - Creating a Class Using Encapsulation

    16/20

    Java Programming Language Module 5, slide 16 of 20Copyright 2007 Sun Microsystems, Inc. All Rights Reserved. Sun Services, Revision G

    Class Methods

    Class method example1 public class Count2 {2 private int serialNumber;3 private static int counter = 0;45 publicstaticintgetTotalCount(){6 return counter;

    7 }89 public Count2() {10 counter++;11 serialNumber = counter;12 }

  • 8/13/2019 Module 5 - Creating a Class Using Encapsulation

    17/20

    Java Programming Language Module 5, slide 17 of 20Copyright 2007 Sun Microsystems, Inc. All Rights Reserved. Sun Services, Revision G

    Class Methods

    Accessing a class method1 public class TestCounter {2 public static void main(String[] args) {3 System.out.println("Number of counter is "4 +Count2.getTotalCount());5 Count2 count1 = new Count2();6 System.out.println("Number of counter is "

    7 +Count2.getTotalCount());8 }9 }

  • 8/13/2019 Module 5 - Creating a Class Using Encapsulation

    18/20

  • 8/13/2019 Module 5 - Creating a Class Using Encapsulation

    19/20

    Java Programming Language Module 5, slide 19 of 20Copyright 2007 Sun Microsystems, Inc. All Rights Reserved. Sun Services, Revision G

    Static Initializers

    Static blocks execute only once

    Static blocks execute in the order of their appearance1 import com.mycompany.utilities.Database.*;2 public class Count4 {3 public static int counter;4 static {5 if (Database.tableExists(?COUNTER?)) {6 counter = Database.getSingleField('COUNTER');7 }8 }9 }10

    11 public class TestStaticInit {12 public static void main(String[] args) {13 System.out.println("counter ="+ Count4.counter);14 }15 }

  • 8/13/2019 Module 5 - Creating a Class Using Encapsulation

    20/20

    Java Programming Language Module 5, slide 20 of 20Copyright 2007 Sun Microsystems, Inc. All Rights Reserved. Sun Services, Revision G

    Static Imports

    1 package com.myproject.shapes;2 static import Math.PI:34 public class Circle {5 private double radius;67 public Circle(double radius) {8 this.radius = radius;

    9 }1011 public double area() {12 return PI*radius*radius;13 }14

    15 public double circumferance {16 return 2*PI*radius;17 }18 }