Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Classes and Objects Overview l Classes and Objects l Constructors l Implicit Constructors l Overloading methods this keyword l public, private and protected.

Similar presentations


Presentation on theme: "1 Classes and Objects Overview l Classes and Objects l Constructors l Implicit Constructors l Overloading methods this keyword l public, private and protected."— Presentation transcript:

1 1 Classes and Objects Overview l Classes and Objects l Constructors l Implicit Constructors l Overloading methods this keyword l public, private and protected l Mutator and Accessor methods

2 2 Classes And Objects In Java, problems are solved by interaction among objects, with each object providing services in its area of specialization (through its methods) to other objects. – hence, Java is called an Object- Oriented Language. Objects are created from classes, thus, to write Java programs one must learn how to write classes. A class usually consists of fields (variables) and methods. These collectively are called class members.

3 3 Class and Objects Example class BankAccount { public double balance = 0.0; public void deposit(double amount ) { balance += amount ; } class RunBank { public static void main(String args[]) { BankAccount richStudent = new BankAccount(); BankAccount poorInstructor; poorInstructor = new BankAccount(); richStudent.deposit(10000); poorInstructor.deposit(5.10 ); System.out.println("Student Balance:" + richStudent.balance ); System.out.println("Prof Balance: " + poorInstructor.balance)); }

4 4 Constructors l A class may also have one or more constructors, one of which is used to initialise the fields when an object of the class is created. l If a class has more than one constructor, then each constructor must have a different signature – number, type and/or position of parameters.

5 5 Constructor Example class BankAccount { public double balance; //Constructor public BankAccount(double initialBalance) { balance = initialBalance; } public BankAccount() { //Constructor balance = 0.0; } public void deposit(double amount) { balance += amount ; } public String toString() { return "Account Balance: " + balance; }

6 6 Constructor Example class RunBank { public static void main( String args[] ) { BankAccount richStudent = new BankAccount(10000); BankAccount poorInstructor = new BankAccount(); poorInstructor.balance = 100; System.out.println( "Prof: " + poorInstructor ); System.out.println( "Student: " + richStudent ); }

7 7 Constructor Example l Notice the addition of toString() method in the above example. l This is a special method which java automatically called to convert objects to strings where necessary. l This happens in println() and when adding objects to strings

8 8 Implicit Constructors l If a class has no constructor, the compiler generates a default (or implicit) constructor for the class. The default constructor initializes those fields of the object that are not already initialised with their default values. l The default values for the different types are shown in the table below TypeDefault value booleanfalse byte(byte) 0 short(short) 0 int0 long0L char \u0000 float0.0f double0.0d object referencenull

9 9 Overloading Methods l The signature of a method is its name with the number, type and order of its parameters. l The return type is not part of the signature of a method. l Two methods in the same class can have the same name if their signatures are different.

10 10 Overloading Methods Examples class OverLoad { public void same() { System.out.println( "No arguments" ); } public void same( int firstArgument ) { System.out.println( "One int arguments" ); } public void same( char firstArgument ) { System.out.println( "One char arguments" ); } public int same( int firstArgument ) { // Compile Error System.out.println( "One int arguments" ); return 5; } public void same( char firstArgument, int secondArgument) { System.out.println( "char + int arguments" ); } public void same( int firstArgument, char secondArgument ) { System.out.println( "int + char arguments" ); }

11 11 this (not that) l this refers to the object on which the method operates l It is used to pass self as a parameter to methods operating on the object. class BankAccount { public double balance; public BankAccount(double balance) { this.balance = balance; } public void deposit(double amount) { balance += amount ; } public String toString() { return "Account Balance: " + balance; }

12 12 Another example: class CustomerList { public BankAccount[] list = new BankAccount[100]; public int nextFreeSlot = 0; public void add(BankAccount newItem ) { list[nextFreeSlot] = newItem; nextFreeSlot++; } class BankAccount { public double balance; public BankAccount(double balance ) { this.balance = balance; } public void badBalanceCheck( CustomerList badAccounts) { if ( balance <= 0 ) badAccounts.add(this); }

13 13 Another example cont.. class RunBank { public static void main( String args[] ) { BankAccount richStudent = new BankAccount( 10000 ); CustomerList customersToDrop = new CustomerList(); richStudent.badBalanceCheck( customersToDrop); }

14 14 public, private and protected. l Most of the methods of a class are meant to provide services to other objects, thus they should be declared as public. However, the fields should be declared as private or at best protected so that only methods of the class or subclass of this class can change them. l A better BankAccount class is shown below.

15 15 public, private and protected. class BankAccount { private double balance; public BankAccount(double initialBalance) { balance = initialBalance; } public BankAccount() { balance = 0.0; } public double getBalance() { return balance; } public void setbalance(double newBalance) { balance = newBalance; } public void deposit(double amount) { balance += amount ; } public String toString() { return "Account Balance: " + balance; }

16 16 Mutator and Accessor Methods l Methods that allow changes to be made to fields of the class are called mutator methods (e.g deposit and setBalance) l Methods that only access the fields but do not change them are called accessor methods.


Download ppt "1 Classes and Objects Overview l Classes and Objects l Constructors l Implicit Constructors l Overloading methods this keyword l public, private and protected."

Similar presentations


Ads by Google