CS111: PROGRAMMING LANGUAGE II Lecture 1: Introduction to classes
Lecture Contents 2 What is a class? Encapsulation Class basics: Data Methods Objects Defining and using a class
In Java 3 Java is an object-oriented programming language As the term implies, an object is a fundamental entity in a Java program Everything must be in a class (Even Main method).
What is a class? 4 Class: Defines a data type (blueprint) Combines data and operations in one place: data / fields/ properties / state variables Operations/ behavior / methods that modify state
What is a data type? Any data type includes Data (range of values) Operations (that can be performed on data) Example: int data type has: Data: +-32,767 Operations: +, -, *, /, % Same with classes Collection of data values together with set of basic operations to be allowed for the values 6-5
Class = Blueprint One blueprint to create several similar, but different, houses: Copyright 2012 Pearson Education, Inc.
7 Encapsulation The bundling of data and procedures(methods) into a single unit(called class). Class : various data elements and member functions are wrapped up together. main feature of object oriented programming
Objects & Classes objects represent things from the real world, or from some problem domain (example: the red car down there in the car park ) classes represent all objects of a kind (example: car ) Objects First with Java - A Practical Introduction using BlueJ, David J. Barnes, Michael Kölling
Objects & Classes 9 variables of class type are objects Object: an instance / realization of a class same as relationship between a datatype and its variables
How to define a Class?? 10 Type definition Object declaration Member access
Basic class structure public class ClassName { Fields Constructors Methods } The contents of a class Objects First with Java - A Practical Introduction using BlueJ, David J. Barnes, Michael Kölling
Defining Classes 12 Identifying Class Attributes Classes have attributes (data) and operations (behaviors). Class attributes are implemented in Java programs as fields, and class operations are implemented as methods.
the Unified Modeling Language (UML) 13 can be defined as a modeling language to capture the architectural, behavioral and structural aspects of a system. The UML has an important role in object oriented analysis and design. Objects are the key to this object oriented world The basic requirement of is to identify the object efficiently.
UML Class diagrams 14 Class diagrams describes the objects in a system and their relationships, so it is widely used by the developer community. The UML representation of a class is a rectangle containing three compartments stacked vertically. The top compartment shows the class's name. The middle compartment lists the class's attributes. The bottom compartment lists the class's operations.
15 Examples What about a student??
The student class - Exercise 16 Draw UML class diagram for class named Student where data is: name, UnivID, date of birth, department, level, GPA Methods are: PrintData()
A class in Java 17.java filename extension The class keyword Public keyword access modifier class is available to the public No main can t execute // Student.java public class Student { // data.. } //methods... //not static!!
Student Test 18 Another class. Why not Import student.java?? Using new object instantiation Dot separator // StudentTest.java public class Students { public static void main (String[] args) { //define object(s) //call a method }
19 Access modifiers Controlling Access to Members Access modifiers Public Methods Client s view of the services provided by the class Private data not accessible outside the class protected. Computer Science Department
Public and Private Members Data in class almost always designated private in definition! Upholds principles of OOP (Data Hiding) Hide data from user Allow manipulation only via operations Which are member methods 6-20
Mark Visibility type + Public # Protected - Private ~ Package 21 Marks for UML-supported visibility types
Example 22. Modify class efinition // Student.java public class Student { // data private //methods public }
Student Test 23 Try to access private members!! What about the public ones? // StudentTest.java public class StudentTest { public static void main (String[] args) { //define object(s) //access different members }
Set/get methods 24 Object needs to have access" to its data accessor methods Allow object to read data Also called "get methods" Simple retrieval of member data Mutator methods Allow object to change data Also called set methods" Manipulated based on application Computer Science Department
Example: set/get methods 25. Class Methods have direct access to class members (data or methods) // Student.java public class Student { // data //methods //set methods //get methods }
Student Test 26 The set method can be used to modify a data field the get method can be used to display the updated value // StudentTest.java public class StudentTest { public static void main (String[] args) { //define object(s) //modify data!! }
Constructors 27 A constructor can be used to initialize an object of a class when the object is created.
Example: constructors 28. Constructor must be public Same name as class no return type Parameters?? // Student.java public class Student { // data //an empty constructor public Student() { } //set/get methods } //other methods
Student Test 29 Keyword new calls the class s constructor to perform the initialization. // StudentTest.java public class StudentTest { public static void main (String[] args) { //define object(s)using empty //constructor } //print data
The default Constructor 30 By default, the compiler provides a default constructor with no parameters in any class that does not explicitly include a constructor. If you declare any constructors for a class, the Java compiler will not create a default constructor for that class With the default constructor, its instance variables are initialized to their default values.
Overloaded Constructors 31 Overloaded constructors enable objects of a class to be initialized in different ways. multiple constructor declarations with different signatures. the compiler differentiates signatures by : the number of parameters, the types of the parameters and the order of the parameter types in each signature.
Example: constructors 32. Define more Constructors Parameters?? // Student.java public class Student { // data //constructors public Student() { } //set/get methods } //other methods
Student Test 33 More objects Different initialization Same methods, different data // StudentTest.java public class StudentTest { public static void main (String[] args) { //define object(s)using different //constructors } //print data
Using this Reference 34 Every object can access a reference to itself with keyword this. Implicitly: refer to the object s instance variables and other methods inside member methods. Enables the class s code to know which object should be manipulated. Explicitly: Can also use keyword this in a non-static method s body.
Example: constructors 35. Use this reference to access class members // Student.java public class Student { // data //constructors public Student() { } //set/get methods } //other methods
36 Important hint Dr. Amal Khalifa, 2013
37 Be careful!! Dr. Amal Khalifa, 2013
38 That s all for today.. Text Book: Chapter 3