Java Lesson 2 - PowerPoint PPT Presentation

1 / 25
About This Presentation
Title:

Java Lesson 2

Description:

For example, mountain bikes, racing bikes, and tandems are all kinds of bicycles. ... and two sets of handle bars; some mountain bikes have an extra set of gears with ... – PowerPoint PPT presentation

Number of Views:88
Avg rating:3.0/5.0
Slides: 26
Provided by: yedi
Category:
Tags: biking | java | lesson | mountain

less

Transcript and Presenter's Notes

Title: Java Lesson 2


1
Java Lesson 2
  • Object Oriented Programming Concepts

2
OO Programming Concepts
  • What is an Object?
  • What is a Message?
  • What is a Class?
  • What is Inheritance?
  • What is an Interface?
  • A Closer Look at Hello World Application

3
What Is an Object?
  • Definition An object is a software bundle of
    variables and related methods.
  • Examples of objects dogs, chairs, tables, trees,
    bicycles, etc.
  • For example
  • Dogs have state (name, color, breed, hungry) and
    behavior (barking, fetching, and wagging tail).
  • Bicycles have state (current gear, current pedal
    cadence, two wheels, number of gears) and
    behavior (braking, accelerating, slowing down,
    changing gears).

4
What Is an Object?
  • Software objects have state and behavior.
  • A software object maintains its state in one or
    more variables. A variable is an item of data
    named by an identifier.
  • A software object implements its behavior with
    methods. A method is a function (subroutine)
    associated with an object.
  • You can represent real-world objects by using
    software objects.

5
What Is an Object?
  • Bicycle Model

6
What Is an Object?
  • Advantages
  • Modularity The source code for an object can be
    written and maintained independently of the
    source code for other objects
  • Information hiding An object has a public
    interface that other objects can use to
    communicate with it.

7
What is a message?
  • An object usually appears as a component of a
    larger program or application that contains many
    other objects.
  • Software objects interact and communicate with
    each other by sending messages to each other.
  • When object A wants object B to perform one of
    B's methods, object A sends a message to object B

8
What is a message?
  • The object to which the message is addressed
    (YourBicycle)
  • The name of the method to perform (changeGears)
  • Any parameters needed by the method (lowerGear)

9
What is a message?
  • Messages provide two important benefits.
  • An object's behavior is expressed through its
    methods, so (aside from direct variable access)
    message passing supports all possible
    interactions between objects.
  • Objects don't need to be in the same process or
    even on the same machine to send and receive
    messages back and forth to each other.

10
What Is a Class?
  • Definition A class is a blueprint, or prototype,
    that defines the variables and the methods common
    to all objects of a certain kind.
  • Bicycles have some state (current gear, current
    cadence, two wheels) and behavior (change gears,
    brake) in common.

11
What Is a Class?
  • After you created the bicycle class, you can
    create any number of instances from that class.

12
What Is a Class?
  • Class Instance Variables
  • Instance variables are in contrast to class
    variables (which are declared using the static
    modifier). The runtime system allocates class
    variables once per class regardless of the number
    of instances created of that class. The system
    allocates memory for class variables the first
    time it encounters the class. A class variable
    contains information that is shared by all
    instances of the class.
  • All instances share a class variable. If one of
    them changes the value of a class variable then
    all other instances access this changed value.
  • All instances share the same copy of the class's
    class variables. You can access class variables
    through an instance or through the class itself.

13
What Is a Class?
  • Bicycle class and its instance

14
What Is Inheritance?
  • Object-oriented systems allow classes to be
    defined in terms of other classes.
  • For example, mountain bikes, racing bikes, and
    tandems are all kinds of bicycles.
  • In object-oriented terminology, mountain bikes,
    racing bikes, and tandems are all subclasses of
    the bicycle class.
  • Similarly, the bicycle class is the superclass of
    mountain bikes, racing bikes, and tandems.

15
What Is Inheritance?
16
What Is Inheritance?
  • Each subclass inherits state (in the form of
    variable declarations) from the superclass.
    Mountain bikes, racing bikes, and tandems share
    some states cadence, speed, and the like.
  • Also, each subclass inherits methods from the
    superclass. Mountain bikes, racing bikes, and
    tandems share some behaviors braking and
    changing pedaling speed, for example.

17
What Is Inheritance?
  • Subclasses can add variables and methods to the
    ones they inherit from the superclass.
  • Tandem bicycles have two seats and two sets of
    handle bars some mountain bikes have an extra
    set of gears with a lower gear ratio.
  • Subclasses can also override inherited methods
    and provide specialized implementations for those
    methods.
  • You are not limited to just one layer of
    inheritance. The inheritance tree, or class
    hierarchy, can be as deep as needed.

18
What Is Inheritance?
  • Inheritance offers the following benefits
  • Subclasses provide specialized behaviors from the
    basis of common elements provided by the
    superclass. Through the use of inheritance,
    programmers can reuse the code in the superclass
    many times.
  • Programmers can implement superclasses called
    abstract classes that define "generic" behaviors.
    The abstract superclass defines and may partially
    implement the behavior, but much of the class is
    undefined and unimplemented. Other programmers
    fill in the details with specialized subclasses.

19
What is an Interface?
  • An interface is a device that unrelated objects
    use to interact with each other
  • Interfaces are useful for the following
  • Capturing similarities among unrelated classes
    without artificially forcing a class
    relationship.
  • Declaring methods that one or more classes are
    expected to implement.
  • Revealing an object's programming interface
    without revealing its class.

20
A Closer Look At Hello World
  • /
  • The HelloWorldApp class implements an
    application that
  • simply displays "Hello World!" to the standard
    output.
  • /
  • class HelloWorldApp
  • public static void main(String args)
  • System.out.println("Hello World!")
    //Display the string.

21
Comments
  • The Java language supports three kinds of
    comments
  • / text /
  • The compiler ignores everything from / to /.
  • / documentation /
  • This indicates a documentation comment (doc
    comment, for short). The compiler ignores this
    kind of comment, just like it ignores comments
    that use / and /. The JDK javadoc tool uses doc
    comments when preparing automatically generated
    documentation. For more information on javadoc,
    see the Java tool documentation .
  • // text
  • The compiler ignores everything from // to the
    end of the line.

22
Classes
  • In the Java language, each method (function) and
    variable exists within a class or an object (an
    instance of a class).
  • The Java language does not support global
    functions or variables.
  • Thus, the skeleton of any Java program is a class
    definition.

23
The Main Method
  • The entry point of every Java application is its
    main method.
  • Every Java application must contain a main method
    whose signature looks like this
  • public static void main(String args)
  • The method signature for the main method contains
    three modifiers
  • public indicates that the main method can be
    called by any object.
  • static indicates that the main method is a class
    method.
  • void indicates that the main method doesn't
    return any value.

24
Using Classes and Objects
  • The other components of a Java application are
    the supporting objects, classes, methods, and
    Java language statements that you write to
    implement the application
  • System.out.println("Hello World!")

25
Rectangle Example
  • http//cse.yeditepe.edu.tr/taytekin/cse252/lab/Re
    ctangleApp1.java
  • http//cse.yeditepe.edu.tr/taytekin/cse252/lab/Re
    ctangleApp2.java
Write a Comment
User Comments (0)
About PowerShow.com