Java Classes and Encapsulation

¡Supera tus tareas y exámenes ahora con Quizwiz!

A method consists of several parts:

An access modifier A return type A method name A parameter list An exception list (Java only) A method body

Overloading methods and constructors

To provide users of your class with different options when they instantiate new objects or invoke new methods, you can create an overloaded method or overloaded constructor. As a programmer, when you overload a method or constructor, you add flexibility to your class so that users can leverage different inputs when interacting with it.

Coding convention for Constants

Constants in Java are typically written in all uppercase with underscores _ in between words.

Loosely coupled code

Coupling is a measure of how closely connected two pieces of software are and how much they depend on each other. If you write your code in a way where changing one thing, like replacing a class or method with another, causes a ripple effect of changes that need to be made in other parts of your code, you've written tightly coupled code.

Encapsulation with methods

Using methods allows you to encapsulate the logic needed to make your clock function. By encapsulating the internal mechanisms of the class, the user of your class has to interact with it in a predictable way.

Each class has Variables and properties

Variables and properties represent an object's characteristics, or state, and methods define an object's behaviors.

Java Methods

When you need an object to "do something" or perform a behavior, you can use methods.

Method return types

You may have noticed in the above example, the method was written as public void tick(). The void part of that means the method doesn't return anything. If you want your method to return something, you can specify the data type of what you want to be returned from a method. For example, if you want to return an int from a method,

Constants

You were introduced to constants in an earlier chapter, using the final keyword. You should know that constants are often declared to be static as well. This is because when you declare a property to be static, it's only allocated memory once, even if it's part of a non-static class that has been instantiated more than once. This is a memory saving technique built into the Java language.

In Java, properties are also commonly called

instance variables, or sometimes, attributes. This chapter frequently uses instance variable interchangeably with property when discussing Java.

private access modifier

private The private access modifier restricts access to a property or method. Private variables and methods are only visible from within the class that it's declared in.

public access modifier

public The modifier keyword public marks something as publicly accessible, meaning it can be used or invoked by other code.

Access modifiers and Encapsulation

Encapsulation Access modifiers are another key feature of encapsulation. This lets you restrict access to the internal mechanisms of how the class works by hiding or protecting its variables. This way, the values remain consistent and can only be changed through methods that the class allows external resources to have access to. In this way, a class controls how it's used and makes sure that use is consistent with its purpose.

Encapsulation is

Encapsulation is defined as the wrapping up of data under a single unit. It is the mechanism that binds together code and the data it manipulates. Another way to think about encapsulation is, it is a protective shield that prevents the data from being accessed by the code outside this shield.

Method name

Every method has a name, followed by (). By convention, method names are camelCase.

When using Getters and Setters, keep the following in mind:

Getters and Setters follow the standard Java camelCase naming convention. Getters never have a parameter and always return the same data type as the instance variable that it's getting. Setters always have a single parameter of the same data type as the instance variable being set and return void.

In Java, all instance variables are accessed through methods called

Getters and Setters. These are named based on the instance variable they're for with either the word get or set before them.

The static modifier means

In Java, the static modifier means something is directly related to a class: if a field is static, then it belongs to the class; if a method is static, then it belongs to the class. As a result, you can use the class's name to call a static method or reference a static field.

What Is a Class? real world example

In the real world, you'll often find many individual objects all of the same kind. There may be thousands of other bicycles in existence, all of the same make and model. Each bicycle was built from the same set of blueprints and therefore contains the same components. In object-oriented terms, we say that your bicycle is an instance of the class of objects known as bicycles.

There are some rules for method names in Java:

Method names are restricted to characters [A-Z], [a-z], [0-9], $, and _. Method names can't start with number. Method names must not be a Java keyword.

Parameter list

Methods commonly accept values, or arguments passed into them through parameters. You can think of a parameter list as a comma-delimited list of variables between the () following the method name. Remember all variables have a data type and a name, and the same can be said for parameters.

There are three rules of an overloaded method:

Overloaded methods must have the same name. Overloaded methods must differ in the number of parameters, parameter types, or both. Overloaded methods can have different return types, but that must not be the only difference

private access modifier

Private: The private access modifier is specified using the keyword private. The methods or data members declared as private are accessible only within the class in which they are declared.Any other class of the same package will not be able to access these members.Top-level classes or interfaces can not be declared as private becauseprivate means "only visible within the enclosing class".protected means "only visible within the enclosing class and any subclasses"

Derived properties

Sometimes, you want properties to represent an object's state, but you need it to be dependent on other properties. For example, a full name of a person is derived or generated using the combination "First Last."

Can we override static method?

Static methods are bonded at compile time using static binding. Therefore, we cannot override static methods in Java.

A constructor is

A constructor is a special method of a class that initializes an object of that class. Constructors have the same name as the class they're in, and because they always return an instance of that class, a return type isn't needed.

A class is

The blueprint from which individual objects are created.

Encapsulation is

The bundling of an object's state and behaviors together is part of the definition of encapsulation, the first pillar of object-oriented programming.

Static methods

The same rules apply: the static method belongs to the class itself and not an instance of it, and you call it by using the class name, not the instance.

A note about "this"

The this keyword is a special variable that refers to the current instance of an object. Using this to refer to a variable, such as this.hour, is how the code knows to retrieve the hour property of this specific instance.

What are Access modifiers?

There are four types of access modifiers available in java: Default - No keyword required Private Protected Public

default access modifier

Default: When no access modifier is specified for a class, method, or data member - It is said to be having the default access modifier by default. The data members, class or methods which are not declared using any access modifiers i.e. having default access modifier are accessible only within the same package.


Conjuntos de estudio relacionados

FD - Diesel Generator Engine Fuel Oil System

View Set

Gospel of Mark 10-12 - Bible Experience 2012

View Set

CRQ 7) In 1798 Thomas Malthus published An Essay on the Principle of Population in which he argued that population growth will inevitably outpace food production, resulting in widespread famine.

View Set

ATI fundamentals Practice Test A

View Set

Chapter 8: Reporting and Interpreting Property, Plant, and Equipment; Natural Resources; and Intangibles

View Set

Health Assessment Exam 3 Prep U Questions

View Set

Nurs. 107 Chapter 29: Management of Patients with Nonmalignant Hematologic Disorders Prep-U & Nclex questions

View Set

Quantitive Reasoning, Algebra, and Statistics

View Set

Self Test: Musculoskeletal System

View Set

W -liverChapter 53 **Care for Patients With Liver Problems

View Set

Cellular respiration, Cellular Respiration

View Set