SlideShare a Scribd company logo
Chapter 10
Classes Continued
Fundamentals of Java
Fundamentals of Java
2
Objectives
 Know when it is appropriate to include class
(static) variables and methods in a class.
 Understand the role of Java interfaces in a
software system and define an interface for
a set of implementing classes.
 Understand the use of inheritance by
extending a class.
Fundamentals of Java
3
Objectives (cont.)
 Understand the use of polymorphism and
know how to override methods in a
superclass.
 Place the common features (variables and
methods) of a set of classes in an abstract
class.
Fundamentals of Java
4
Objectives (cont.)
 Understand the implications of reference
types for equality, copying, and mixed-mode
operations.
 Know how to define and use methods that
have preconditions, postconditions, and
throw exceptions.
Fundamentals of Java
5
Vocabulary
 Abstract class
 Abstract method
 Aggregation
 Class (static) method
 Class (static) variable
 Concrete class
 Dependency
Fundamentals of Java
6
Vocabulary (cont.)
 Final method
 Inheritance
 Interface
 Overriding
 Postcondition
 Precondition
Fundamentals of Java
7
Class (static) Variables and
Methods
 static variables and methods belong to a
class.
– Not an instance of the class
 Class variable: Storage allocated at
program startup
– Independent of number of instances created
 Class method: Activated when message
sent to the class rather than to an object
Fundamentals of Java
8
Class (static) Variables and
Methods (cont.)
 Class variables and methods are declared
with the keyword static.
 Example:
– private static int studentCount = 0;
 Shared by all instances of the class with this
declaration
 Use a static variable in any situation in which
all instances share a common data value.
Fundamentals of Java
9
Class (static) Variables and
Methods (cont.)
 Use static methods to provide public access
to static variables.
 Class constants: Combine keyword final
with keyword static
– Example:
 public static final int MIN_SCORE = 0;
Fundamentals of Java
10
Class (static) Variables and
Methods (cont.)
Static variables, methods, and constants example
Fundamentals of Java
11
Class (static) Variables and
Methods (cont.)
Static variables, methods, and constants example (cont.)
Fundamentals of Java
12
Class (static) Variables and
Methods (cont.)
 Using class variables example:
 Using class constants example:
Fundamentals of Java
13
Class (static) Variables and
Methods (cont.)
 Two rules for using static variables:
– Class methods can reference only static
variables.
 Never instance variables
– Instance methods can reference static and
instance variables.
 The main method for an executable java
class is static.
– JVM sends main message to start a program.
Fundamentals of Java
14
Turtle Graphics
 Open-source Java package for drawing
– Used in this text to illustrate features of object-
oriented programming
– Pen used for drawing on a window
 StandardPen is a specific type of Pen.
Fundamentals of Java
15
Turtle Graphics (cont.)
Table 10-1: Pen messages
Fundamentals of Java
16
Turtle Graphics (cont.)
Example 10.1: Drawing a square using Turtle Graphics
Fundamentals of Java
17
Java Interfaces—The Client
Perspective
 Interface: A list of a class’s public methods
– Provides information to use a class without
revealing its implementation
– When related classes have same interface, they
can be used interchangeably in a program.
 In Turtle Graphics, Pen is an interface.
– StandardPen, WigglePen, and RainbowPen
are examples of classes that conform to the
Pen interface.
Fundamentals of Java
18
Java Interfaces—The Client
Perspective (cont.)
 A Java interface specifies the method
signatures for an interface.
Fundamentals of Java
19
Java Interfaces—The Client
Perspective (cont.)
 An interface is not a class.
– But can be used as a data type
 Having multiple classes conform to the same
interface allows for polymorphic behavior:
Fundamentals of Java
20
Java Interfaces—The Client
Perspective (cont.)
 A class that conforms to an interface is said
to implement the interface.
 When declaring a variable or parameter, use
the interface type when possible.
– Methods using interface types are more
general.
– They are easier to maintain.
Fundamentals of Java
21
Java Interfaces—The
Implementation Perspective
 A class
implements an
interface using
the implements
keyword.
Fundamentals of Java
22
Java Interfaces – The
Implementation Perspective (cont.)
 A class that implements an interface must
implement every method in the interface.
 A variable declared with the interface type
can reference objects of any class that
implements the interface.
Fundamentals of Java
23
Java Interfaces – The
Implementation Perspective (cont.)
The Circle class
Fundamentals of Java
24
Java Interfaces – The
Implementation Perspective (cont.)
The Circle class (cont.)
Fundamentals of Java
25
Java Interfaces – The
Implementation Perspective (cont.)
Example 10.3: Try out some shapes
Fundamentals of Java
26
Java Interfaces – The
Implementation Perspective (cont.)
Example 10.3: Try out some shapes (cont.)
Fundamentals of Java
27
Java Interfaces – The
Implementation Perspective (cont.)
Figure 10.3: Output from the TestShapes program
Fundamentals of Java
28
Java Interfaces – The
Implementation Perspective (cont.)
 Important interface concepts:
– Interface contains only methods, never
variables.
– Interface methods are usually public.
– If more than one class implements an interface,
its methods are polymorphic.
Fundamentals of Java
29
Java Interfaces – The
Implementation Perspective (cont.)
 Important interface concepts (cont.):
– A class can implement methods in addition to
those listed in the interface.
– A class can implement more than one interface.
– Interfaces can be in an inheritance hierarchy.
Fundamentals of Java
30
Code Reuse Through Inheritance
 All classes are part of a large class hierarchy.
– Object class is the root.
– Each class inherits variables and methods of
the classes above it in the hierarchy.
 New classes can add new variables, add new
methods, or alter existing inherited methods.
– Class immediately above a class is a
superclass.
– Any classes that inherit are subclasses.
Fundamentals of Java
31
Code Reuse Through Inheritance
(cont.)
 A class can only have one superclass, but
may have many subclasses.
 The descendents of a class consist of its
subclasses, their subclasses, etc.
Figure 10-4: Part of a class hierarchy
Fundamentals of Java
32
Code Reuse Through Inheritance
(cont.)
 A class can inherit the characteristics of another
class using the extends keyword.
The Wheel class extends the Circle class.
Fundamentals of Java
33
Code Reuse Through Inheritance
(cont.)
The Wheel class extends the Circle class (cont.).
Fundamentals of Java
34
Code Reuse Through Inheritance
(cont.)
Example 10.4: Draw a wheel and a circle.
Fundamentals of Java
35
Code Reuse Through Inheritance
(cont.)
Figure 10-5: A circle and a wheel with the
same radius but different positions
Fundamentals of Java
36
Code Reuse Through Inheritance
(cont.)
 Wheel implements Shape because it
extends Circle, which implements Shape.
 xPos, yPos, and radius inherited by
Wheel from Shape
– Must modify these instance variables in Shape
to be protected rather than private
 Protected access modifier means variables
are accessible only in current class and its
descendents.
Fundamentals of Java
37
Code Reuse Through Inheritance
(cont.)
 Methods may also be protected.
 A constructor may call superclass constructor
using super();.
 To call one of superclass methods:
 Overriding: A subclass can modify a
superclass method by re-implementing it.
Fundamentals of Java
38
Inheritance and Abstract Classes
 Abstract class: Provides functionality
(methods), but can never be instantiated
– Can only be extended
– Declared with keyword abstract
– May contain standard methods and abstract
methods
Fundamentals of Java
39
Inheritance and Abstract Classes
(cont.)
 Abstract method: A method with no body
– Defined using keyword abstract
– A class with an abstract method will also be
abstract.
– A subclass of an abstract class must implement
every abstract method in that class.
 Final methods: Methods that cannot be
overridden in a subclass
Fundamentals of Java
40
Inheritance and Abstract Classes
(cont.)
 Partial abstract class/method example:
Fundamentals of Java
41
 A Java interface has a name and consists of
a list of method headers.
 One or more classes can implement the
same interface.
 If a variable is declared as an interface type,
it can be associated with an object from any
class that implements the interface.
Interfaces, Inheritance, and
Relationships among Classes
Fundamentals of Java
42
Interfaces, Inheritance, and
Relationships among Classes (cont.)
 If a class implements an interface, then all of
its subclasses do so implicitly.
 A subclass inherits all of the characteristics of
its superclass.
– Subclass can add new variables and methods or
modify inherited methods.
 Characteristics common to several classes
can be collected in a common abstract
superclass that is never instantiated.
Fundamentals of Java
43
Interfaces, Inheritance, and
Relationships among Classes (cont.)
 An abstract class can contain headers for
abstract methods that are implemented in the
subclasses.
 A class’s constructors and methods can use
constructors and methods in the superclass.
 Inheritance reduces repetition and promotes
the reuse of code.
Fundamentals of Java
44
Interfaces, Inheritance, and
Relationships among Classes (cont.)
 Interfaces and inheritance promote the use
of polymorphism.
 When a message is sent to an object, Java
looks for a matching method.
– Search starts in the object’s class and, if
necessary, continues up the class hierarchy
Fundamentals of Java
45
Interfaces, Inheritance, and
Relationships among Classes (cont.)
 Four ways in which methods in a subclass
can be related to methods in a superclass:
– Implementation of an abstract method
– Extension
– Overriding
– Finality
 It is possible to work only with abstract
classes, forgoing interfaces.
Fundamentals of Java
46
Interfaces, Inheritance, and
Relationships among Classes (cont.)
Figure 10-7: Three types of relationships among classes
Fundamentals of Java
47
Acceptable Classes for
Parameters and Return Values
 If an object of class BBB is expected, it is
always acceptable to substitute an object of a
subclass.
– But never of a superclass
 A subclass of BBB inherits all of BBB’s methods.
 No guarantees about the methods in the
superclass
Fundamentals of Java
48
Error Handling with Classes
 Before implementing error handling code,
must determine error conditions for a class
 Preconditions: Describe what must be true
before a particular method is called
– Values for parameters/instance variables
 Postconditions: Describe what must be true
after a particular method has been executed
– Return values and altered instance variables
Fundamentals of Java
49
Error Handling with Classes
(cont.)
 Example:
Fundamentals of Java
50
Exceptions
 JVM can throw exceptions when illegal
operations are attempted.
 Commonly used exception classes:
Fundamentals of Java
51
Exceptions (cont.)
 Can throw exceptions in methods:
– To enforce pre-conditions or any other condition
 Syntax:
 Example:
Fundamentals of Java
52
Exceptions (cont.)
 Exceptions to enforce pre-conditions:
Fundamentals of Java
53
Exceptions (cont.)
 Clients who call such methods need to catch
the exceptions so the program does not halt.
– Embed method call in a try-catch statement
Fundamentals of Java
54
Exceptions (cont.)
 A method may throw more than one type of
exception.
– Client can handle each type explicitly.
 Example:
Fundamentals of Java
55
Reference Types, Equality,
and Object Identity
 Aliasing: Two reference variables point to
same object
 Comparing objects for equality:
– Comparing two reference variables using ==
indicates whether the variables point to the
same object.
– To compare values of two distinct objects for
equality, use the equals method.
Fundamentals of Java
56
Reference Types, Equality,
and Object Identity (cont.)
 equals method: Defined in Object class
– Uses == operator by default
– A class must override equals to allow for
comparison of object’s contents.
 Example:
Fundamentals of Java
57
Reference Types, Equality,
and Object Identity (cont.)
 Copying objects:
– Aliasing is not copying.
– Use a copy constructor:
– Implement Clonable:
Fundamentals of Java
58
Summary
 Class (static) variables provide storage for
data that all instances of a class can access
but do not have to own separately.
 Class (static) methods are written primarily
for class variables.
 An interface specifies a set of methods that
implementing classes must include.
– Gives clients enough information to use a class
Fundamentals of Java
59
Summary (cont.)
 Polymorphism and inheritance reduce the
amount of code that must be written by
servers and learned by clients.
 Classes that extend other classes inherit
their data and methods.
 Methods in different classes that have the
same name are polymorphic.
Fundamentals of Java
60
Summary (cont.)
 Abstract classes are not instantiated.
– Help organize related subclasses
– Contain their common data and methods
 Error handling can be distributed among
methods and classes by using preconditions,
postconditions, and exceptions.
Fundamentals of Java
61
Summary (cont.)
 Because of the possibility of aliasing, the
programmer should provide:
– equals method for comparing for equality
– clone method for creating a copy of an object

More Related Content

What's hot

9781111530532 ppt ch10
9781111530532 ppt ch109781111530532 ppt ch10
9781111530532 ppt ch10Terry Yoast
 
Chapter 2 - Basic Elements of Java
Chapter 2 - Basic Elements of JavaChapter 2 - Basic Elements of Java
Chapter 2 - Basic Elements of JavaAdan Hubahib
 
9781111530532 ppt ch08
9781111530532 ppt ch089781111530532 ppt ch08
9781111530532 ppt ch08Terry Yoast
 
9781111530532 ppt ch03
9781111530532 ppt ch039781111530532 ppt ch03
9781111530532 ppt ch03Terry Yoast
 
Java interfaces & abstract classes
Java interfaces & abstract classesJava interfaces & abstract classes
Java interfaces & abstract classesShreyans Pathak
 
CIS 1403 lab 3 functions and methods in Java
CIS 1403 lab 3 functions and methods in JavaCIS 1403 lab 3 functions and methods in Java
CIS 1403 lab 3 functions and methods in JavaHamad Odhabi
 
9781111530532 ppt ch07
9781111530532 ppt ch079781111530532 ppt ch07
9781111530532 ppt ch07Terry Yoast
 
9781111530532 ppt ch06
9781111530532 ppt ch069781111530532 ppt ch06
9781111530532 ppt ch06Terry Yoast
 
9781439035665 ppt ch07
9781439035665 ppt ch079781439035665 ppt ch07
9781439035665 ppt ch07Terry Yoast
 
Chapter 13 - Recursion
Chapter 13 - RecursionChapter 13 - Recursion
Chapter 13 - RecursionAdan Hubahib
 
Polymorphism presentation in java
Polymorphism presentation in javaPolymorphism presentation in java
Polymorphism presentation in javaAhsan Raja
 
9781111530532 ppt ch04
9781111530532 ppt ch049781111530532 ppt ch04
9781111530532 ppt ch04Terry Yoast
 
9781111530532 ppt ch12
9781111530532 ppt ch129781111530532 ppt ch12
9781111530532 ppt ch12Terry Yoast
 
Java interview questions 1
Java interview questions 1Java interview questions 1
Java interview questions 1Sherihan Anver
 
9781111530532 ppt ch05
9781111530532 ppt ch059781111530532 ppt ch05
9781111530532 ppt ch05Terry Yoast
 

What's hot (19)

Ppt chapter03
Ppt chapter03Ppt chapter03
Ppt chapter03
 
Ppt chapter02
Ppt chapter02Ppt chapter02
Ppt chapter02
 
Ppt chapter08
Ppt chapter08Ppt chapter08
Ppt chapter08
 
9781111530532 ppt ch10
9781111530532 ppt ch109781111530532 ppt ch10
9781111530532 ppt ch10
 
Chapter 2 - Basic Elements of Java
Chapter 2 - Basic Elements of JavaChapter 2 - Basic Elements of Java
Chapter 2 - Basic Elements of Java
 
9781111530532 ppt ch08
9781111530532 ppt ch089781111530532 ppt ch08
9781111530532 ppt ch08
 
9781111530532 ppt ch03
9781111530532 ppt ch039781111530532 ppt ch03
9781111530532 ppt ch03
 
Towards Improving Interface Modularity in Legacy Java Software Through Automa...
Towards Improving Interface Modularity in Legacy Java Software Through Automa...Towards Improving Interface Modularity in Legacy Java Software Through Automa...
Towards Improving Interface Modularity in Legacy Java Software Through Automa...
 
Java interfaces & abstract classes
Java interfaces & abstract classesJava interfaces & abstract classes
Java interfaces & abstract classes
 
CIS 1403 lab 3 functions and methods in Java
CIS 1403 lab 3 functions and methods in JavaCIS 1403 lab 3 functions and methods in Java
CIS 1403 lab 3 functions and methods in Java
 
9781111530532 ppt ch07
9781111530532 ppt ch079781111530532 ppt ch07
9781111530532 ppt ch07
 
9781111530532 ppt ch06
9781111530532 ppt ch069781111530532 ppt ch06
9781111530532 ppt ch06
 
9781439035665 ppt ch07
9781439035665 ppt ch079781439035665 ppt ch07
9781439035665 ppt ch07
 
Chapter 13 - Recursion
Chapter 13 - RecursionChapter 13 - Recursion
Chapter 13 - Recursion
 
Polymorphism presentation in java
Polymorphism presentation in javaPolymorphism presentation in java
Polymorphism presentation in java
 
9781111530532 ppt ch04
9781111530532 ppt ch049781111530532 ppt ch04
9781111530532 ppt ch04
 
9781111530532 ppt ch12
9781111530532 ppt ch129781111530532 ppt ch12
9781111530532 ppt ch12
 
Java interview questions 1
Java interview questions 1Java interview questions 1
Java interview questions 1
 
9781111530532 ppt ch05
9781111530532 ppt ch059781111530532 ppt ch05
9781111530532 ppt ch05
 

Similar to Ppt chapter10 (20)

Polymorphism
PolymorphismPolymorphism
Polymorphism
 
Java abstract Keyword.pdf
Java abstract Keyword.pdfJava abstract Keyword.pdf
Java abstract Keyword.pdf
 
Core java questions
Core java questionsCore java questions
Core java questions
 
Suga java training_with_footer
Suga java training_with_footerSuga java training_with_footer
Suga java training_with_footer
 
Java interview questions
Java interview questionsJava interview questions
Java interview questions
 
L5
L5L5
L5
 
core_java.ppt
core_java.pptcore_java.ppt
core_java.ppt
 
Oop
OopOop
Oop
 
116824015 java-j2 ee
116824015 java-j2 ee116824015 java-j2 ee
116824015 java-j2 ee
 
Java interview
Java interviewJava interview
Java interview
 
Unit 4
Unit 4Unit 4
Unit 4
 
this keyword in Java.pdf
this keyword in Java.pdfthis keyword in Java.pdf
this keyword in Java.pdf
 
Reflection in java
Reflection in javaReflection in java
Reflection in java
 
PPT Lecture-1.4.pptx
PPT Lecture-1.4.pptxPPT Lecture-1.4.pptx
PPT Lecture-1.4.pptx
 
Complete java&j2ee
Complete java&j2eeComplete java&j2ee
Complete java&j2ee
 
Java/J2EE interview Qestions
Java/J2EE interview QestionsJava/J2EE interview Qestions
Java/J2EE interview Qestions
 
Java scjp-part1
Java scjp-part1Java scjp-part1
Java scjp-part1
 
Java 06
Java 06Java 06
Java 06
 
Java Basics Presentation
Java Basics PresentationJava Basics Presentation
Java Basics Presentation
 
Java
JavaJava
Java
 

More from Richard Styner

More from Richard Styner (15)

OneNote for Students.pptx
OneNote for Students.pptxOneNote for Students.pptx
OneNote for Students.pptx
 
Hopscotch.docx
Hopscotch.docxHopscotch.docx
Hopscotch.docx
 
Lit Review + Case Study (1).docx
Lit Review + Case Study (1).docxLit Review + Case Study (1).docx
Lit Review + Case Study (1).docx
 
udlspace.docx
udlspace.docxudlspace.docx
udlspace.docx
 
Documentary Video UDL Lesson.docx
Documentary Video UDL Lesson.docxDocumentary Video UDL Lesson.docx
Documentary Video UDL Lesson.docx
 
rubric.docx
rubric.docxrubric.docx
rubric.docx
 
Ppt chapter04
Ppt chapter04Ppt chapter04
Ppt chapter04
 
Ppt chapter03
Ppt chapter03Ppt chapter03
Ppt chapter03
 
Ppt chapter01
Ppt chapter01Ppt chapter01
Ppt chapter01
 
Richard Styner Issues and trends
Richard Styner Issues and trendsRichard Styner Issues and trends
Richard Styner Issues and trends
 
Ppt chapter 01
Ppt chapter 01Ppt chapter 01
Ppt chapter 01
 
Tech mentoring project presentation
Tech mentoring project presentationTech mentoring project presentation
Tech mentoring project presentation
 
The Photoshop Effect
The Photoshop EffectThe Photoshop Effect
The Photoshop Effect
 
Stanford Solar Center Joint Curriculum
Stanford Solar Center Joint CurriculumStanford Solar Center Joint Curriculum
Stanford Solar Center Joint Curriculum
 
The Stanford Solar Lab
The Stanford Solar Lab The Stanford Solar Lab
The Stanford Solar Lab
 

Recently uploaded

The Art Pastor's Guide to Sabbath | Steve Thomason
The Art Pastor's Guide to Sabbath | Steve ThomasonThe Art Pastor's Guide to Sabbath | Steve Thomason
The Art Pastor's Guide to Sabbath | Steve ThomasonSteve Thomason
 
UNIT – IV_PCI Complaints: Complaints and evaluation of complaints, Handling o...
UNIT – IV_PCI Complaints: Complaints and evaluation of complaints, Handling o...UNIT – IV_PCI Complaints: Complaints and evaluation of complaints, Handling o...
UNIT – IV_PCI Complaints: Complaints and evaluation of complaints, Handling o...Sayali Powar
 
Matatag-Curriculum and the 21st Century Skills Presentation.pptx
Matatag-Curriculum and the 21st Century Skills Presentation.pptxMatatag-Curriculum and the 21st Century Skills Presentation.pptx
Matatag-Curriculum and the 21st Century Skills Presentation.pptxJenilouCasareno
 
Palestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptxPalestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptxRaedMohamed3
 
How to Create Map Views in the Odoo 17 ERP
How to Create Map Views in the Odoo 17 ERPHow to Create Map Views in the Odoo 17 ERP
How to Create Map Views in the Odoo 17 ERPCeline George
 
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXXPhrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXXMIRIAMSALINAS13
 
How to Break the cycle of negative Thoughts
How to Break the cycle of negative ThoughtsHow to Break the cycle of negative Thoughts
How to Break the cycle of negative ThoughtsCol Mukteshwar Prasad
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaasiemaillard
 
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdfAdversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdfPo-Chuan Chen
 
Jose-Rizal-and-Philippine-Nationalism-National-Symbol-2.pptx
Jose-Rizal-and-Philippine-Nationalism-National-Symbol-2.pptxJose-Rizal-and-Philippine-Nationalism-National-Symbol-2.pptx
Jose-Rizal-and-Philippine-Nationalism-National-Symbol-2.pptxricssacare
 
INU_CAPSTONEDESIGN_비밀번호486_업로드용 발표자료.pdf
INU_CAPSTONEDESIGN_비밀번호486_업로드용 발표자료.pdfINU_CAPSTONEDESIGN_비밀번호486_업로드용 발표자료.pdf
INU_CAPSTONEDESIGN_비밀번호486_업로드용 발표자료.pdfbu07226
 
Accounting and finance exit exam 2016 E.C.pdf
Accounting and finance exit exam 2016 E.C.pdfAccounting and finance exit exam 2016 E.C.pdf
Accounting and finance exit exam 2016 E.C.pdfYibeltalNibretu
 
MARUTI SUZUKI- A Successful Joint Venture in India.pptx
MARUTI SUZUKI- A Successful Joint Venture in India.pptxMARUTI SUZUKI- A Successful Joint Venture in India.pptx
MARUTI SUZUKI- A Successful Joint Venture in India.pptxbennyroshan06
 
Forest and Wildlife Resources Class 10 Free Study Material PDF
Forest and Wildlife Resources Class 10 Free Study Material PDFForest and Wildlife Resources Class 10 Free Study Material PDF
Forest and Wildlife Resources Class 10 Free Study Material PDFVivekanand Anglo Vedic Academy
 
Additional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdfAdditional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdfjoachimlavalley1
 
The Benefits and Challenges of Open Educational Resources
The Benefits and Challenges of Open Educational ResourcesThe Benefits and Challenges of Open Educational Resources
The Benefits and Challenges of Open Educational Resourcesaileywriter
 
slides CapTechTalks Webinar May 2024 Alexander Perry.pptx
slides CapTechTalks Webinar May 2024 Alexander Perry.pptxslides CapTechTalks Webinar May 2024 Alexander Perry.pptx
slides CapTechTalks Webinar May 2024 Alexander Perry.pptxCapitolTechU
 
Advances in production technology of Grapes.pdf
Advances in production technology of Grapes.pdfAdvances in production technology of Grapes.pdf
Advances in production technology of Grapes.pdfDr. M. Kumaresan Hort.
 

Recently uploaded (20)

B.ed spl. HI pdusu exam paper-2023-24.pdf
B.ed spl. HI pdusu exam paper-2023-24.pdfB.ed spl. HI pdusu exam paper-2023-24.pdf
B.ed spl. HI pdusu exam paper-2023-24.pdf
 
The Art Pastor's Guide to Sabbath | Steve Thomason
The Art Pastor's Guide to Sabbath | Steve ThomasonThe Art Pastor's Guide to Sabbath | Steve Thomason
The Art Pastor's Guide to Sabbath | Steve Thomason
 
UNIT – IV_PCI Complaints: Complaints and evaluation of complaints, Handling o...
UNIT – IV_PCI Complaints: Complaints and evaluation of complaints, Handling o...UNIT – IV_PCI Complaints: Complaints and evaluation of complaints, Handling o...
UNIT – IV_PCI Complaints: Complaints and evaluation of complaints, Handling o...
 
Matatag-Curriculum and the 21st Century Skills Presentation.pptx
Matatag-Curriculum and the 21st Century Skills Presentation.pptxMatatag-Curriculum and the 21st Century Skills Presentation.pptx
Matatag-Curriculum and the 21st Century Skills Presentation.pptx
 
Palestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptxPalestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptx
 
How to Create Map Views in the Odoo 17 ERP
How to Create Map Views in the Odoo 17 ERPHow to Create Map Views in the Odoo 17 ERP
How to Create Map Views in the Odoo 17 ERP
 
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXXPhrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
 
How to Break the cycle of negative Thoughts
How to Break the cycle of negative ThoughtsHow to Break the cycle of negative Thoughts
How to Break the cycle of negative Thoughts
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
 
NCERT Solutions Power Sharing Class 10 Notes pdf
NCERT Solutions Power Sharing Class 10 Notes pdfNCERT Solutions Power Sharing Class 10 Notes pdf
NCERT Solutions Power Sharing Class 10 Notes pdf
 
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdfAdversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
 
Jose-Rizal-and-Philippine-Nationalism-National-Symbol-2.pptx
Jose-Rizal-and-Philippine-Nationalism-National-Symbol-2.pptxJose-Rizal-and-Philippine-Nationalism-National-Symbol-2.pptx
Jose-Rizal-and-Philippine-Nationalism-National-Symbol-2.pptx
 
INU_CAPSTONEDESIGN_비밀번호486_업로드용 발표자료.pdf
INU_CAPSTONEDESIGN_비밀번호486_업로드용 발표자료.pdfINU_CAPSTONEDESIGN_비밀번호486_업로드용 발표자료.pdf
INU_CAPSTONEDESIGN_비밀번호486_업로드용 발표자료.pdf
 
Accounting and finance exit exam 2016 E.C.pdf
Accounting and finance exit exam 2016 E.C.pdfAccounting and finance exit exam 2016 E.C.pdf
Accounting and finance exit exam 2016 E.C.pdf
 
MARUTI SUZUKI- A Successful Joint Venture in India.pptx
MARUTI SUZUKI- A Successful Joint Venture in India.pptxMARUTI SUZUKI- A Successful Joint Venture in India.pptx
MARUTI SUZUKI- A Successful Joint Venture in India.pptx
 
Forest and Wildlife Resources Class 10 Free Study Material PDF
Forest and Wildlife Resources Class 10 Free Study Material PDFForest and Wildlife Resources Class 10 Free Study Material PDF
Forest and Wildlife Resources Class 10 Free Study Material PDF
 
Additional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdfAdditional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdf
 
The Benefits and Challenges of Open Educational Resources
The Benefits and Challenges of Open Educational ResourcesThe Benefits and Challenges of Open Educational Resources
The Benefits and Challenges of Open Educational Resources
 
slides CapTechTalks Webinar May 2024 Alexander Perry.pptx
slides CapTechTalks Webinar May 2024 Alexander Perry.pptxslides CapTechTalks Webinar May 2024 Alexander Perry.pptx
slides CapTechTalks Webinar May 2024 Alexander Perry.pptx
 
Advances in production technology of Grapes.pdf
Advances in production technology of Grapes.pdfAdvances in production technology of Grapes.pdf
Advances in production technology of Grapes.pdf
 

Ppt chapter10

  • 2. Fundamentals of Java 2 Objectives  Know when it is appropriate to include class (static) variables and methods in a class.  Understand the role of Java interfaces in a software system and define an interface for a set of implementing classes.  Understand the use of inheritance by extending a class.
  • 3. Fundamentals of Java 3 Objectives (cont.)  Understand the use of polymorphism and know how to override methods in a superclass.  Place the common features (variables and methods) of a set of classes in an abstract class.
  • 4. Fundamentals of Java 4 Objectives (cont.)  Understand the implications of reference types for equality, copying, and mixed-mode operations.  Know how to define and use methods that have preconditions, postconditions, and throw exceptions.
  • 5. Fundamentals of Java 5 Vocabulary  Abstract class  Abstract method  Aggregation  Class (static) method  Class (static) variable  Concrete class  Dependency
  • 6. Fundamentals of Java 6 Vocabulary (cont.)  Final method  Inheritance  Interface  Overriding  Postcondition  Precondition
  • 7. Fundamentals of Java 7 Class (static) Variables and Methods  static variables and methods belong to a class. – Not an instance of the class  Class variable: Storage allocated at program startup – Independent of number of instances created  Class method: Activated when message sent to the class rather than to an object
  • 8. Fundamentals of Java 8 Class (static) Variables and Methods (cont.)  Class variables and methods are declared with the keyword static.  Example: – private static int studentCount = 0;  Shared by all instances of the class with this declaration  Use a static variable in any situation in which all instances share a common data value.
  • 9. Fundamentals of Java 9 Class (static) Variables and Methods (cont.)  Use static methods to provide public access to static variables.  Class constants: Combine keyword final with keyword static – Example:  public static final int MIN_SCORE = 0;
  • 10. Fundamentals of Java 10 Class (static) Variables and Methods (cont.) Static variables, methods, and constants example
  • 11. Fundamentals of Java 11 Class (static) Variables and Methods (cont.) Static variables, methods, and constants example (cont.)
  • 12. Fundamentals of Java 12 Class (static) Variables and Methods (cont.)  Using class variables example:  Using class constants example:
  • 13. Fundamentals of Java 13 Class (static) Variables and Methods (cont.)  Two rules for using static variables: – Class methods can reference only static variables.  Never instance variables – Instance methods can reference static and instance variables.  The main method for an executable java class is static. – JVM sends main message to start a program.
  • 14. Fundamentals of Java 14 Turtle Graphics  Open-source Java package for drawing – Used in this text to illustrate features of object- oriented programming – Pen used for drawing on a window  StandardPen is a specific type of Pen.
  • 15. Fundamentals of Java 15 Turtle Graphics (cont.) Table 10-1: Pen messages
  • 16. Fundamentals of Java 16 Turtle Graphics (cont.) Example 10.1: Drawing a square using Turtle Graphics
  • 17. Fundamentals of Java 17 Java Interfaces—The Client Perspective  Interface: A list of a class’s public methods – Provides information to use a class without revealing its implementation – When related classes have same interface, they can be used interchangeably in a program.  In Turtle Graphics, Pen is an interface. – StandardPen, WigglePen, and RainbowPen are examples of classes that conform to the Pen interface.
  • 18. Fundamentals of Java 18 Java Interfaces—The Client Perspective (cont.)  A Java interface specifies the method signatures for an interface.
  • 19. Fundamentals of Java 19 Java Interfaces—The Client Perspective (cont.)  An interface is not a class. – But can be used as a data type  Having multiple classes conform to the same interface allows for polymorphic behavior:
  • 20. Fundamentals of Java 20 Java Interfaces—The Client Perspective (cont.)  A class that conforms to an interface is said to implement the interface.  When declaring a variable or parameter, use the interface type when possible. – Methods using interface types are more general. – They are easier to maintain.
  • 21. Fundamentals of Java 21 Java Interfaces—The Implementation Perspective  A class implements an interface using the implements keyword.
  • 22. Fundamentals of Java 22 Java Interfaces – The Implementation Perspective (cont.)  A class that implements an interface must implement every method in the interface.  A variable declared with the interface type can reference objects of any class that implements the interface.
  • 23. Fundamentals of Java 23 Java Interfaces – The Implementation Perspective (cont.) The Circle class
  • 24. Fundamentals of Java 24 Java Interfaces – The Implementation Perspective (cont.) The Circle class (cont.)
  • 25. Fundamentals of Java 25 Java Interfaces – The Implementation Perspective (cont.) Example 10.3: Try out some shapes
  • 26. Fundamentals of Java 26 Java Interfaces – The Implementation Perspective (cont.) Example 10.3: Try out some shapes (cont.)
  • 27. Fundamentals of Java 27 Java Interfaces – The Implementation Perspective (cont.) Figure 10.3: Output from the TestShapes program
  • 28. Fundamentals of Java 28 Java Interfaces – The Implementation Perspective (cont.)  Important interface concepts: – Interface contains only methods, never variables. – Interface methods are usually public. – If more than one class implements an interface, its methods are polymorphic.
  • 29. Fundamentals of Java 29 Java Interfaces – The Implementation Perspective (cont.)  Important interface concepts (cont.): – A class can implement methods in addition to those listed in the interface. – A class can implement more than one interface. – Interfaces can be in an inheritance hierarchy.
  • 30. Fundamentals of Java 30 Code Reuse Through Inheritance  All classes are part of a large class hierarchy. – Object class is the root. – Each class inherits variables and methods of the classes above it in the hierarchy.  New classes can add new variables, add new methods, or alter existing inherited methods. – Class immediately above a class is a superclass. – Any classes that inherit are subclasses.
  • 31. Fundamentals of Java 31 Code Reuse Through Inheritance (cont.)  A class can only have one superclass, but may have many subclasses.  The descendents of a class consist of its subclasses, their subclasses, etc. Figure 10-4: Part of a class hierarchy
  • 32. Fundamentals of Java 32 Code Reuse Through Inheritance (cont.)  A class can inherit the characteristics of another class using the extends keyword. The Wheel class extends the Circle class.
  • 33. Fundamentals of Java 33 Code Reuse Through Inheritance (cont.) The Wheel class extends the Circle class (cont.).
  • 34. Fundamentals of Java 34 Code Reuse Through Inheritance (cont.) Example 10.4: Draw a wheel and a circle.
  • 35. Fundamentals of Java 35 Code Reuse Through Inheritance (cont.) Figure 10-5: A circle and a wheel with the same radius but different positions
  • 36. Fundamentals of Java 36 Code Reuse Through Inheritance (cont.)  Wheel implements Shape because it extends Circle, which implements Shape.  xPos, yPos, and radius inherited by Wheel from Shape – Must modify these instance variables in Shape to be protected rather than private  Protected access modifier means variables are accessible only in current class and its descendents.
  • 37. Fundamentals of Java 37 Code Reuse Through Inheritance (cont.)  Methods may also be protected.  A constructor may call superclass constructor using super();.  To call one of superclass methods:  Overriding: A subclass can modify a superclass method by re-implementing it.
  • 38. Fundamentals of Java 38 Inheritance and Abstract Classes  Abstract class: Provides functionality (methods), but can never be instantiated – Can only be extended – Declared with keyword abstract – May contain standard methods and abstract methods
  • 39. Fundamentals of Java 39 Inheritance and Abstract Classes (cont.)  Abstract method: A method with no body – Defined using keyword abstract – A class with an abstract method will also be abstract. – A subclass of an abstract class must implement every abstract method in that class.  Final methods: Methods that cannot be overridden in a subclass
  • 40. Fundamentals of Java 40 Inheritance and Abstract Classes (cont.)  Partial abstract class/method example:
  • 41. Fundamentals of Java 41  A Java interface has a name and consists of a list of method headers.  One or more classes can implement the same interface.  If a variable is declared as an interface type, it can be associated with an object from any class that implements the interface. Interfaces, Inheritance, and Relationships among Classes
  • 42. Fundamentals of Java 42 Interfaces, Inheritance, and Relationships among Classes (cont.)  If a class implements an interface, then all of its subclasses do so implicitly.  A subclass inherits all of the characteristics of its superclass. – Subclass can add new variables and methods or modify inherited methods.  Characteristics common to several classes can be collected in a common abstract superclass that is never instantiated.
  • 43. Fundamentals of Java 43 Interfaces, Inheritance, and Relationships among Classes (cont.)  An abstract class can contain headers for abstract methods that are implemented in the subclasses.  A class’s constructors and methods can use constructors and methods in the superclass.  Inheritance reduces repetition and promotes the reuse of code.
  • 44. Fundamentals of Java 44 Interfaces, Inheritance, and Relationships among Classes (cont.)  Interfaces and inheritance promote the use of polymorphism.  When a message is sent to an object, Java looks for a matching method. – Search starts in the object’s class and, if necessary, continues up the class hierarchy
  • 45. Fundamentals of Java 45 Interfaces, Inheritance, and Relationships among Classes (cont.)  Four ways in which methods in a subclass can be related to methods in a superclass: – Implementation of an abstract method – Extension – Overriding – Finality  It is possible to work only with abstract classes, forgoing interfaces.
  • 46. Fundamentals of Java 46 Interfaces, Inheritance, and Relationships among Classes (cont.) Figure 10-7: Three types of relationships among classes
  • 47. Fundamentals of Java 47 Acceptable Classes for Parameters and Return Values  If an object of class BBB is expected, it is always acceptable to substitute an object of a subclass. – But never of a superclass  A subclass of BBB inherits all of BBB’s methods.  No guarantees about the methods in the superclass
  • 48. Fundamentals of Java 48 Error Handling with Classes  Before implementing error handling code, must determine error conditions for a class  Preconditions: Describe what must be true before a particular method is called – Values for parameters/instance variables  Postconditions: Describe what must be true after a particular method has been executed – Return values and altered instance variables
  • 49. Fundamentals of Java 49 Error Handling with Classes (cont.)  Example:
  • 50. Fundamentals of Java 50 Exceptions  JVM can throw exceptions when illegal operations are attempted.  Commonly used exception classes:
  • 51. Fundamentals of Java 51 Exceptions (cont.)  Can throw exceptions in methods: – To enforce pre-conditions or any other condition  Syntax:  Example:
  • 52. Fundamentals of Java 52 Exceptions (cont.)  Exceptions to enforce pre-conditions:
  • 53. Fundamentals of Java 53 Exceptions (cont.)  Clients who call such methods need to catch the exceptions so the program does not halt. – Embed method call in a try-catch statement
  • 54. Fundamentals of Java 54 Exceptions (cont.)  A method may throw more than one type of exception. – Client can handle each type explicitly.  Example:
  • 55. Fundamentals of Java 55 Reference Types, Equality, and Object Identity  Aliasing: Two reference variables point to same object  Comparing objects for equality: – Comparing two reference variables using == indicates whether the variables point to the same object. – To compare values of two distinct objects for equality, use the equals method.
  • 56. Fundamentals of Java 56 Reference Types, Equality, and Object Identity (cont.)  equals method: Defined in Object class – Uses == operator by default – A class must override equals to allow for comparison of object’s contents.  Example:
  • 57. Fundamentals of Java 57 Reference Types, Equality, and Object Identity (cont.)  Copying objects: – Aliasing is not copying. – Use a copy constructor: – Implement Clonable:
  • 58. Fundamentals of Java 58 Summary  Class (static) variables provide storage for data that all instances of a class can access but do not have to own separately.  Class (static) methods are written primarily for class variables.  An interface specifies a set of methods that implementing classes must include. – Gives clients enough information to use a class
  • 59. Fundamentals of Java 59 Summary (cont.)  Polymorphism and inheritance reduce the amount of code that must be written by servers and learned by clients.  Classes that extend other classes inherit their data and methods.  Methods in different classes that have the same name are polymorphic.
  • 60. Fundamentals of Java 60 Summary (cont.)  Abstract classes are not instantiated. – Help organize related subclasses – Contain their common data and methods  Error handling can be distributed among methods and classes by using preconditions, postconditions, and exceptions.
  • 61. Fundamentals of Java 61 Summary (cont.)  Because of the possibility of aliasing, the programmer should provide: – equals method for comparing for equality – clone method for creating a copy of an object