Unit 1 Intro to Object Oriented Programming PT1

Download Unit 1 Intro to Object Oriented Programming PT1

Post on 03-Apr-2018

215 views

Category:

Documents

0 download

Embed Size (px)

TRANSCRIPT

  • 7/29/2019 Unit 1 Intro to Object Oriented Programming PT1

    1/41

    Unit 1: Introduction to Object OrientedProgramming

    public class Car{private String name;private String model;private String color;private int year;

    }

  • 7/29/2019 Unit 1 Intro to Object Oriented Programming PT1

    2/41

    Objectives

    At the end of this unit students should be able to:

    PT1

    1. Outline the basic ideas behind object oriented programming

    2. State what is a class and how a class is related to an object

    3. Demonstrate the creation(definition) of a simple class structure (with variables and

    methods)

    4. Define and use instance variables, class variables, instance methods and class

    methods

    5. Define constructors for classes.

    6. Create objects of a class

    PT2

    7. Describe method and constructor overloading and construct classes that have

    overloaded methods and constructors

    8. Use the Java Class Library and existing classes

    9. Define, create and use java packages

    10. Differentiate between the various access modifiers and when it is appropriate touse them

  • 7/29/2019 Unit 1 Intro to Object Oriented Programming PT1

    3/41

    Object Oriented Program?

    An Object-Oriented Program consists of a group ofcooperating objects, exchanging messages, for the

    purpose of achieving a common objective.

  • 7/29/2019 Unit 1 Intro to Object Oriented Programming PT1

    4/41

    Objective:2

    State what is a class and how a class related to anobject

  • 7/29/2019 Unit 1 Intro to Object Oriented Programming PT1

    5/41

    Object

    An objectis a self-contained entity.

    It has its own private collection ofattributes/fields

    (ie. data or properties) and operations/methods (ie.

    behaviour ) thatencapsulate functionality into a

    reusable and dynamically loaded structure.

  • 7/29/2019 Unit 1 Intro to Object Oriented Programming PT1

    6/41

    Classes

    The basic element of object-oriented programmingis a class.

    A class is a software blueprint that can be used to

    instantiate or create many individual objects.

    A class: defines attributes /state which are variables which can

    store a value that represents a particular property defines the methods that manipulate the object or perform

    interaction between related objects and methods

  • 7/29/2019 Unit 1 Intro to Object Oriented Programming PT1

    7/41

    Objective: #3

    Demonstrate the creation(definition) of a simpleclass structure (with variables and methods)

  • 7/29/2019 Unit 1 Intro to Object Oriented Programming PT1

    8/41

    UML Notation for Classes

    Example

    Class Name

    Attribute

    Methods

  • 7/29/2019 Unit 1 Intro to Object Oriented Programming PT1

    9/41

    Classes contd.

    A Class defines a new Reference Data Type

    A class defines the shape and behaviour of an

    object and is a template for multiple objects with

    similar features.

    Once defined this new data type can be used to

    create objects of that type

    Thus, a class is a template for an object, and an

    object is an instance of a class.

    Example : Car audi = new Car();

  • 7/29/2019 Unit 1 Intro to Object Oriented Programming PT1

    10/41

    Class contd.

    When you declare a class, you declare its exact formand nature. You do this by specifying the data that it

    contains(fields/attributes) and the code that

    operates on that data(methods).

    While very simple classes may contain only code or

    only data, most real world classes contains both

  • 7/29/2019 Unit 1 Intro to Object Oriented Programming PT1

    11/41

    Syntax is: class {

    //***

    }

    Classes contd.

    The requirements for thecreation of a class include a

    source file with the ,

    class keyword, followed by a

    legal identifier

    and a pair of curly braces for

    the body.

  • 7/29/2019 Unit 1 Intro to Object Oriented Programming PT1

    12/41

    Code:

    public class Car{//private String model;private String make;private int year;

    private String color;

    public Car(){}

    public void setMake(){}

    public String getMake(){}

    }

    Classes contd.

  • 7/29/2019 Unit 1 Intro to Object Oriented Programming PT1

    13/41

    Objective:#4

    Define and use instance variables, class variables,instance methods and class methods

  • 7/29/2019 Unit 1 Intro to Object Oriented Programming PT1

    14/41

    ATTRIBUTES & METHODS

    The attributes (state, data or variables) definedwithin the class are sometimes referred to as

    instance variables.

    The code for the behaviour of the objects of the

    class are contained within the method

    Collectively the methods and variables within theclass are called members of the class.

  • 7/29/2019 Unit 1 Intro to Object Oriented Programming PT1

    15/41

    Declaring Attributes

    The elements of attribute declaration are:

    Syntax is:

    Example

    public int name;

  • 7/29/2019 Unit 1 Intro to Object Oriented Programming PT1

    16/41

    For example, if the Books name, the authors

    name, number of pages of a Book class are itsattributes. So the definition of the Book class wouldbe:

    public class Book{

    private String name;private String authorname;private int nopages;

    }

  • 7/29/2019 Unit 1 Intro to Object Oriented Programming PT1

    17/41

    Methods

    Class behaviour are represented in Java by methods

    Java methods are similar to functions or procedures

    in other programming languages

    Methods are a series of statements that perform

    some repeated task. Instead of writing 10 lines of

    code we can put those ten lines in a method and

    just call it in one line.

  • 7/29/2019 Unit 1 Intro to Object Oriented Programming PT1

    18/41

    Methods

    Modules in Java are called methods and classes.

    Java program are written by combining new

    methods and classes the programmers write with

    prepackaged methods and classes available in the

    Java API

    Classes generally consists of two things:

    Instance variables (Attributes)

    Methods Methods allow the programmmer to modularize

    program

  • 7/29/2019 Unit 1 Intro to Object Oriented Programming PT1

    19/41

    Declaring Methods

    The elements of method declaration are: :

    :

    :

    :

    Syntax is:

    (){//body of code

    }

  • 7/29/2019 Unit 1 Intro to Object Oriented Programming PT1

    20/41

    Methods

    return_typespecifies the type of data returned

    by the method. (this can be any valid type

    including class types that you create) If the

    method does not return a value, the type mustbe set to void

    public String getName(){

    return name;

    }

    public void setName(String name){

    this.name=name;

    }

  • 7/29/2019 Unit 1 Intro to Object Oriented Programming PT1

    21/41

    Methods that have a return type other than

    void, return a value to the calling routine using

    the following return statements:

    Here value is the value returned.

    Syntax is:return value;

  • 7/29/2019 Unit 1 Intro to Object Oriented Programming PT1

    22/41

    method_namespecifies the name given to themethod.(this can be any legal identifier other than

    those already used by other items within the

    current scope).

    public String getName(){

    return name;

    }

    public void setName(String name){

    this.name=name;

    }

  • 7/29/2019 Unit 1 Intro to Object Oriented Programming PT1

    23/41

    Parameters-this is a sequence of type and identifierpairs separated by commas.

    Parameters are essential variables that receive the

    value of arguments passed to the method when it is

    called.

    If the method has no parameters, then the parameter

    list will be empty.public String getName(){

    return name;}

    public void setName(String name){

    this.name=name;

    }

  • 7/29/2019 Unit 1 Intro to Object Oriented Programming PT1

    24/41

    local variables All variable declared in method definitions

    they are known only in the method in which they are

    defined.

    A methods parameters are also local variables

  • 7/29/2019 Unit 1 Intro to Object Oriented Programming PT1

    25/41

    Lets look at the example

    Book.java

    Book

    - name: String

    - author: String

    - nopages: int

    + Book ()

    + getName(): String

    + getAuthor(): String+ getNoPages():int

    + setName(String):void

    + setAuthor(String):void

    +setNoPages(int):void

  • 7/29/2019 Unit 1 Intro to Object Oriented Programming PT1

    26/41

    Objective:#5

    Define constructors for classes.

  • 7/29/2019 Unit 1 Intro to Object Oriented Programming PT1

    27/41

    Constructor

    A constructor initializes anobject immediately uponcreation.

    It has the same name as the

    class in which it resides andis syntactically similar to amethod.

    Once defined, it isautomatically calledimmediately after the objectis created, before the newoperator completes.

    Book

    - name: String

    - author: String

    - nopages: int

    + Book ()

    + getName(): String

    + getAuthor(): String

    + getNoPages():int

    + setName(String):void

    + setAuthor(String):void

    +setNoPages(int):void

  • 7/29/2019 Unit 1 Intro to Object Oriented Programming PT1

    28/41

    Constructor

    Constructors do not have a return type, not evenvoid

    It is the job of the constructor to initialize the

    internal state of an object, so that the code creating

    an instance will have fully initialized usable objectsimmediately

    Constructors like methods can receive parameters//Constructor1

    public Book(){name = "Java Programming";

    authorname=" ";

    nopages = 0;

    }

    //Constructor2

    public Book(String n, String an, String n, int p){name= n;

    authorname=an;

    nopages = p ;

    }

  • 7/29/2019 Unit 1 Intro to Object Oriented Programming PT1

    29/41

    Constructors need not be explicitly called becauseconstructors are called during the object creation and

    should be provided with the required parameters if any.

    Book java=new book(Patrick Norton);

    The above object creation(java) gives a call to theconstructor which takes string parameter.

    Whenever such definition of the constructor or list of

    constructor are made it is said that the constructors are

    overloaded. Will be discussed in details later

    Such cases one constructor among the list of constructors is

    called depending upon the object creation

  • 7/29/2019 Unit 1 Intro to Object Oriented Programming PT1

    30/41

    Lets look at the example Book.java

    BookApp.java

  • 7/29/2019 Unit 1 Intro to Object Oriented Programming PT1

    31/41

    Objective:6#

    Create objects of a class

  • 7/29/2019 Unit 1 Intro to Object Oriented Programming PT1

    32/41

    Declaring Objects

    When you create a class you are creating a newdata type.

    You can use this type to declare objects of that type.

  • 7/29/2019 Unit 1 Intro to Object Oriented Programming PT1

    33/41

    Declaring Objects

    Obtaining objects is a two step process.

    1. Declare the variable of the class type (does not

    define and object

    2. Acquire a physical copy of the object and assign itto that variable. (this is done using the new

    operator.

    Example:Book b = new Book();

  • 7/29/2019 Unit 1 Intro to Object Oriented Programming PT1

    34/41

    The New Operator

    The new operator allocates memory for an object and

    returns a reference to that object. The reference is thenstored in the variable.

    b is the reference to an instance of Book.

    Java automatically reclaims the memory used by the objectwhen it is no longer referred to by any variable.

    When a new object is created a distinct set of instance

    variables are obtained. The instances of the class are created during the program

    execution and discarded as and when required.

    Example:Book b= new Book();

  • 7/29/2019 Unit 1 Intro to Object Oriented Programming PT1

    35/41

    Difference between Class and Object

    A class creates a new data type that can be use tocreate objects

    A class creates a logical framework that defines the

    relationship between members

    A class is like a blueprint from which an object is

    built.

  • 7/29/2019 Unit 1 Intro to Object Oriented Programming PT1

    36/41

    Difference between Class and Object

    When you declare an object of a class, you aredeclaring an instance of that class

    Hence,

    a class is a logical construct.

    an objecthas physical reality

  • 7/29/2019 Unit 1 Intro to Object Oriented Programming PT1

    37/41

    Dot Operator

    The dot notation is used to obtain the value of the

    instance variables.

    . Where objectReference is the name of the object

    and variable name is the instance variable.

  • 7/29/2019 Unit 1 Intro to Object Oriented Programming PT1

    38/41

    Invoking a Method

    A method is invoked (i.e. made to perform itsdesignated task)by a method call.

    The method call specifies the method name and

    provides information (as arguments) that the called

    methods needs to do its tasks

  • 7/29/2019 Unit 1 Intro to Object Oriented Programming PT1

    39/41

    Method Call

    Calling a method is similar to calling or referring toan instance variable.

    The methods are accessed using dot operator alongwith the object.

    Syntax is:Objectname.Methodname(param1,param2,);

    Example:Book b = new Book();

    b.setName(The World Prose);

  • 7/29/2019 Unit 1 Intro to Object Oriented Programming PT1

    40/41

    Lets look at the example BookApp.java

  • 7/29/2019 Unit 1 Intro to Object Oriented Programming PT1

    41/41

    Additional Reading

    http://java.sun.com/docs/books/tutorial/java/package/namingpkgs.html

    http://www.jarticles.com/package/package_eng.html

    http://www.entertainingcode.com/archives/design-fundamentals-

    encapsulation/

    http://java.sun.com/docs/books/tutorial/java/package/namingpkgs.htmlhttp://java.sun.com/docs/books/tutorial/java/package/namingpkgs.htmlhttp://www.jarticles.com/package/package_eng.htmlhttp://www.jarticles.com/package/package_eng.htmlhttp://java.sun.com/docs/books/tutorial/java/package/namingpkgs.htmlhttp://java.sun.com/docs/books/tutorial/java/package/namingpkgs.html