{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

16387526-Object-Oriented-Programming-02-Java-Fundamentals

16387526-Object-Oriented-Programming-02-Java-Fundamentals -...

Info iconThis preview shows pages 1–8. Sign up to view the full content.

View Full Document Right Arrow Icon
1 TCP1311 Object Oriented Programming Lecture 2: Java Fundamentals
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
2 TCP1311 Object Oriented Programming Java application program +creditAmount(amount : double) +withdrawAmount(amount : double) +printBalance() +balance : double +acc_number : int +name : String Account class modifier class name public class Account{ double balance; int acc_number; Variables String name; void creditAmount(double amount){ Methods balance=balance+amount; } void withdrawAmount(double amount){ balance=balance-amount; } void printBalance(){ System.out.println ("Balance amount in Account No: " + acc_number + " is RM " + balance); { {
Background image of page 2
3 TCP1311 Object Oriented Programming Account a1; a1 = new Account(); null a1 balance acc_number name a1 Account a1 = new Account(); To create an object of Class Account
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
4 TCP1311 Object Oriented Programming a1.balance = 2300 a1.acc_number = 131 a1.name = “Xmen” a1.creditAmount(….); a1.withdrawAmount(…); a1.printBalance(); +creditAmount(amount : double) +withdrawAmount(amount : double) +printBalance() +balance : double = 2300 +acc_number : int = 131 +name : String = Xmen Account Please ignore + symbols To refer variables and methods of an object a1 (of Class Account) outside of its class.
Background image of page 4
5 TCP1311 Object Oriented Programming Access Modifier and Scope Control who access the object’s variables and methods private (accessible within the class only) <invisible>* (ditto, within same package) protected (ditto, within subclasses) public (everywhere) * Known as default level or sometimes, package level
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
6 TCP1311 Object Oriented Programming // save as Account.java public class Account{ double balance; int acc_number; String name; void creditAmount(double amount){...} void withdrawAmount(double amount){...} void printBalance(){…} } // save as MyProg.java class MyProg{ public static void main (String [] args) { Account a1 = new Account(); a1.balance = 2300; a1.acc_number = 131; a1.name = “Xmen”; a1.creditAmount(200); a1.withdrawAmount(500); a1.printBalance(); // 2000 } Access Modifier and Scope
Background image of page 6
7 TCP1311 Object Oriented Programming // save as Account.java public class Account{ private double balance; private int acc_number; private String name; void creditAmount(double amount){...} void withdrawAmount(double amount){...} void printBalance(){…} } // save as MyProg.java class MyProg{ public static void main (String [] args) { Account a1 = new Account(); a1.balance = 2300; a1.acc_number = 131; a1.name = “Xmen”; a1.creditAmount(200); a1.withdrawAmount(500); a1.printBalance(); // 2000 } error!!
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Image of page 8
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}