Classes and Objects Overview l Classes and Objects

  • Slides: 16
Download presentation
Classes and Objects Overview l Classes and Objects l Constructors l Implicit Constructors l

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

Classes And Objects l In Java, problems are solved by interaction among objects, with

Classes And Objects l 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. l Objects are created from classes, thus, to write Java programs one must learn how to write classes. l. A class usually consists of fields (variables) and methods. These collectively are called class members. 2

Class and Objects Example class Bank. Account { public double balance = 0. 0;

Class and Objects Example class Bank. Account { public double balance = 0. 0; public void deposit(double amount ) { balance += amount ; } } class Run. Bank { public static void main(String args[]) { Bank. Account rich. Student = new Bank. Account(); Bank. Account poor. Instructor; poor. Instructor = new Bank. Account(); rich. Student. deposit(10000); poor. Instructor. deposit(5. 10 ); System. out. println("Student Balance: " + rich. Student. balance ); System. out. println("Prof Balance: " + poor. Instructor. balance)); } 3 }

Constructors l. A class may also have one or more constructors, one of which

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. 4

Constructor Example class Bank. Account { public double balance; //Constructor public Bank. Account(double initial.

Constructor Example class Bank. Account { public double balance; //Constructor public Bank. Account(double initial. Balance) { balance = initial. Balance; } public Bank. Account() { balance = 0. 0; } //Constructor public void deposit(double amount) { += amount ; } balance public String to. String() { return "Account Balance: " + balance; } } 5

Constructor Example class Run. Bank { public static void main( String args[] ) {

Constructor Example class Run. Bank { public static void main( String args[] ) { Bank. Account rich. Student = new Bank. Account(10000); Bank. Account poor. Instructor = new Bank. Account(); poor. Instructor. balance = 100; System. out. println( "Prof: " + poor. Instructor ); System. out. println( "Student: " + rich. Student ); } } 6

Constructor Example l Notice the addition of to. String() method in the above example.

Constructor Example l Notice the addition of to. String() 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 7

Implicit Constructors l l If a class has no constructor, the compiler generates a

Implicit Constructors l 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. The default values for the different types are shown in the table below Type Default value boolean false byte (byte) 0 short (short) 0 int 0 long char 0 L u 0000 float 0. 0 f double 0. 0 d object reference null 8

Overloading Methods l The signature of a method is its name with the number,

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. 9

Overloading Methods Examples class Over. Load { public void same() { System. out. println(

Overloading Methods Examples class Over. Load { public void same() { System. out. println( "No arguments" ); } public void same( int first. Argument ) { System. out. println( "One int arguments" ); } public void same( char first. Argument ) { System. out. println( "One char arguments" ); } public int same( int first. Argument ) { // Compile Error System. out. println( "One int arguments" ); return 5; } public void same( char first. Argument, int second. Argument) { System. out. println( "char + int arguments" ); } public void same( int first. Argument, char second. Argument ) { System. out. println( "int + char arguments" ); } } 10

this (not that) l l this refers to the object on which the method

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

Another example: class Customer. List { public Bank. Account[] list = new Bank. Account[100];

Another example: class Customer. List { public Bank. Account[] list = new Bank. Account[100]; public int next. Free. Slot = 0; public void add(Bank. Account new. Item ) { list[next. Free. Slot] = new. Item; next. Free. Slot++; } } class Bank. Account { public double balance; public Bank. Account(double balance ) { this. balance = balance; } public void bad. Balance. Check( Customer. List bad. Accounts) { if ( balance <= 0 ) bad. Accounts. add(this); } } 12

Another example cont. . class Run. Bank { public static void main( String args[]

Another example cont. . class Run. Bank { public static void main( String args[] ) { Bank. Account rich. Student = new Bank. Account( 10000 ); Customer. List customers. To. Drop = new Customer. List(); rich. Student. bad. Balance. Check( customers. To. Drop); } } 13

public, private and protected. l Most of the methods of a class are meant

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 Bank. Account class is shown below. 14

public, private and protected. class Bank. Account { private double balance; public Bank. Account(double

public, private and protected. class Bank. Account { private double balance; public Bank. Account(double initial. Balance) { balance = initial. Balance; } public Bank. Account() { balance = 0. 0; } public double get. Balance() { return balance; } public void setbalance(double new. Balance) { balance = new. Balance; } public void deposit(double amount) { balance += amount ; } public String to. String() { return "Account Balance: " + balance; } 15

Mutator and Accessor Methods l Methods that allow changes to be made to fields

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 set. Balance) l Methods that only access the fields but do not change them are called accessor methods. 16