Java is an object-oriented programming (OOP) language, which means that we can design classes, objects, and methods that can perform certain actions. These behaviors are important in the construction of larger, more powerful Java programs.
Classes
Syntax
One fundamental concept of object-oriented programming in Java is the class.
A class is a set of instructions that describe how a data structure should behave.
Java provides us with its own set of pre-defined classes, but we are also free to create our own custom classes.
Classes in Java are created as follows:
|
|
Constructors
Let’s start by creating the starting state of our class. We can do this by adding a class constructor to it.
- A class constructor will allow us to create
Dog
instances. With a class constructor, we can set some information about theDog
. - If we do not create a class constructor, Java provides one that does not allow you to set initial information.
The code below demonstrates how a class constructor is created:
|
|
Instance Variables
When we create a new class, we probably have specific details that we want the class to include. We save those specific details into instance variables.
Here is an instance variable in the Car
class that describes a detail that we might want to associate with a car:
|
|
Constructor Parameters
Perfect! By adding a class constructor and creating instance variables, we will soon be able to use the Dog
class. However, the class constructor Dog
is still empty. Let’s modify this by adding parameters to the Dog
constructor.
You can think of parameters like options at an ice cream store. You can choose to order a traditional ice cream cone, but other times you may want to specify the size of the cone or the flavor of the ice cream.
For the Dog
class, we can specify the initial dog age by adding parameters to the class constructor.
- Parameters allow data types to be created with specified attributes.
Let’s add parameters to our Car
class constructor:
|
|
The Main Method
This is Java’s built-in main
method. We will learn more about methods and keywords around the main
method later on, but first let’s understand what the purpose of main
is.
- When Java runs your program, the code inside of the
main
method is executed.
For now, you can ignore the keywords in the main
method that we have not yet covered. You will learn about them later in the course.
Objects
To use the Dog
class, we must create an instance of the Dog
class. An instance of a class is known as an object in Java.
The example below demonstrates how to create a Car
object:
|
|
In the example above, we create a Car
object named myFastCar
. When creating myFastCar
, we used the class constructor and specified a value for the required int
parameter year
.
2007
is the model year of myFastCar
. Note that we declared the new object inside the main
method.
Method
A method is a pre-defined set of instructions. Methods are declared within a class. Java provides some pre-defined methods available to all classes, but we can create our own as well.
Let’s create a new method:
|
|
Here is an example of calling a method on an object using the Car
class:
|
|
Inheritance
One of the object-oriented programming concepts that allows us to reuse and maintain code more efficiently is called inheritance. It is used to share or inherit behavior from another class. Let’s look at an example:
|
|