OOP Classes
OOP Classes
A specification gives:
• The name of the class (type) of object and a brief description of what the objects represent.
(**Class name.)
• A description of constructors, the ways of creating instances of the type of object. (**Data
about the object or what it can have.)
**SHORT RECAP
// The name of the file should begin with The name of the class and end with .java (file
extension)
// So if you write a definition of the class Automobile , it should be in a file named
Automobile.java.
// You can compile a Java class before you have a program in which to use it.
The compiled byte code will have the same name as the class, but ending with .class rather
than .java
// Compiling the file Automobile.java will create a file called Automobile.class.
**Local Variables
A variable defined within one method is local to that method, meaning that if it happens
that there is another variable with the same name but in a different method it does not
change the first variable, this simply because in java variables have memory locations.
This is true whether the variables are in the same class or file.
we have discussed two types of variables -
1. instance variables whose meaning is confined to an object of a class.
2. local variables whose meaning is confined within a method definition.
**Global variable:
Java does not have a global variable rather it has a ==(static variable) that behaves the
same as a global variable.
**Block
Any local variable declared between a set of curly braces is local to that scope, meaning it
cannot be used anywhere else.
% We can call methods within other methods without any receiving object
Implementation:
Private instance variables
Private constants
Private methods
Bodies of public methods
Interface:
Comments
Headings of public methods
Public named constants
Programmer who uses the class
% Application programming interface(API is the same thing as the class interface for the class.
% A well-encapsulated class definition is one where the internal data(instance variables) and
implementation are hidden from external code, and access to these details is controlled through
public methods
% anything that is private cannot be part of the class interface(the external code)
% The body of the method definition is not part of the class interface, only the name can be
used, and the underlying code is hidden in process called abstraction.
% When declaring variables of a primitive type the data value is stored in the memory location
assigned to the variable
% When declaring variables of the class type the the object itself is not stored in the variable
but rather the memory address of the object is assigned to the variable. The object itself is
stored in some other location called **reference.
The address of this other location is called reference to the object. Thus class types are
often called reference types.
Reference types are are types whose variables hold references(memory locations) as
opposed to actual values of objects.
% Variables of a class type(e.g. String) can have varying sizes thus cannot be stored like
variables of a primitive type.
% All class types are reference types but not all reference types are class types.
% Parameters of a class type, Formal parameters hold the memory address of the location of
objects of the class type.
Read the summaries in the book from the most difficult sessions.
**Constructors
% Static variables and static methods belong to a class as a whole and not to an object of a
class.
%static methods()
public static double convertInchesToFeet(double inches) {
return inches / INCHES_PER_FOOT;
}
// We call a static method using the class name. that method
// A non static method can reference a static variable and instance variable
// A static method can reference a static variable/static method but not an instance variable/non-
static method unless it has an object to do so other than the keyword(this) as its calling object
meaning it does not have a separate object other than the class itself.
// Main is a static method.
%The value returned by Math.min(n1, n2) will be of type long because when two numeric
values of different types are compared, Java promotes the smaller type ( int ) to the larger type
( long ) before performing the comparison.
**Wrapper Classes
% A wrapper class in Java is an object that "wraps" or encapsulates a primitive data type (like
int, char, double) in an object so that it can be treated like any other object. This provides extra
functionality and allows primitives to be used in contexts where only objects are allowed.
**Primitive Data Types vs. Wrapper Classes
1. Working with collections: Javas collection classes (like ArrayList, HashSet, etc.) can only
work with objects , not primitive types. So if you need to store integers you cannot store
(int) directly , but you can store an Integer (a wrapper class).
2. Methods that require objects: some methods only want objects not primitives and wrapper
classes allow for this.
3. Utility Methods: Wrapper classes provide useful methods . Like for example Integer class
has methods like Integer.parseInt() to convert strings to integers.
int a = 10;
Integer obj = a; // int to Integer object(primitive to object) // This i
just an abbreviahtion for creating an intger obj
Integer obj = new Integer)
% It is always a good idea to use the trim() method to avoid any trailing white space when using
the parseInt method
Double.parseDouble(theString.trim())
**Overloading
Overloading the method name means giving the same name to more than one method in
the same class.
By doing this you must ensure that the methods have different parameters
% You cannot have methods with the same name and same parameters but different return
types(You can overload on the basis of return type).
**Privacy leak
A private instance variable having a class type could be changed outside of the class.
To avoid this we should use the class type without mutator methods e.g.(String class has
no accessor methods)
Or primitive types to name instance variables.
To avoid the problem of returning an object that could be modified externally (and therefore
altering the internal state of the PetPair class), you should create separate accessor methods
for each property of the pet (name, age, and weight). These methods will return only specific
pieces of data rather than returning the entire object.
Here are the three accessor methods you can define to retrieve the pet's name, age, and
weight without exposing the full object:
}`
Explanation:
1. getFirstPetName(): This method returns the name of the first pet, using the getName()
method of the Pet class.
2. getFirstPetAge(): This method returns the age of the first pet, using the getAge()
method of the Pet class.
3. getFirstPetWeight(): This method returns the weight of the first pet, using the
getWeight() method of the Pet class.
By splitting the accessors in this way, you avoid returning the full Pet object, preventing the
caller from modifying the internal state of the PetPair class indirectly through mutator methods
like setName() , setAge() , or setWeight() .
**ENUMERATION AS A CLASS
The compiler creates a Suit class. The enumerated values are names of public static objects
whose type is Suit. Referenced in this format:
The class has methods: equals, compareTo, ordinal, toString, and valueOf.
**Packages
package general.utilities;
I f we want to import the class HelpfulClass from the package general.utilities we would
say
import general.utilities.HelpfulClass;
.......