Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template...

33
Programming in Java Unit 2

Transcript of Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template...

Page 1: Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.

Programming in JavaUnit 2

Page 2: Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.

Class and variable declaration• A class is best thought of as a template from which objects are

created.

• You can create many instances of a class.

• Creating a class is referred to as ‘instantiating’.

• A Java class uses variables to define data fields and methods to define actions.

• A class method/s known as constructors are designed to perform initialization of data fields of objects, and can also perform any other actions.

Page 3: Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
Page 4: Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.

• This program begins with // in line 1, indicating comments in a line.

• Line 2 indicates the program file is in a package called welcome

• In line 3, public class Welcome is a programmer defined class

• Class is a keyword(reserverd by Java) that is used as a prefix to a class name

• In line 5, public static void main (String []args) indicates the main method which is void thus it returns nothing.

• In line 7, System.out.println (“Welcome to Java 2015”) instructs the computer to perform an action thus it will print a string of characters that are within the double quotes.

Page 5: Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.

State (instance variables)• Each object (instance of a class) will have its own unique set of

instance variables as defined in the class.

• Collectively, the values assigned to an instance variables make up the object's state.

• You must always refer to it with the object that you declared, as shown in the next example:

ClassName myObj = new ClassName();

myObj.theVariable = 90;

Page 6: Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.

Behaviour (methods)• Methods (called functions or procedures in other languages) actually

assist you in modularizing a program into self-contained manageable units.

• The main method is called once to begin program execution.

• When programmers create a class, they create methods for that class.

• Methods are where the class' logic is stored.

• Methods are where the real work gets done.

• They are where algorithms get executed, and data gets manipulated.

Page 7: Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.

Constants and variables• Variables should start with a lowercase letter, short, meaningful

names, which sound good to us as programmers.

• Some examples:   buttonWidth accountBalance myString

• A constant variable is a variable that contains a value which does not change for the entire program, thus it should be declared with the keyword final.

• Constants should be named using uppercase letters with underscore characters as separators. private final int INCREMENT

//Declares a constant called INCREMENT of type int that cannot be modified

Page 8: Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.

//Declaring class called Dog public class Dog { //Declaring variables name and colour of type string String name; String colour; public void eat() {

//method called eat //Eat code in here }

public void bark() { //Bark code in here

}

}

Page 9: Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.

Access modifiers• A class can consist of modifiers as well.

• Access modifiers determine the visibility or accessibility of an object’s attributes and methods to other objects.

• Before we can begin implementing our design, we must consider which attributes and methods of our classes should be public and which should be private.

• A modifier is a keyword that is added in front of the class and is used to describe how the class can be accessed or used.

• Variables or methods declared with access modifier private are accessible only to the methods of the class to which they are declared.

Page 10: Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.

Public (class) modifier• A public class may be accessed from anywhere.

• Classes that are not public can only be accessed from other classes that are within the same directory. public class Dog {

//Class body

}

• When a class is declared public, it is visible to all other classes.

• Take note that you may only have one public class in each file.

• The name of the file must also be the same as the name of the public class.

• When using the access modifier public, it means that that classes, methods and data fields can be accessed from other classes.

Page 11: Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.

Final (class) modifier• A final class may not be extended.

• The compiler will return an error if you try to extend a class with a final modifier. final class Dog {

//Class body

}

Page 12: Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.

Abstract (class) modifier• An abstract class is used to declare one or more abstract methods.

• A class cannot be both final and abstract.

• It can be public and abstract, though. abstract class Dog {

//Class body

}

Page 13: Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.

Default (class) modifier• When you do not specify an access modifier for a class, the class will

have default/package access.

• This means that the class will only be visible, and can only be used, in the current package or directory.

Page 14: Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.

Public (member) modifier• Public access for members of a class works in the same way as it does

with classes.

• The members are visible to, and can be used by, any other class.

Page 15: Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.

Default (member) modifier• Members with default access are only visible to other classes in the

same package.

• This happens when there is no access modifier declared, then by default the classes, method and data fields are accessible by any class in the same package.

• This is known as package-access.

Page 16: Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.

Protected (member) modifier• Protected members are the same as default members, but are also

visible to subclasses of a class that can be in any other package.

Page 17: Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.

Private (member) modifier• Private members are only visible to the class and cannot be used by

any other class.

• A private data field cannot be accessed by an object from outside the class that defines the private field.

• To make a private data field accessible, provide a get method to return its value and provide a set method to set a new private data field value.

Page 18: Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
Page 19: Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.

The this reference• Every object can access a reference to itself by using the keyword this also known as the this reference.

• The this keyword is the name of a reference that refers to a calling object itself.

• Its common uses are to reference a class's hidden data fields.

• A hidden instance variable can be accessed by using the keyword this.

Page 20: Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.

Set and Get methods• Set methods are also known as mutator methods as they change the

object’s state.

• This means that they modify values of instance variables.

• Get methods are also known as accessor/query methods.

Page 21: Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.

Composition• A class can have reference to objects of other classes as members.

• This is called ‘composition’ and is usually termed the has-a relationship.

Page 22: Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.

Garbage collection through the method finalize• The method finalize is a method of class object under package

java.lang.

• The JVM performs automatic garbage collection to reclaim memory occupied by redundant objects.

• This means that when there are no more references to an object it is eligible to be collected.

• The finalize method is called by the garbage collector to terminate an object just before it reclaims memory.

• This method finalize has no argument list and always has return type void.

Page 23: Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.

Static class members• The declaration begins with keyword static.

• This is normally used when every object has its own copy of all the instance variables of the class, therefore one copy of a variable will be shared by all objects of a class thus we make use of static variables to represent class wide information.

• A static cannot access non-static class members, because a static method can be called even when no objects of the class have been instantiated, thus the this reference cannot be used in a static method.

Page 24: Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.

Creating packages• Packages are used to ease complexity and be able as programmers to

manage application components.

• They also aid in software reuse as well as unique naming convention.

• This basically means that each class in the Java API belongs to a package that contains a cluster of related classes.

Page 25: Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.

Constructors• Each class you declare can provide a special method called constructor that

can be used to initialise an object of a class when the object is created.

• A constructor must have the same name as the class itself (defining class).

• Constructors do not have a return type - not even void.

• Constructors are invoked using the new operator when an object is created, which plays the role of object initialisation

• Java requires a constructor call for every object that is created.

• A constructor must have the same name as the class and it uses the keyword new which requests memory to store the object, and then calls the corresponding class’s constructor to initialise the object.

• A constructor without arguments is referred to as a no-argument constructor.

• A class that is defined without a constructor calls a no-argument constructor by default.

Page 26: Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.

Overloaded constructors• This enables objects of a class to be initialised in different ways.

• To achieve this one should simply provide multiple constructors with different signatures or parameter/argument list.

Page 27: Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.

Method overloading• Methods of the same name can be declared in the same class having

different set of arguments or parameters.

• When an overloaded method is called, the Java compiler selects the appropriate method based on the arguments and their signature.

Page 28: Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
Page 29: Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.

Method overriding• When you define a method in your subclass that has exactly the same

signature as a method in the superclass, the method is overridden and replaces the inherited method.

• When overriding methods, you are not allowed to make the methods more private, for example, you cannot define a public method as protected.

Page 30: Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
Page 31: Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.

Relational operators• High-level programming like Java provides selection statements that

allow choice of action with alternative courses.

• Java selection statements use conditions, which are Boolean in expressions.

• This means that conditions are expressions that can be true or false, thus a computer program should be able to make a decision based on a condition’s value.

Page 32: Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.

==

Page 33: Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.

Test your knowledge• Complete Self Review Exercises from your e-book, p. 67 - 74.

• Complete exercises from your Toolbox Research exercise: 2, 3 Group work: 1, 2, 5, 10 Tutorial: 2, 3 Lab exercises: 1, 2, 3, 7