Lesson 3Object-Oriented Programming with Java
Starting with this lesson, you study various elements of the Java language with brief descriptions to get you started with programming in the shortest possible time. But you are certainly encouraged to refer to the extensive Java SE documentation that’s available online at http://docs.oracle.com/javase/8/
.
Classes and Objects
Java is an object-oriented language, which means that it has constructs to represent objects from the real world. Each Java program has at least one class that knows how to do certain things or how to represent some type of object. For example, the simplest class, HelloWorld
, knows how to greet the world.
Classes in Java may have methods and fields (also known as attributes). Methods represent actions or functions that a class can perform. Up until Java 8, every function had to be represented as a method of some class. Lambda expressions (see working_with_streams) give more freedom to functions, but for now the focus is on the Java foundation — classes, methods, and fields.
Let’s create and discuss a class named Car
. This class will have methods, describing what this type of vehicle can do, such as start the engine, shut it down, accelerate, brake, lock the doors, and so on.
This class will also have some fields: body color, number of doors, sticker price, and so on.
Get Java Programming 24-Hour Trainer, 2nd Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.