Skip to content

Latest commit

 

History

History
216 lines (145 loc) · 7.72 KB

File metadata and controls

216 lines (145 loc) · 7.72 KB

OOP

Java OO

  • What Is an Object?

    Objects are key to understanding object-oriented technology. Look around right now and you'll find many examples of real-world objects: your dog, your desk, your television set, your bicycle.

    Real-world objects share two characteristics: They all have state and behavior. Dogs have state (name, color, breed, hungry) and behavior (barking, fetching, wagging tail).

    Software objects are conceptually similar to real-world objects: they too consist of state and related behavior. An object stores its state in fields (variables in some programming languages) and exposes its behavior through methods ( functions in some programming languages). Methods operate on an object's internal state and serve as the primary mechanism for object-to-object communication. Hiding internal state and requiring all interaction to be performed through an object's methods is known as data encapsulation — a fundamental principle of object-oriented programming.

  • What Is a Class?

    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. A class is the blueprint from which individual objects are created.

    class Bicycle {
    
        int cadence = 0;
        int speed = 0;
        int gear = 1;
    
        void changeCadence(int newValue) {
             cadence = newValue;
        }
    
        void changeGear(int newValue) {
             gear = newValue;
        }
    
        void speedUp(int increment) {
             speed = speed + increment;   
        }
    
        void applyBrakes(int decrement) {
             speed = speed - decrement;
        }
    
        void printStates() {
             System.out.println("cadence:" +
                 cadence + " speed:" + 
                 speed + " gear:" + gear);
        }
    }

Java Classes

  • Declaring Classes

    class MyClass {
      // field, constructor, and 
      // method declarations
    }

    This is a class declaration. The class body (the area between the braces) contains all the code that provides for the life cycle of the objects created from the class: constructors for initializing new objects, declarations for the fields that provide the state of the class and its objects, and methods to implement the behavior of the class and its objects.

  • Declaring Member Variables

    There are several kinds of variables:

    • Member variables in a class—these are called fields.
    • Variables in a method or block of code—these are called local variables.
    • Variables in method declarations—these are called parameters.

    The Bicycle class uses the following lines of code to define its fields:

      public int cadence;
      public int gear;
      public int speed;

    Field declarations are composed of three components, in order:

    1. Zero or more modifiers, such as public or private.
    2. The field's type.
    3. The field's name.
  • Access Modifiers

    modifier used lets you control what other classes have access to a member field

    • public modifier—the field is accessible from all classes.
    • private modifier—the field is accessible only within its own class.
  • Types

    All variables must have a type. You can use primitive types such as int, float, boolean, etc. Or you can use reference types, such as strings, arrays, or objects.

  • Variable Names

    All variables, whether they are fields, local variables, or parameters, follow the same naming rules and conventions that were covered in the Language Basics lesson

    • the first letter of a class name should be capitalized, and
    • the first (or only) word in a method name should be a verb.
  • Defining Methods

    Here is an example of a typical method declaration:

      public double calculateAnswer(double wingSpan,int numberOfEngines,
            double length,double grossTons){
            //do the calculation here
            }

    The only required elements of a method declaration are the method's return type, name, a pair of parentheses, (), and a body between braces, {}.

    More generally, method declarations have six components, in order:

    1. Modifiers—such as public, private.
    2. The return type—the data type of the value returned by the method, or void if the method does not return a value.
    3. The method name—the rules for field names apply to method names as well, but the convention is a little different.
    4. The parameter list in parenthesis—a comma-delimited list of input parameters, preceded by their data types, enclosed by parentheses, (). If there are no parameters, you must use empty parentheses.
    5. An exception list.
    6. The method body, enclosed between braces—the method's code, including the declaration of local variables, goes here.
  • Naming a Method

    Although a method name can be any legal identifier, code conventions restrict method names. By convention, method names should be a verb in lowercase or a multi-word name that begins with a verb in lowercase, followed by adjectives, nouns, etc. In multi-word names, the first letter of each of the second and following words should be capitalized. Here are some examples:

    • run
    • runFast
    • getBackground
  • Overloading Methods

    Typically, a method has a unique name within its class. However, a method might have the same name as other methods due to method overloading.

    The Java programming language supports overloading methods, and Java can distinguish between methods with different method signatures. This means that methods within a class can have the same name if they have different parameter lists

  • Providing Constructors for Your Classes

    A class contains constructors that are invoked to create objects from the class blueprint. Constructor declarations look like method declarations—except that they use the name of the class and have no return type.

    a constructor is called by the new operator.

    new Bicycle(30, 0, 8) creates space in memory for the object and initializes its fields.

    public Bicycle() {
    gear = 1;
    cadence = 10;
    speed = 0;
    }

Binary, Decimal and Hexadecimal Numbers

  • Decimal

    Every digit in a decimal number has a "position", and the decimal point helps us to know which position is which:

  • Bases

    The Decimal Number System is also called "Base 10", because it is based on the number 10, with these 10 symbols:

    0, 1, 2, 3, 4, 5, 6, 7, 8 and 9

  • Counting with Different Number Systems

    But you don't have to use 10 as a "Base". You could use 2 ("Binary"), 16 ("Hexadecimal"), or any number you want to!

    So the general rule is:

    • Count up until just before the "Base Number", then start at 0 again, but first you add 1 to the number on your left.
  • Binary Numbers

    Binary Numbers are just "Base 2". So you start counting at 0, then 1, then you run out of digits ... so you start back at 0 again, but increase the number on the left by 1.

  • Hexadecimal Numbers

    Hexadecimal numbers are interesting. There are 16 of them!

    They look the same as the decimal numbers up to 9, but then there are the letters ("A',"B","C","D","E","F") in place of the decimal numbers 10 to 15.

    So a single Hexadecimal digit can show 16 different values: 0 - F