0% found this document useful (0 votes)
17 views50 pages

Chapter 03

Uploaded by

sally D.allah
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views50 pages

Chapter 03

Uploaded by

sally D.allah
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 50

Chapter 3

Classes, Methods, and Interfaces


 Developcode that declares classes, interfaces,
and enums, and includes the appropriate use
of package and import statements.

 Develop code that declares an interface.


Develop code that implements or extends one
or more interfaces. Develop code that declares
an abstract class. Develop code that extends
an abstract class.
 Develop code that declares both static and non-
static methods. Develop code that declares and
uses a variable-length argument list.

 Develop constructors for one or more of the


classes. Given a class declaration, determine if a
default constructor will be created, and if so,
determine the behavior of that constructor. Given
a nested or non-nested class listing, write code to
instantiate the class.
 Using Methods
 Working with Classes and Objects
 Understanding Enums
 Inheritance
 Writing and Invoking Constructors
 Writing and Using Interfaces
 Methods represent operations on data and
also hold the logic to determine those
operations
 Using methods offer two main advantages:
• A method may be executed (called) repeatedly from
different points in the program: decrease the program
size, the effort to maintain the code and the
probability for an error
• Methods help make the program logically segmented,
or modularized: less error prone, and easier to
maintain
A method is a self-contained block of code
that performs specific operations on the data
by using some logic
 Method declaration:
• Name
• Parameter(s)
• Argument(s)
• Return type
• Access modifier
 The syntax for writing a method in Java:
 The static methods and variables are shared by
all the instances of a class
 The static modifier may be applied to a
variable, a method, and a block of code inside
a method
 Because a static element of a class is visible to
all the instances of the class, if one instance
makes a change to it, all the instances see that
change.
A static variable is initialized when a class is
loaded, whereas an instance variable is
initialized when an instance of the class is
created
 A static method also belongs to the class. It
can be called even before a single instance of
the class exists
 A static method can only access the static
members of the class
A class can also have a static code block
outside of any method
 The code block does not belong to any
method, but only to the class
 executed before the class is instantiated, or
even before the method main() is called
The rules to define variable-length parameters:
 There must be only one variable-length
parameters list.
 If there are individual parameters in addition
to the list, the variable-length parameters list
must appear last inside the parentheses of the
method.
 The variable-length parameters list consists of
a type followed by three dots and the name.
A JavaBean is a special kind of Java class that is
defined by following certain rules:
• The private variables / properties can only be accessed
through getter and setter methods
• The getter and setter methods must be public so that
anyone who uses the bean can invoke them.
• A setter method must have the void return type and must
have a parameter that represents the type of the
corresponding property
• A getter method does not have any parameter and its
return type matches the argument type of the
corresponding setter method.
 getMeanScore() and setMeanScore(),
correspond to the variable (property)
meanScore
 Using Methods
 Working with Classes and Objects
 Understanding Enums
 Inheritance
 Writing and Invoking Constructors
 Writing and Using Interfaces
A class is a template that contains the data
variables and the methods that operate on
those data variables following some logic

 Class members:
• Variables represent the state of an object
• Methods constitute its behavior
 The general syntax:
<modifier> class <className> { }
 <className> specifies the name of the class
 class is the keyword
 <modifier> specifies some characteristics of
the class:
• Access modifiers: private, protected, default and
public
• Other modifiers: abstract, final, and strictfp
 <variableName>: the name of the object
reference that will refer to the object that you
want to create
 <className>: the name of an existing class
 <classConstructor>: a constructor of the
class
 The right side of the equation creates the object
of the class specified by <className> with the
new operator, and assigns it to <variableName>
(i.e. <variableName> points to it)
 allowsyou to define a class (like a variable or a
method) inside a top-level class (outer class or
enclosing class)
 aninstance of an inner class can only exist
within an instance of its outer class
 Using Methods
 Working with Classes and Objects
 Understanding Enums
 Inheritance
 Writing and Invoking Constructors
 Writing and Using Interfaces
 useful when you want a variable to hold only a
predetermined set of values
 define an enum variable in two steps:
1. Define the enum type with a set of named values
2. Define a variable to hold one of those values
 subclass of the Java class Enum
 Using Methods
 Working with Classes and Objects
 Understanding Enums
 Inheritance
 Writing and Invoking Constructors
 Writing and Using Interfaces
 enable the programmer to write a class based
on an already existing class - the parent class /
super class
 The subclass inherits (reuses) the non-private
members of the super class
 The keyword to derive a class from another
class is extends
 Using Methods
 Working with Classes and Objects
 Understanding Enums
 Inheritance
 Writing and Invoking Constructors
 Writing and Using Interfaces
 The constructor of a class has the same name
as the class and has no explicit return type
 The new operator allocates memory for the
instance, and executes the constructor to
initialize the memory

ComputerLab csLab = new ComputerLab();


1. Allocates memory for an instance of class
ComputerLab
2. Initializes the instance variables of class ComputerLab
3. Executes the constructor ComputerLab()
 Ifyou do not provide any constructor for a
class you write, the compiler provides the
default constructor for that class
 You can also define non default constructors
with parameters
 The constructor may be called:
• from inside the class: from within another constructor,
using this() or super()
• from outside the class: with the new operator
 use this() to call another constructor in the
same class, and use super() to call a constructor
in the superclass
 must appear in the beginning the constructor
 Without a super call or a this call, the compiler
places a super call in the beginning of the
constructor’s body
 Before executing the body of a constructor in a
class that is being instantiated, a constructor in
the superclass must be executed
A constructor of a class has the same name as
the class, and has no explicit return type
 A class may have more than one constructor
 If the programmer defines no constructor in a
class, the compiler will add the default
constructor with no arguments
 If there are one or more constructors defined
in the class, the compiler will not provide any
constructor
A constructor may have zero or more parameters
 From outside the class, a constructor is always
called with the new operator
 From inside the class, it may be called from
another constructor with the this or super
operator
 Unlike other methods, the constructors are not
inherited
 If there is no super call in a constructor, the
default super call is placed by the compiler
 Using Methods
 Working with Classes and Objects
 Understanding Enums
 Inheritance
 Writing and Invoking Constructors
 Writing and Using Interfaces
 Java supports single inheritance - a subclass in
Java can have only one superclass
 If multiple inheritance is needed: use an
interface
 An interface is a template that contains some
method declarations - no implementation
 The class that inherits from an interface must
provide the implementation for the methods
declared in the interface
 You define an interface by using the keyword
interface
 The methods declared in an interface are implicitly
public and abstract
 The data variables declared in an interface are
inherently constants and are visible from all the
instances of the class that implements the interface
A class can extend only one class by using the
keyword extends, but it can inherit from one
or more interfaces by using the keyword
implements
 An interface can also extend one or more
interfaces by using the keyword extends
 An interface cannot implement any interface
or class
 All interface variables are inherently public,
static, and final
 All interface methods are inherently public and
abstract
 Because interface methods are inherently
abstract, they cannot be declared final,
native, strictfp, or synchronized
 A class can extend another class and implement
one or more interfaces at the same time
 An interface cannot extend a class
 An interface cannot implement another interface
or class

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy