3-0 classespolymorphism n inheritance3-1 classobject n method

Upload: qvo-ling

Post on 04-Apr-2018

217 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/30/2019 3-0 Classespolymorphism n Inheritance3-1 Classobject n Method

    1/19

    Introduction To Java Programming1.0 Basic Concepts of Java Programming2.0 Classes, Polymorphism and Inheritance

    3.0 Exception handling4.0

  • 7/30/2019 3-0 Classespolymorphism n Inheritance3-1 Classobject n Method

    2/19

  • 7/30/2019 3-0 Classespolymorphism n Inheritance3-1 Classobject n Method

    3/19

    Create classes and declare their membersin Java programme.1

    Define method and identify its uses.2

    Add methods in Java programmes.3

    Identify the built-in methods in Javalibrary.4

    Write Java programme using methods.5

  • 7/30/2019 3-0 Classespolymorphism n Inheritance3-1 Classobject n Method

    4/19

    Create objects for a class in Javaprogrammes.6

    Develop Java programme using classesand objects.7

    Define constructors and identify its uses.8

    Add constructors in Java programmes.9

    Write Java programme using

    constructors.10

  • 7/30/2019 3-0 Classespolymorphism n Inheritance3-1 Classobject n Method

    5/19

    Introduction

    Java is an object-oriented programming language.

    The concept behind object-oriented programmingis implemented in Java using classes and objects.

    Classes A class is a user-defined data type. Class is acontainer which holds variables and methods .

    Objects The Objects are created to access thevariables and methods from classes.

  • 7/30/2019 3-0 Classespolymorphism n Inheritance3-1 Classobject n Method

    6/19

    Example - Creating a Class

    class Book

    { //body of the class Book }

  • 7/30/2019 3-0 Classespolymorphism n Inheritance3-1 Classobject n Method

    7/19

    Variable and Method Declaration Variables and methods are declared in the body of class. Methods are necessary for operating with the data contained in the class.

    Method declarations have four basic parts:

    The name of the method ( methodname ).The type of the value the method returns ( type ).

    A list of parameters ( parameter-list ).

    The body of the method.

  • 7/30/2019 3-0 Classespolymorphism n Inheritance3-1 Classobject n Method

    8/19

    Variable and Method Declaration class Act5F {

    String name, int age;

    void insert(String n, int a) {

    name = n; age = a;

    }void show()

    { System.out.println (My Name is + name); System.out.println (My Age is + age);

    } public static void main(String[]args)

    { Act5F obj = new Act5F();

    obj.insert (Nabila, 15); obj.show();

    } }

  • 7/30/2019 3-0 Classespolymorphism n Inheritance3-1 Classobject n Method

    9/19

    Creating Objects Object is an entity that consists of variables and methods to manipulate those

    variables.

    The class members can be accessed through objects.

    For example, create an object for class Book.

    Book b1; b1 = new Book( );

    @Book b1 = new Book( );

  • 7/30/2019 3-0 Classespolymorphism n Inheritance3-1 Classobject n Method

    10/19

    Creating Objectsclass Act5F {

    String name, int age;

    void insert(String n, int a) {

    name = n; age = a;

    }void show()

    { System.out.println (My Name is + name); System.out.println (My Age is + age);

    } public static void main(String[]args){

    Act5F obj = new Act5F();obj.insert (Nabila, 15); obj.show();

    }}

  • 7/30/2019 3-0 Classespolymorphism n Inheritance3-1 Classobject n Method

    11/19

    Assigning Object Reference

    You can create an object for a class and assignit to another object.

    Book b1 = new Book( );Book b2 = b1;

  • 7/30/2019 3-0 Classespolymorphism n Inheritance3-1 Classobject n Method

    12/19

    Assign Value to Instance Variables

    You can assign values to the Instance variablesthrough the methods and access the class members.

    ExampleBook b1 = new Book( );

    b1.getData("Java 2 A Beginner's Guide","Herbert Schildt",100);

  • 7/30/2019 3-0 Classespolymorphism n Inheritance3-1 Classobject n Method

    13/19

    class Act5F {

    String name, int age;

    void insert(String n, int a) {

    name = n; age = a;

    }void show()

    { System.out.println (My Name is + name); System.out.println (My Age is + age);

    } public static void main(String[]args) {

    Act5F obj = new Act5F(); obj.insert (Nabila, 15); obj.show();

    } }

    Assign Value to Instance Variables

  • 7/30/2019 3-0 Classespolymorphism n Inheritance3-1 Classobject n Method

    14/19

    Syntax to Assign Value to Instance Variables

    .(parameter-list);

    Here,1. objectname is the name of the object.2. methodname is the method of the class that you

    wish to call. 3. Parameter-list is the comma separated list of

    values that must match in type and number withthe parameter list of the methodname declared inthe class.

  • 7/30/2019 3-0 Classespolymorphism n Inheritance3-1 Classobject n Method

    15/19

    Access Data members in a class

    The data members in a class is accessedusing the dot operator.

    Example :b1.book_name = "Java 2 A Beginner's Guide";b1.author_name = "Herbert Schildt ; b1.no_of_pages = 100;

  • 7/30/2019 3-0 Classespolymorphism n Inheritance3-1 Classobject n Method

    16/19

    Constructors

    Is a special method that enables an object to

    initialise itself when it is created. It is used to initialise the data members of a class.

    Constructor gets automatically called when an

    object is created.

  • 7/30/2019 3-0 Classespolymorphism n Inheritance3-1 Classobject n Method

    17/19

    Constructors

    Characteristics of Constructors Constructors have the same name as the class name. The constructor allocates sufficient memory space for

    the object. They do not return any value. The syntax of a constructor is similar to that of a

    method. Once defined the constructor is automatically called

    immediately when the object is created before thenew operator completes its job. Java creates a default constructor when you do not

    define a constructor.

  • 7/30/2019 3-0 Classespolymorphism n Inheritance3-1 Classobject n Method

    18/19

    Example - Constructor class Book {Book(String bname, String aname, int nopages){

    book_name = bname;

    author_name = aname;no_of_pages = nopages;

    }public static void main(String args[])

    {Book b1 = new Book ("Java 2","HerbertSchildt",100);}

    }

  • 7/30/2019 3-0 Classespolymorphism n Inheritance3-1 Classobject n Method

    19/19

    Example without Constructor class Book {void getData(String bname, String aname, int nopages){

    book_name = bname;

    author_name = aname;no_of_pages = nopages;

    }public static void main(String args[])

    {Book b1 = new Book ();b1.getData ("Java 2","Herbert Schildt",100);}

    }