java session05

Upload: rohit-chaudhary

Post on 09-Apr-2018

214 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/8/2019 JAVA Session05

    1/22

    Slide 1 of 22Session 5Ver. 1.0

    Java Programming Language

    In this session, you will learn to:

    Understand the use of casting objects

    Describe overloading methods and methods with variable

    arguments

    Describe overloading constructors and invoking parent classconstructors

    Understand Wrapper classes

    Understand autoboxing of primitive types

    Create static variables, methods, and initializers

    Objectives

  • 8/8/2019 JAVA Session05

    2/22

    Slide 2 of 22Session 5Ver. 1.0

    Java Programming Language

    Casting Objects

    Casting objects is used where you have received a

    reference to a parent class, and you want to access the full

    functionality of the object of the subclass:

    Use instanceof to test the type of an object.

    Restore full functionality of an object by casting.Check for proper casting using the following guidelines:

    Casts upward in the hierarchy are done implicitly.

    Downward casts must be to a subclass and checked by the

    compiler.

    The object type is checked at runtime when runtime errors can

    occur.

  • 8/8/2019 JAVA Session05

    3/22

    Slide 3 of 22Session 5Ver. 1.0

    Java Programming Language

    Overloading Methods

    The methods which perform closely related tasks can be

    given the same name by overloading them.

    Overloading can be used as follows:

    public void println(int i)

    public void println(float f)

    public void println(String s)

    Argument list must differ across various methods.

    Return types can be different.

  • 8/8/2019 JAVA Session05

    4/22

    Slide 4 of 22Session 5Ver. 1.0

    Java Programming Language

    Methods Using Variable Arguments

    The varargs or variable arguments is a feature provided

    by J2SE 5.0.

    It helps to pass variable number of arguments, of the same

    type, as parameters, to a method.

    It can be used when you have a number of overloadedmethods, which share the same functionality.

  • 8/8/2019 JAVA Session05

    5/22

    Slide 5 of 22Session 5Ver. 1.0

    Java Programming Language

    Methods Using Variable Arguments (Contd.)

    The following example demonstrates the usage ofvarargs:

    public class Statistics {

    public float average(int... nums) {

    int sum = 0;for ( int x : nums ) {

    sum += x;

    }

    return ((float) sum) / nums.length;

    }}

    We can invoke the average method by passing any numberof arguments as integers.

    nums is an array

    of type int[]

  • 8/8/2019 JAVA Session05

    6/22

    Slide 6 of 22Session 5Ver. 1.0

    Java Programming Language

    Overloading Constructor

    As with methods, constructors can be overloaded:

    An example is:

    public Employee(String name, double

    salary,Date doB)

    public Employee(String name, double salary)public Employee(String name, Date DoB)

    Argument lists must differ.

    You can use the this reference at the first line of a constructor

    to call another constructor.

  • 8/8/2019 JAVA Session05

    7/22

    Slide 7 of 22Session 5Ver. 1.0

    Java Programming Language

    Overloading Constructor (Contd.)

    Example of overloading constructors:

    public class Employee {

    private static final double BASE_SALARY =15000.00;

    private String name;

    private double salary;

    private Date birthDate;

    public Employee(String name, double

    salary, Date DoB) {

    this.name = name;

    this.salary = salary;

    this.birthDate = DoB;

    }

    Initializes all

    instance

    variables

  • 8/8/2019 JAVA Session05

    8/22

    Slide 8 of 22Session 5Ver. 1.0

    Java Programming Language

    Overloading Constructor (Contd.)

    public Employee(String name, doublesalary) {

    this(name, salary, null);

    }

    public Employee(String name, Date

    DoB) {

    this(name, BASE_SALARY, DoB);

    }

    // more Employee code...

    }

    The this keyword in a constructor must be the first line of

    code in the constructor.

    used as a forwarding call

    to the first constructor

    calls the first constructor

    passing in the classconstant BASE_SALARY

  • 8/8/2019 JAVA Session05

    9/22

    Slide 9 of 22Session 5Ver. 1.0

    Java Programming Language

    Constructors Are Not Inherited

    A subclass inherits all methods and variables from the

    superclass (parent class).

    A subclass does not inherit the constructor from the

    superclass.

    Two ways to include a constructor are:Use the default constructor

    Write one or more explicit constructors

  • 8/8/2019 JAVA Session05

    10/22

    Slide 10 of 22Session 5Ver. 1.0

    Java Programming Language

    Invoking Parent Class Constructors

    To invoke a parent class constructor, you must place a callto super() in the first line of the constructor.

    You can call a specific parent constructor by the argumentsthat you use in the call to super().

    If the parent class defines constructors, but does not providea no-argument constructor, then a compiler error message

    is issued.

  • 8/8/2019 JAVA Session05

    11/22

    Slide 11 of 22Session 5Ver. 1.0

    Java Programming Language

    Invoking Parent Class Constructors (Contd.)

    An example of invoking parent class constructor:

    public class Manager extends Employee {

    private String department;

    public Manager(String name, double salary,

    String dept)

    {

    super(name, salary);

    department = dept;

    }public Manager(String name, String dept)

    {

    super(name);

    department = dept; }

  • 8/8/2019 JAVA Session05

    12/22

    Slide 12 of 22Session 5Ver. 1.0

    Java Programming Language

    Invoking Parent Class Constructors (Contd.)

    public Manager(String dept)

    {

    // This code fails: no super()

    department = dept;

    }

    //more Manager code...

    }

  • 8/8/2019 JAVA Session05

    13/22

    Slide 13 of 22Session 5Ver. 1.0

    Java Programming Language

    The Object Class

    The Object class is the root of all classes in Java.

    A class declaration with no extends clause implies extends

    Object. For example:public class Employee{

    ...}

    is equivalent to:public class Employee extends Object{...

    }Two important methods of object class are:

    equals()

    toString()

  • 8/8/2019 JAVA Session05

    14/22

    Slide 14 of 22Session 5Ver. 1.0

    Java Programming Language

    The equals Method

    The == operator determines if two references are identical

    to each other (that is, refer to the same object).

    The equals() method determines if objects are equal but

    not necessarily identical.

    The Object implementation of the equals() method usesthe == operator.

    User classes can override the equals method to implement

    a domain-specific test for equality.

    Note: You should override the hashCode method if youoverride the equals method.

  • 8/8/2019 JAVA Session05

    15/22

    Slide 15 of 22Session 5Ver. 1.0

    Java Programming Language

    The toString Method

    The toString() method has the following characteristics:

    This method converts an object to a String.

    Use this method during string concatenation.

    Override this method to provide information about a

    user-defined object in readable format.Use the wrapper classs toString() static method to

    convert primitive types to a String.

  • 8/8/2019 JAVA Session05

    16/22

    Slide 16 of 22Session 5Ver. 1.0

    Java Programming Language

    Wrapper Classes

    The Java programming language provides wrapper

    classes to manipulate primitive data elements as objects.

    Each Java primitive data type has a corresponding wrapperclass in the java.lang package.

    Example of Primitive Boxing using wrapper classes:int pInt = 420;

    IntegerwInt = newInteger(pInt);

    // this is called boxing

    int p2 = wInt.intValue();

    // this is called unboxing

  • 8/8/2019 JAVA Session05

    17/22

    Slide 17 of 22Session 5Ver. 1.0

    Java Programming Language

    Autoboxing of Primitive Types

    The autoboxing feature enables you to assign and retrieve

    primitive types without the need of the wrapper classes.

    Example of Primitive Autoboxing:

    int pInt = 420;

    IntegerwInt = pInt; // this is called autoboxing

    int p2 = wInt; //this is called autounboxing

    The J2SE 5.0 compiler will create the wrapper object

    automatically when assigning a primitive to a variable of the

    wrapper class type.

    The compiler will also extract the primitive value when

    assigning from a wrapper object to a primitive variable.

  • 8/8/2019 JAVA Session05

    18/22

    Slide 18 of 22Session 5Ver. 1.0

    Java Programming Language

    The static Keyword

    The static keyword is used as a modifier on variables,

    methods, and nested classes.

    The static keyword declares the attribute or method is

    associated with the class as a whole rather than any

    particular instance of that class.Thus,static members are often called class members, suchas class attributes orclass methods.

  • 8/8/2019 JAVA Session05

    19/22

    Slide 19 of 22Session 5Ver. 1.0

    Java Programming Language

    Static Attribute:A public static class attribute can be accessed from outside theclass without an instance of the class.

    Static Method:A static method can be invoked without creating the instance of

    the class.Static methods can not access instance variables.

    Static Initializers:A class can contain code in a static block that does not existwithin a method body.

    Static block code executes once only, when the class isloaded.

    Usually, a static block is used to initialize static (class)attributes.

    The static Keyword (Contd.)

  • 8/8/2019 JAVA Session05

    20/22

    Slide 20 of 22Session 5Ver. 1.0

    Java Programming Language

    Let see how to declare static members in a class, including

    both member variables and methods.

    Demonstration

  • 8/8/2019 JAVA Session05

    21/22

    Slide 21 of 22Session 5Ver. 1.0

    Java Programming Language

    Summary

    In this session, you learned that:

    Casting objects is used where you have received a reference

    to a parent class, and you want to access the full functionality

    of the object of the subclass.

    The methods which perform closely related tasks can be giventhe same name by overloading them.

    The varargs feature helps us to write a generic code to pass

    variable number of arguments, of the same type to a method.

    The super keyword is used to call the constructor of the

    parent class.

    The object class is the root of all classes. The two importantmethods of the object class are equals() method andtostring() method.

  • 8/8/2019 JAVA Session05

    22/22

    Slide 22 of 22Session 5Ver. 1.0

    Java Programming Language

    Summary (Contd.)

    Wrapper classes are used to manipulate primitive data

    elements as objects. Each Java primitive data type has acorresponding wrapper class in the java.lang package.

    Autoboxing feature of J2SE 5.0 enables you to assign and

    retrieve primitive types without the need of the wrapper

    classes.The static keyword declares members (attributes, methods,

    and nested classes) that are associated with the class rather

    than the instances of the class.