Classes, Methods, Constructors, Objects, Access Modifiers, and Overloading
Lesson - 303.8
Java Classes, Methods, Constructors, Objects, Access Modifiers, and Overloading
Learning Objectives:
By the end of this section, learners will be able to:
Define Object-Oriented Programming concepts.
Utilize classes and class members in Java.
Explain objects and instances.
Describe Constructor, Constructor Overloading, and Access Modifier.
Demonstrate the use of constructor, constructor overloading, and access modifiers in Java programming.
Section 1
Object-Oriented Programming
Table of Contents
Topic 1: Overview of Object-Oriented Programming.
Pillars of Object-Oriented Programming.
Topic 1a: Introduction to Java Classes.
What Are Classes?
Classes Composing Functionality.
What are Class Members?
Class Definition in Java.
Class Members - Fields (Class Variables).
Local variables.
Topic 2: Access Specifiers.
Topic 3: Where do Objects Come From?
Topic 4a: Class Members - Constructors.
Topic 4b: Constructor Overloading.
Overview of Object-Oriented Programming
Object-Oriented programming (OOP) is a programming paradigm that uses "objects" – data structures consisting of data fields and methods together with their interactions – to design applications and computer programs. The emphasis is on data rather than on the procedure. Here, we observe two of the tenets of OOP:
Reusability. Classes, once defined, are templates for highly functional objects that we can use and re-use.
Composition. We can define classes that bring disparate types of objects together to achieve advanced functionality and solve specialized problems.
Overview of Object-Oriented Programming (continued)
Classes and Objects are basic concepts of Object-Oriented Programming (OOP), which revolve around the real-life entities.
OOP relies on a few things that you have already used:
Classes – Contains skeletons for your code.
Objects – Contains runtime entities.
Methods – Contains the logic of your code.
Variables – Contains values your code can use.
Pillars of Object-Oriented Programming
The four pillars of (OOP) are:
Encapsulation: The process of wrapping code and data together into a single unit.
Abstraction: The process of hiding implementation details and exposing only the functionality to the user.
Inheritance: The process of one class inheriting properties and methods from another class.
Polymorphism: The ability to perform many things in many ways.
Topic 1a: Introduction to Java Classes
Classes are the foundation of any Java application. All Java code runs within the context of a class. You may recall from our first look at a console application that the main() method — the entry point for the JVM — was written within a class definition.
Well-designed classes represent reusable units of work. For instance:
The String class gives you all of the capabilities that you need to work with string data.
The Scanner class gives you the ability to read and parse values from a console, file, or other sources of character data.
Classes model the real world, or for games and other exercises in imagination, the unreal world.
Medical records software will use classes to represent patients, pathologies, and therapies.
A flight simulator will use classes to represent wind, weather, and a jet. The class representing a jet will certainly be composed of other classes that represent its many components – wings, landing gear, hydraulics, cockpit controls, etc.
What Are Classes?
In Java, a class is a definition of objects of the same kind. In other words, a class is a blueprint, template, or prototype that defines and describes the static attributes and dynamic behaviors common to all objects of the same kind.
A class in Java is a logical template to create Objects that share common properties and methods.
When we define a class, we provide fields to hold the data, getters/setters to provide access to the data, and methods to work with the data. We might also provide mechanisms to notify consumers of the class of changes made to the data. We may also provide constructors to initialize instances of the class when they are created.
Classes Composing Functionality
When creating an application, bring multiple classes together to accomplish the functional objectives of that application. You will write some of the classes yourself.
Many others will come from packages — either the JDK packages, or the many packages that the developer community has made available.
You will discover that some of the classes have utility in several applications, so you will create your own shareable packages.
Composition and code-reuse are two of the core concepts of Object-Oriented Programming (OOP).
What are Class Members?
Classes are composed of distinct parts:
Fields (also called class variables and attributes) - local to the class; they give an object state.
Methods (or behaviors and operations) - specialized to work with the fields and manage the state of objects.
Constructors - give the ability to initialize objects once they are created.
Finalizer - special methods that are called by the Garbage Collector.
Class Definition in Java
How to create a Class in Java:
Classes are declared using the class keyword.
In Java, we use the keyword class to define a class, but it cannot start with a number. The best practice is to use Pascal casing although it is not enforced by the compiler.
Classes can optionally have an access specifier. Classes declared without an access modifier have package access.
The body of a class is contained within a pair of curly braces {}.
Class Members - Fields (Class Variables)
Let’s design our class named Statistics to calculate the minimum, maximum, and sum of a collection of double values. We need variables in which to store these values.
Variables defined within a class are called Fields or Class variables.
Fields or Class variables can be declared anywhere within the class definition; but by convention, they are declared near the top of the class definition. Here, we have declared four fields.
Initializing Fields (Class Variables)
Fields are automatically initialized to default values; they can be explicitly initialized.
Numeric types are initialized to 0.
Reference types are initialized to null.
For our Statistics class, it makes sense to initialize min and max so that they are ready to be used in the loop where we will process a collection of double values.
Local Variables
Local variables are defined inside the method (not outside the method). They are declared and initialized within the method body.
When declaring a local variable, we must initialize it with a default value within the method block because the JVM DOES NOT provide any default values for local variables.
Remember: Class variables or Fields are similar to local variables except:
Fields can be accessed anywhere within the class definition.
Fields can be given access specifiers.
Class variables are members of Class, and local variables are members of method.
Fields must declare a default value for local variables.
Example:
Topic 2. Access Specifiers
Access specifiers (also called access modifiers), as the name suggests, access modifiers in Java to help restrict the scope of a class, constructor, variable, method, and data members.
public – visible/access to all other classes or everywhere. e.g. public class BankAccount
private – visible/access only to the current class, its methods, and every instance (object) of its class. e.g. child class cannot refer to its parent's private members - private String myID;
default (no specifier) – the class or member is accessible to the class itself and to other classes defined within the same package.
protected – visible to the current class, and all of its child classes. e.g. protected int myWidth; child classes are classes that inherit from (extend) this class.
Topic 3. Where do Objects Come From?
An Object is the instance of the class, which helps programmers use variables and methods from inside the class. However, we can create multiple instances of the Object for the same class.
The class has to be declared only once, but Objects of that class can be declared several times, depending on the requirement.
Objects act as a variable of the class.
A class does not take any memory spaces when a programmer creates one. An object takes memory when a programmer creates an Object of that class.
Instantiation: Instantiate == create an instance == create an object of a class.
In Java, the new keyword is used to create new Objects.
Declaration − A variable declaration with a variable name with an object type.
Instantiation − The 'new' keyword is used to create the object.
Initialization − The 'new' keyword is followed by a call to a constructor.
Creating an Object for Statistics Class
Creating an object of Statistics class.
Caller Class (main class) Instantiate of Statistics Class
The new operator does three things:
Allocates memory on the Heap memory to accommodate all of the Statistics object’s fields.
Invokes a constructor so that the object can initialize itself.
Returns a reference to the new object that we can store in a local variable.
Topic 4a. Class Members - Constructors
Constructors are special methods that are invoked only when an object is created using the new operator.
Constructors are similar to methods, except:
They have NO return type (not even void).
They always have the same name as the class.
They CANNOT be directly invoked, except from another constructor.
If a class does not declare any constructors, the compiler will automatically generate a default constructor – a public constructor that accepts no arguments.
A class can declare multiple constructors as long as each constructor has a unique parameter list. This is called constructor overloading.
Best practice: Classes should have at least two constructors; one is without argument, and the other is with argument.
Example - Constructors
Constructor for our Statistics class:
This constructor is public – it is accessible everywhere.
The constructor has no return type.
The constructor has the same name as the class.
This constructor takes a single argument of type double [].
Within the constructor, we initialize all of our fields.
When the constructor finishes, our object has meaningful state.
Topic 4b. Constructor Overloading
A class can define multiple constructors with different parameter lists.
Each constructor must have a unique parameter list.
Constructor overloading is a technique in Java that allows a class to have more than one constructor that share the same name.
Constructor overloading is similar to method overloading.
Learning Objectives:
By the end of this section, learners should be able to:
Describe Methods.
Describe Method Attributes.
Explain Methods Overloading
Explain Return Types of Methods.
Section 2: Overview of Methods in Java
Table of Contents
Topic 5: Overview of Java Method.
Method Reusability.
Semantic Code Organization.
Avoiding Code Repetition.
Method Declarations.
Method Access Specifier.
Method Return Types.
Void Return Type.
Method Parameter Lists.
Method Overloading.
Identify Correct Overloads.
Use Informative Parameter Names.
What Makes Parameter Lists Unique?
Why Return Type Does Not Matter.
The Entry Point and the Call Chain.
Static Code Analysis
Topic 5. Overview of Java Method
A method is a named block of code.
A method can be called (invoked) by name from other methods.
A method can define zero or more parameters, allowing it to be called with different options.
A method can optionally return a value.
Methods are also called subroutines, subprograms, or functions.
A function is sometimes reserved for those methods that return a value.
Methods can help organize code to make it more readable and reusable.
Each method has a unique signature based on its name and parameters.
A method’s return type is not part of its signature.
Remember: Methods are always a member of a class.
Method Reusability
Suppose you have an application that defines a spreadsheet document, and you want to autosize a column so that it is wide enough to visually accommodate all of its content over every row. This involves examining each row of the column, assessing the width needed for that cell’s content, and then keeping the maximum value found to use as the final column width.
That is a lot of code! And once you have written the code, it is not something that you want copied/pasted all over your application. It is ideal for placing in a method named autoFitColumn. The method could take just one parameter – the integer index of the column to auto-fit. Now, you can re-use it whenever you need it:
Semantic Code Organization
Methods allow us to organize code into semantically meaningful units.
This example is gaming code implemented without methods:
This example is gaming code implemented with methods:
Avoiding Code Repetition
The repetitive code of the snippet on the left can be replaced by the method-based code on the right.
In these examples, it is not necessarily less code, but it is definitely more versatile and reusable.
Method Declarations
Here is the declaration of the method we created earlier:
The method body is 0 or more statements contained within the curly braces following the declaration.
Examples - Method Declaration
Examples of spreadsheet class method declarations:
public boolean addWorksheet(String worksheetName);
public void autoFitRow(int startRowIndex, int endRowIndex);
public LocalDateTime getCellValueAsDateTime(int rowIndex, int columnIndex);
public double getCellValueAsDouble(String cellReference);
public double getColumnWidth(int columnIndex);
public List<String> getSheetNames();
public boolean isColumnHidden(String columnName);
Method Access Specifiers
A method’s access specifier (also called access modifier) determines its visibility to consumers of a class.
The access rules are the same as the rules for other class members:
public
means that the method can be invoked by any consumer of the class.private
means that the method can be invoked only from within the class definition.protected
means that the method can be invoked from within the class or from within any subclass of the class.(default)
means that the method can be invoked from within the class or by any other class within the same package that contains this class.
Method Return Types
Challenge: Which of these method declarations is valid?
All methods declare a return type.
If a method declares a non-void return type, it must return a value of that data type.
A method can also return a value that can be automatically converted to the declared return type.
Void Return Type
If a method does not need to return a value, it declares a return type void.
If a method performs a task but it does not give anything back, it is said to be void of a return value.
Returning anything from a method with a void return type leads to a compile error.
Method Parameter Lists
A method’s parameters are enclosed in parentheses.
Methods can declare 0 or more parameters:
0 parameters:
public void doSomethingSpecial() { ... }
1 parameter:
public void doSomethingSpecial(int count) { ... }
2 parameters:
public void doSomethingSpecial(int count, double value) { ... }
3 parameters:
public void doSomethingSpecial(int count, double value, int options) { ... }
Each of these methods has the same name and return type (void).
Each of these methods has a unique parameter list.
When methods have the same name, they must have unique parameters.
This technique is called overloading.
The return-type is irrelevant.
Method Overloading
A class can define multiple methods with the same name.
Each method with the same name must have a unique parameter list.
This is a valid method overload:
This is an example of invalid method overloading:
To the compiler, these two methods are identical:
The names of the parameters do not matter.
The compiler only sees the parameter types and their order.
Identify Correct Overloads
Which of these methods is unique from the perspective of the compiler? Assume the first-declared method is correct.
Identify Correct Overloads (continued)
Which of these overloads is correct?
Although these overloads are valid, they are not well-named.
When overloading methods, use parameter names that clearly indicate what the overload does compared to the other overloads.
Use Informative Parameter Names
These overloads use better parameter names:
This same advice applies even when you are not overloading a method!
What Makes Parameter Lists Unique?
When the number of parameters is different:
method()
is different frommethod(int count)
When the number of parameters is the same but the parameter types are different:
method(int value)
is different frommethod(double value)
When the number of parameters is the same and the types are the same, but the types are in a different order:
method(int count, double value)
is different frommethod(double value, int count)
When parameter names do not matter (for example, these parameter lists are identical:
method(int n, double v)
andmethod(int count, double value)
Why Return Type Does Not Matter
A method’s return type is not part of its signature/arguments.
Consider the below example. Within a single class, these methods are NOT unique:
A method’s signature is composed only of its name and its parameters.
The validity of overloading is based only on the signature.
Why Return Type Does Not Matter (continued)
A method’s return type is not part of its signature because the caller of a method is not obligated to use the return value.
Given these methods defined in one class:
How does the compiler choose which method to call here?
The Entry Point and the Call Chain
The statements within a method do not execute unless the method is explicitly invoked.
In any program, the process starts when the main method is invoked by the Java Virtual Machine (JVM).
The main method is the JVM’s entry point.
The main method can invoke other methods, and those methods can invoke other methods, forming a call chain.
Declaring and implementing a method does not guarantee the method will run.
The method must be called from somewhere by another method that is called from somewhere, etc.
Methods that are never called from anywhere are called dead codes.
Static Code Analysis
Suppose you created an application that grows trees.
You have experimented repeatedly with the algorithm that figures out where to best plant the trees.
You have moved on from the lines of inquiry of the less-successful of your experiments:
Yet, the code is still a formal part of your application:
It cannot be called because it is not within any possible call-chain.
It is never invoked from anywhere.
Static code analyzers can discover un-callable code (dead code).
Eclipse will (by its default settings) help identify methods that are outside of the application’s call chain.
Summary
Java is an OOP language. Everything in Java is associated with classes and objects, along with its attributes and methods. For example, a car is an object, which has attributes such as weight and color, and use methods such as drive and brake.
A class is like an object constructor — a blueprint for creating objects:
Modifiers - can be public or have default access.
Class keyword - used to create a class.
Class name - begins with an initial letter (capitalized by convention).
Body - class body surrounded by braces { }.
A constructor in Java is similar to a method that is invoked when an object of the class is created. a constructor has the same name as that of the class and does not have any return type.
Last updated