CSC 313 Java Objects in Java - PowerPoint PPT Presentation

1 / 21
About This Presentation
Title:

CSC 313 Java Objects in Java

Description:

Objects are an integral part of the Java language. Many of Java's build-in collection classes store only data ... Java programs are implicitly object-oriented ... – PowerPoint PPT presentation

Number of Views:33
Avg rating:3.0/5.0
Slides: 22
Provided by: gar59
Category:
Tags: csc | java | javas | objects

less

Transcript and Presenter's Notes

Title: CSC 313 Java Objects in Java


1
CSC 313 JavaObjects in Java
2
Java Data Types
  • Java has two kinds of types
  • fundamental, primitive or simple
  • derived or composite
  • Derived or composite types are represented as
    Object References.
  • An object is a class instance or an array. The
    reference values (often just references) are
    pointers to these objects, and a special null
    reference, which refers to no object.

3
Java Program Structure
  • A Java program always consists of a number of
    classes. There is at least one class in every
    program, and there can be many.
  • Classes specify the objects you use in
    object-oriented programming.
  • You must give each file the same name as that of
    the class that is defined within it.

4
Classes Encapsulation
  • The class as a data type can encapsulate
  • variables or fields, which represent features or
    properties that class instances share
  • procedures
  • Procedures used to create or construct instances
    of a class are known as constructors.
  • Procedures that represent class operations or
    behaviors are know as methods.

5
Object-oriented Programming
  • In Java almost everything is an object. So What
    are Objects?
  • Anything can be thought of as an object. Objects
    are all around you. You can consider Tree to be a
    particular class of objects, and then the tree in
    my yard which I call myOak, the tree in your yard
    which I call thatDarnedTree, and a
    generalSherman, the well-known redwood, could be
    instances of Tree. In other words, they are
    objects of the class Tree.

6
What are Objects?
7
Define a Class of Objects - Hat
8
Operating on Objects
9
Complete Class Definition
10
Encapsulation (1)
  • Encapsulation refers to hiding items of data and
    methods within an object by specifying them as
    private in the definition of the class. In the
    Hat class we defined earlier, the instance
    variables, name, size and hatOn were
    encapsulated.
  • They were only accessible through the methods
    defined for the class.

11
Encapsulation (2)
  • Being able to encapsulate members of a class in
    this way is important for the security and
    integrity of class objects.
  • Another major advantage of encapsulation-the
    ability to hide the implementation of a class. A
    particular object, for example a cowboyHat, will
    incorporate, or encapsulate, the name, the size
    of the object, and the status of the hat in the
    instance variable hatOn. Only the constructor,
    and the putHatOn(), takeHatOff() and getSize()
    methods can be accessed externally.

12
Wrapper Classes for Primitive Types
  • Objects are an integral part of the Java
    language.
  • Many of Javas build-in collection classes
    store only data of type object.
  • To allow these collection classes (vector, stack,
    linked list etc.) to store primitive data, Java
    provides wrapper classes for converting primitive
    data to Object type.
  • These wrapper classes for the primitive data are
    displayed in the following slide.

13
Wrapper Classes for Primitive Types
14
Wrapper Class Methods
  • All of these wrapper classes directly or
    indirectly inherit from Object.
  • Therefore standard methods are available such
    as
  • toString
  • Clone
  • equals

15
Constants Utility Methods
  • The Java platform includes constants and utility
    methods related to the primitive types in wrapper
    classes. For example
  • Integer.parseInt (String)
  • Double.parseDouble(String)
  • Character.isDigit
  • Character.isSpace

16
Object-oriented programming
  • Inheritance
  • Software reusability
  • Classes are created from existing ones
  • Absorbing attributes and behaviors
  • Adding new capabilities
  • Convertible inherits from Automobile
  • Polymorphism
  • Enables developers to write programs in general
    fashion
  • Handle variety of existing and yet-to-be-specified
    classes
  • Helps add new capabilities to system

17
Object-oriented programming
  • Inheritance
  • Subclass inherits from superclass
  • Subclass usually adds instance variables and
    methods
  • Single vs. multiple inheritance
  • Java does not support multiple inheritance
  • Interfaces (discussed later) achieve the same
    effect
  • Is a relationship
  • Composition
  • Has a relationship

18
Composition vs. Inheritance
  • Inheritance
  • Is a relationship
  • Teacher is an Employee
  • Composition
  • Has a relationship
  • Employee has a TelephoneNumber

19
Superclasses and Subclasses
  • Is a Relationship
  • Object is an object of another class
  • Rectangle is a quadrilateral
  • Class Rectangle inherits from class Quadrilateral
  • Form tree-like hierarchical structures

20
Summary
  • Java programs are implicitly object-oriented
  • A java program always consists of a number of
    classes. There is at least one class in every
    program, and there may be many.
  • The class as a data type can encapsulate
  • variables or fields, which represent features or
    properties that class instances share
  • Procedures
  • Wrapper classes are provided by Java for
    converting primitive data type Object.

21
Thats All For Today Folks!
Write a Comment
User Comments (0)
About PowerShow.com