Java Classes and Objects - PowerPoint PPT Presentation

1 / 37
About This Presentation
Title:

Java Classes and Objects

Description:

Method overloading is Java's polymorphic behavior. Method ... However, in some cases the match need not be exact because of Java's automatic type conversions. ... – PowerPoint PPT presentation

Number of Views:1454
Avg rating:3.0/5.0
Slides: 38
Provided by: drewh6
Category:
Tags: classes | java | javas | objects

less

Transcript and Presenter's Notes

Title: Java Classes and Objects


1
Java Classes and Objects
2
Classes
  • The basic element of object-oriented programming
    object in Java is a class.
  • A class defines the structure (instance
    variables) of an object and its functional
    interface (methods).
  • Once a Java program is running, the system uses
    class definitions to create instances (objects)
    of classes.

3
Classes
  • To define a class, use the class keyword and the
    name of the class
  • class MyClassName
  • class-body

4
Classes
  • The body of the class
  • type instance-variable1
  • type instance-variable2
  • ...
  • type instance-variableN
  • type methodname1(parameter-list)
    method-body
  • type methodname2(parameter-list)
    method-body
  • ...
  • type methodnameN(parameter-list)
    method-body

5
Subclasses
  • If this class is a subclass of another specific
    class
  • class myClassName extends mySuperClassName
  • When a class declaration does not contain an
    extends clause, the class is automatically made a
    subclass of the Object class.

6
Dot (.) Operator
  • The dot(.) operator is used to access the
    instance variables and methods within an object.
  • Objectname.variablename
  • objectname.method(parameter-list)

7
new Operator
  • The new operator creates single instance of a
    named class and returns a reference to that
    object.
  • Classvariable objectreference new classname()

8
new Operator
  • The statement actually is composed of two parts
  • (1) Classvariable objectreference declares an
    object reference to an object of the class type.
    (the object has a null value)
  • (2) objectreference new classname() allocates
    memory for the object and assign the reference to
    the object.
  • (OnePoint.java, TwoPoints.java, Class1.java)

9
Constructor
  • The ( ) in the new statement specifies the
    constructor for the class.
  • A constructor defines what occurs when an object
    of a class is created.
  • A constructor is a method which initializes an
    object immediately upon creation.
  • Constructors have no return type and void.

10
Constructor
  • Most real-world classes explicitly define their
    own constructors within their class definition.
  • If no constructor is specified, Java will
    automatically supply a default constructor.
  • (ClassInit2.java and ClassInit3.java)

11
Object Reference Variables
  • Class1 c1 new Class1( )
  • Class1 c2 c1
  • c1 null
  • Both c1 and c2 refer to the same original object.
  • After c1 is set to null, c2 still point to the
    original object.

12
Instance Variables
  • Data is encapsulated in a class by declaring
    variables inside of the of the class
    declaration.
  • Instance variables are declared the same way as
    the local variables are declared.
  • Instance variables are declared outside of the
    scope any method in the class.

13
Methods
  • type methodname1(parameter-list)
    method-body
  • Type specifies the type of data returned by the
    method.
  • If the method does not return a value, its return
    type must be void.
  • Methods that have return type other than void
    return a value to the calling routine using the
    the return statement
  • return value.

14
Methods
  • The parameter-list is a sequence of type and
    identifier pairs separated by comma. They are
    arguments passed to the the method when it is
    called.
  • A method dose not have to have a parameter-list.

15
Methods
  • When an instance variable is accessed by code
    that is not part of the class, it must be done
    through an object, by using the dot operator.
  • If the instance variable is accessed by code that
    is part of the same class, that variable can be
    referred to directly.
  • (ClassMethod1, 2, 3.java ClassTestStack.java)

16
Method Overloading
  • It is possible to define two or more methods
    within the same class using the same name, as
    long as each parameter declaration (not return
    type) is unique.
  • When this happens, the methods are said to be
    overloaded, and the process is called
    overloading.
  • Method overloading is Javas polymorphic behavior.

17
Method Overloading
  • When an overloaded method is invoked, Java uses
    the type and/or the number of the parameters as
    the guide to determine which version of the
    overloaded method to be called.
  • However, in some cases the match need not be
    exact because of Javas automatic type
    conversions.
  • (MethodOverloading1, 2, 3. Java)

18
Overloading Constructors
  • Like regular methods, constructors can also take
    varying numbers and types of parameters, enabling
    you to create your object with exactly the
    properties you want it to have, or for it to be
    able to calculate properties from different kinds
    of input.
  • (ConstructorOverloading1, 2, 3.java)

19
Objects as Parameters
  • It is common to construct a new object so that it
    is initially the same as some existing object.
  • To do this, define a constructor function that
    takes an object of its class as a parameter.
  • (ConstructorOverloading4.java)

20
Return Objects
  • A method can return any type of data, including
    class types that you create.
  • Since all objects are dynamically allocated using
    new, the object will continue to exist even the
    method in which it was created terminates.
  • (ReturningObject1.java)

21
Argument Passing
  • Call-by-value. A value (of a simple type) is
    passed to the parameter of the method.
  • Call-by-reference. A reference (of an object) to
    an argument is passed to the parameter of the
    method.
  • (ArgumentPassing1, 2.java)

22
Recursion
  • Recursion is the process of defining something in
    terms of itself.
  • It allows a method to call itself.
  • A recursive call does not make a new copy of the
    method. Only the parameter is different.

23
Recursion
  • Recursive versions of many routines may execute a
    bit more slowly than the iterative equivalent
    because of the added overhead of the additional
    function calls.
  • The main advantage is that recursion can be used
    to create clearer and simpler versions of the
    algorithms.
  • (Recursion1.java)

24
Access Control
  • Through encapsulation, you can control what
    members of a Java class can be accessed by other
    parts of the program.
  • The access specifiers determine the
    accessibility.
  • Javas access spedifiers are public, private, and
    protected.

25
Access Control
  • Public specifies that the member can be accessed
    by any other code in a Java program. The main()
    is a typical example.
  • Private specifies that the member can only be
    accessed by the other members of the class.
  • Protected applies only when inheritance is
    involved.

26
Access Control
  • Usually, you will want to restrict access to the
    data members of a class - allowing access only
    through methods.
  • The general form
  • accessspecifier variablename/methodname
  • (AccessControl1.java)

27
static
  • Define a class member to be used independently of
    any object of that class.
  • Create a class member that can be used by itself,
    without any reference to a specific instance.
  • When a member is declared static, it can be
    accessed before any objects of its class are
    created. The main() method is a typical example.

28
static
  • Instance variables declared as static are
    essentially global variables.
  • Methods declared as static
  • (1) can only call other static methods,
  • (2) must only access static data,
  • (3) cannot refer to this or super.
  • (StaticClass 1, 2.java)

29
main()
  • The main() method is the first thing that a Java
    application gets called. The form is
  • public static void main(String args )
  • Public means that this method is available to
    other classes and objects.
  • Static means that this is a class method.

30
main()
  • Void means that the main() method doesnt return
    anything.
  • The main() method takes one parameter an array
    of strings. This argument is used for
    command-line arguments.
  • (MainMethod.java)

31
super
  • Whenever a subclass needs to refer to its
    immediate superclass, it can do so by use of the
    keyword super.
  • Super must always be used as the first statement
    inside a subclass constructor.
  • (Inheritance1.java)

32
Inheritance in Multilevel Hierarchy
  • You can build class hierarchies that contain as
    many as layers of inheritance as you like.
  • Constructors in the multilevel hierarchy are
    called in order of derivation, from superclass to
    subclass.
  • (InheritanceMultilevel.1)

33
Method Overriding
  • In a class hierarchy, when a method in a subclass
    has the same name and type signature as a method
    in its superclass, the method is the subclass is
    said to override the method in the superclass.
  • The version of the method defined by the
    superclass will be hidden.

34
Method Overriding
  • To access the superclass version of the
    overridden method, use the keyword super.
  • Method overriding occurs only when the names and
    type signatures are identical. If they are not,
    then the two methods are simply overloaded.
  • (MethodOverriding1, 2, 3, 4.java)

35
Dynamic Method Dispatch
  • If a superclass contains a method that is
    overridden by a subclass, then different versions
    of the method are executed based on the type of
    the object being referred at the time the call
    occurs.
  • This happens at run-time and is called Javas
    implementation of run-time polymorphism.
    (DynamicMethodDispatch1.java)

36
final
  • The final modifier is used when you do not want a
    method or an instance variable to be overridden.
  • It is common to choose all uppercase identifiers
    for final variables or methods.
  • You can also declare a class final to prevent the
    class from being inherited.

37
abstract
  • Sometimes you want to create an abstract
    superclass that only defines a generalized form
    that will be shared by all of its subclasses,
    leaving it to each subclass to implement what is
    needed.
  • Any class which contains any methods declared
    abstract must also be declared abstract.
Write a Comment
User Comments (0)
About PowerShow.com