1 Programming II (CS300) Chapter 03: Creating Classes MOUNA KACEM mouna@cs.wisc.edu Spring 2019
Creating Classes 2 Constructors and Object Initialization Static versus non-static fields/methods Encapsulation Practice Example Introduction to Abstraction Keep in Mind
Constructors and Object Initialization 3 In Java, an object is an instance of any non-primitive type When a reference type is declared, no object is allocated. At that point, the reference is to null Objects must be explicitly constructed Objects are created with the operator new new calls a special type of subroutine called constructor When invocated, the constructor allocates memory for the object, initializes the object s instance variables, and returns a reference to the object
Constructors and Object Initialization 4 A constructor does not have any return type (not even void) The name of the constructor must be the same as the name of the class in which it is defined A constructor can t be declared static Every class has at least one constructor default constructor Does not take any argument allocate memory and initialize instance variables Overloaded constructor
Static versus non-static fields/methods 5 Static fields, variables, and constants have values that are shared across all instances (objects) of a particular class Static fields and methods belong to the Class rather than to an individual object No instance of the class is required in order to use a static field or a static method Non-static fields and non-static methods defined in a class can be used only when an object of that class has been created Heap (memory Allocation) Code Static Memory Stack (method call) console.log() Main() JVM
Creating Classes 6 Constructors and Object Initialization Static versus non-static fields/methods Encapsulation Practice Example Introduction to Abstraction Keep in Mind
Encapsulation Concept 7 Encapsulation refers to information hiding The fact of packaging a number of features of an entity/object in a capsule to form a single unit In object-oriented programming, the single unit combining data and methods is called a Class
How to enforce Encapsulation Class 8 Class Private Fields (data) Public methods (behavior) private String field1; private int field2; To enforce encapsulation Hidden information Visible information The data is hidden : not accessed directly (private fields) The operations are visible: accessed directly (public methods) public void setfield1 (String s){ public String getfield1 (){ public void operationx( ){ Fields of the class are manipulated using public getter and setter methods (accessors and mutators)
Practice Example 9 Create a Student class with the following: A String variable named name to store the student s name An integer variable named uniqueid that contains the unique ID number for this student An integer class variable named nextuniqueid that contains the unique ID number for the next student A String variable named DoB to store the student s date of birth An integer class variable named numberofstudents that keeps track of the number of students that have been created so far A constructor Student(String name, String DoB) Several public get/set methods
Practice Example 10 public class Student { private String name; private int uniqueid; private String DoB; //Constructor public Student(String name, String DoB) { this.name = name; this.dob = DoB;
Practice Example 11 public class Student { private String name; private int uniqueid; private static int nextuniqueid = 1; private String DoB; private static int numberofstudents = 0; //Constructor public Student(String name, String DoB) { this.name = name; uniqueid = nextuniqueid; nextuniqueid++; this.dob = DoB; numberofstudents++; //Getters and setters methods for all properties public String getname() { return name; public void setname(string newname) { name = newname; public int getuniqueid() { return uniqueid; public int getnextuniqueid() { return nextuniqueid; public String getdob() { return DoB; public void setdob(string newdob) { DoB = newdob; public int getnumberofstudents() { return numberofstudents;
Encapsulation: Advantages 12 Encapsulation provides a layer of security around the manipulated data protecting it from external interference and mis-use A class can have total control over what is stored in its fields The users of a class ignore how the class stores the data The fields of a class can be made ready-only or write-only by providing getter and setter methods as the only access points to these fields from the outside of the class Encapsulation promotes the code s Maintainability Flexibility extensibility
Creating Classes 13 Constructors and Object Initialization Static versus non-static fields/methods Encapsulation Practice Example Introduction to Abstraction Keep in Mind
Introduction to Abstraction 14 Abstraction Providing the essential features of an entity/object without including the implementation details hide unnecessary details of an object from the user In Object-Oriented Programming, a feature can be An attribute/field reflecting a property (data or state) An operation reflecting a method (or behavior or function)
Introduction to Abstraction 15 Abstraction Essential element of object-oriented programming Abstraction specifies necessary and sufficient descriptions rather than the implementation details Hiding internal implementation details of an object from the user In a real-world problem, humans manage complexity through abstraction Abstraction allows people to focus on what an object is able to do rather than on its technical implementation Car iphone
Abstraction versus Encapsulation 16
Abstraction versus Encapsulation 17 Abstraction Separates interface and implementation Provides access to a specific part of the data (Users know only how to use the visible part of the data according to the abstraction without allowing access to the implementation details) The ability to encapsulate and isolate design from execution information Encapsulation Groups related concepts into one unit Encapsulation hides data and users have only access to them through the accessors and mutators methods Encapsulation is a concept embedded in abstraction
Keep in Mind 18 A constructor defines how an object is declared and initialized. A constructor is a method that has the same name as the class and no return type If you write a constructor with return type void, you have actually written a method with the same name as the class, but this is NOT a constructor A class may define multiple constructors A constructor is invoked using the keyword new Private members cannot be accessed outside of the class.
Keep in Mind 19 Encapsulation is the process of combining data and functions into a single unit called class To enforce encapsulation, define the class fields as private and define public getter and setter methods to manipulate them Private members (fields or methods) cannot be accessed outside of the class Abstraction is a process where you show only relevant data and hide unnecessary details of an object from the user. The class consists of two parts: the specification (accessible) and the implementation (hidden)