Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- • Class’s Client
- o Any class that uses class X is called a client of X
- • Public vs. Private
- o Public constructors and methods of a class constitute its interface with classes that use it – its clients
- o All fields are usually declared private – they are hidden from clients
- o Static constants occasionally can be public
- o “Helper” methods that are needed only inside the class are declared private
- o A private field is accessible anywhere within the class’s source code.
- o Any object can access and modify a private field of another object of the same class.
- • Accessors and Modifiers
- o A programmer often provides methods, called accessors, that return values of private fields; methods that set values of private fields are called modifiers or mutators.
- o Accessors’ names often start with get, and modifiers’ names often start with set
- o These are not precise categories; the same method can modify several fields or modify a field and also return its old or new value.
- • Encapsulation
- o Hiding the implementation details of a class (making all fields and helper methods private) is called encapsulation
- o Encapsulation helps in program maintenance; a change in one class does not affect other classes
- o A client of a class interacts with the class only through well-documented public constructors and methods; this facilitates team development
- • Constructors
- o A constructor is a procedure for creating objects of the class.
- o A constructor often initializes an object’s fields.
- o Constructors do not have a return type (not even void) and they do not return a value.
- o All constructors in a class have the same name – the name of the class.
- o Constructors may take parameters.
- o If a class has more than one constructor, they must have different numbers and/or types of parameters
- o Programmers often provide a “no-args” constructor that takes no parameters (aka arguments)
- o If a programmer does not define any constructors, Java provides one default no-args constructor, which allocates memory and sets fields to default values.
- o Constructors of a class can call each other using the keyword this – a good way to avoid duplicating code.
- • Operator new
- o Constructors are invoked using the operator new
- o Parameters passed to new must match the number, types, and order of parameters expected by one of the constructors.
- o You must create an object before you can use it; the new operator is a way to do it.
- i.e ratio = new Fraction (3, 4)
- • Methods
- o To define a method:
- Decide between public and private (usually private)
- Give it a name
- Specify the types of parameters and give them names
- Specify the method’s return type or choose void
- Write the method’s code
- o A method is always defined inside a class
- o A method returns a value of the specified type unless it is declared void; the return type can be any primitive data type or a class type
- o A method’s parameters can be of any primitive data types or class types
- o A method name starts with a lowercase letter
- o Method names often sound like verbs
- o The name of a method that returns the value of a field often starts with get:
- getWidth, getX
- o The name of a method that sets the value of a field often starts with set
- setLocation, setText
- • Passing Parameters to Constructors and Methods
- o Any expression that has an appropriate data type can serve as a parameter
- o A “smaller” type can be promoted to a “larger” type (for example, int to long, float to double)
- o Int is promoted to double when necessary
- o Objects are always passed as references: the reference is copied, not the object.
- o A method can change an object passed to it as a parameter because the method gets a reference to the original object.
- o Inside a method, this refers to the object for which the method was called. This can be passed to other constructors and methods as a parameter
- • Return statement
- o A method, unless void, returns a value of the specified type to the calling method
- o The return statement is used to immediately quit the method and return a value
- o A void method can use the return statement to exit early
- o If its return type is a class, the method returns a reference to an object (or null)
- o Often the returned object is created in the method using new. For example:
- Return new Fraction(denom, num)
- • Overloaded Methods
- o Methods of the same class that have the same name but different numbers or types of parameters are called overloaded methods
- o Use overloaded methods when they perform tasks
- o The compiler treats overloaded methods as completely different methods
- o The compiler knows which one to call based on the number and the types of the parameters passed to the method
- • Static Fields
- o A static field (aka class field or class variable) is shared by all objects of the class
- o A non-static field (aka instance field or instance variable) belongs to an individual object
- o A static field can hold a constant shared by all objects of the class
- o A static field can be used to collect statistics or totals for all objects of the class (for example, total sales for all vending machines)
- o Static fields are stored with the class code, separately from instance variables that describe an individual object
- o Public static fields, usually global constants, are referred to in other classes using dot notation: ClassName.constName
- o Usually static fields are NOT initialized in constructors (they are initialized either in declarations or in public static methods)
- o If a class has only static fields, there is no point in creating objects of that class (all of them would be identical)
- o Math and System are examples of the above. They have no public constructors and cannot be instantiated.
- • Static Methods
- o Static methods can access and manipulate class’s static fields
- o Static methods cannot access non-static fields or call non-static methods of the class
- o Static methods are called using “dot notation”: ClassName.statMethod(…)
- o Main is static and therefore cannot access non-static fields or call non-static methods of its class
- • Non-Static Methods
- o A non-static method is called for a particular object using “dot notation”: objName.instMethod
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement