Tt
Click this widget to change the font size.
CC
Click this widget to change contrast.

Home Page 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | Links | Search | Bio |

Guide to Object-oriented Programming With Java (Buffalo State University Version)


Chapter 7: Some Basic Java Concepts

There are certain Java concepts that will be used throughout this guide, including object (constructor, instantiation), class, data attributes, methods, encapsulation, information hiding, Polymorphism, and inheritance.

Objects

An object is an instance of a class. There should be a constructor to create the object.

  public BMIcalculator(double w, double h) {
    weight = w;
    height = h;
  }

Figure 7-1: An Object Constructor
    BMIcalculator calculator = new BMIcalculator(60, 1.70);

Figure 7-2: Instantiating (Creating) an Object Using the new Keyword

Classes

An object is an instance of a class. There should be a constructor to create the object.

public class BMIcalculator {

  // declare variables
  double w;
  double h;
  double BMI;

  public BMIcalculator(double w, double h) {
    weight = w;
    height = h;
  }

  public calculateBMI() {
    return weight / (height * height);
  }

  /**
   * The main() is the entry point for a Java program when executed
   *
   * @param string array - command line arguments passed to program
   * @return none
   *
   */

  public static void main(String[] args) {
    BMIcalculator calculator = new BMIcalculator(60, 1.70);
    double bmi = calculator.calculateBMI();
    // print to the console
    System.out.println("Your BMI is " + bmi);
  }
}

Figure 7-3: The BMIcalculator Class

Data and Attributes

The data and attributes associated with an object are know as the object's properties.

Methods

Methods (functions) are the actions an object can perform.

Encapsulation

Encapsulation means binding an object's state (properties) and behavior (methods) together. When you create a class, you are encapsulation that object's data and methods.

Information Hiding

Information hiding means that the object's data and attributes are protected from change by other parts of the application.

Polymorphism

Polymorphism allows actions (methods) to act differently based on the object performing the action or the object the action is being performed on. For example, The sound that a cat makes is "meow". So, if an object had a method, makeSound() we could use that method on a "cat" object and it would return "meow". If we were to use this methiod on a "dog" object, it would return "bark". Let's pretend that all animals can makeSound(). So depending on the type of animal (object) the method does something different.

Inheritance

Inheritance allows for extending objects and code reuse. For example, if we had an object, "person" with properties (first name, last name, etc.) and methods (walk, talk, eat, etc.), we could "extend" the "person" object to a subclass such as a "student". The "student" object would "inherit" the parent object's properties and methods. The "student" object could then have it's own properties (classes, grades, etc.) and methods (enroll, graduate, etc.).

Help contribute to my OER Resources. Donate with PayPal button via my PayPal account.
Creative Commons License This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License. Copyright © 2016-2024 Jim Gerland