1 abstract class, packages and interface from chapter 9 lecture 10
TRANSCRIPT
![Page 1: 1 Abstract Class, Packages and interface from Chapter 9 Lecture 10](https://reader035.vdocuments.us/reader035/viewer/2022062417/551c5807550346a5458b508f/html5/thumbnails/1.jpg)
1
Abstract Class, Packages aAbstract Class, Packages and interface nd interface from Chapter 9from Chapter 9
Abstract Class, Packages aAbstract Class, Packages and interface nd interface from Chapter 9from Chapter 9
Lecture 10Lecture 10
![Page 2: 1 Abstract Class, Packages and interface from Chapter 9 Lecture 10](https://reader035.vdocuments.us/reader035/viewer/2022062417/551c5807550346a5458b508f/html5/thumbnails/2.jpg)
2
Review
Abstract classesUsing final with inheritancePackagesAccess protectionImporting packages
![Page 3: 1 Abstract Class, Packages and interface from Chapter 9 Lecture 10](https://reader035.vdocuments.us/reader035/viewer/2022062417/551c5807550346a5458b508f/html5/thumbnails/3.jpg)
3
Abstract ClassesSometimes, we want to define a superclass that declares the structure of a abstraction with a complete implementation of every method.That is, we want to create a superclass that only defines a generalised form that will be shared by all of its subclasses.
![Page 4: 1 Abstract Class, Packages and interface from Chapter 9 Lecture 10](https://reader035.vdocuments.us/reader035/viewer/2022062417/551c5807550346a5458b508f/html5/thumbnails/4.jpg)
4
More about Abstract ClassesThe main purpose of an abstract class is to define a common interface for it's subclasses to which some or all implementation is deferred. Abstract classes may be used in designing a family of related subclasses. The general form is:abstract type name (parameter-list)
![Page 5: 1 Abstract Class, Packages and interface from Chapter 9 Lecture 10](https://reader035.vdocuments.us/reader035/viewer/2022062417/551c5807550346a5458b508f/html5/thumbnails/5.jpg)
5
Example of AbstractIn an object-oriented drawing application, we can draw circles, lines, squares, and so on. Each of these graphic objects share certain states (position), and behavior (move, draw).You can take advantage of these similarities and declare them all to inherit from the same parent Graphic Object.
Graphic Object
circles lines square
![Page 6: 1 Abstract Class, Packages and interface from Chapter 9 Lecture 10](https://reader035.vdocuments.us/reader035/viewer/2022062417/551c5807550346a5458b508f/html5/thumbnails/6.jpg)
6
Example of Graphic Objectabstract class GraphicObject { int x, y; . . . void moveTo(int newX, int newY) { . . . } abstract void draw();}
. . . }}class circle extends GraphicObject { void draw() { . . . }}
![Page 7: 1 Abstract Class, Packages and interface from Chapter 9 Lecture 10](https://reader035.vdocuments.us/reader035/viewer/2022062417/551c5807550346a5458b508f/html5/thumbnails/7.jpg)
7
Example –page 217
![Page 8: 1 Abstract Class, Packages and interface from Chapter 9 Lecture 10](https://reader035.vdocuments.us/reader035/viewer/2022062417/551c5807550346a5458b508f/html5/thumbnails/8.jpg)
8
ExplanationIn object-oriented programming, we want to model an abstract concept without being able to create an instance of it. Here, class A defines an abstract class callme() {This class also supports a concrete method callme1(). }callme() is implemented in Class B.
![Page 9: 1 Abstract Class, Packages and interface from Chapter 9 Lecture 10](https://reader035.vdocuments.us/reader035/viewer/2022062417/551c5807550346a5458b508f/html5/thumbnails/9.jpg)
9
Final
Final with inheritance has three uses.First, it can be used to create the equivalent of a named constantSecondly, to apply final to prevent overridingThirdly, to use final to prevent inheritance
![Page 10: 1 Abstract Class, Packages and interface from Chapter 9 Lecture 10](https://reader035.vdocuments.us/reader035/viewer/2022062417/551c5807550346a5458b508f/html5/thumbnails/10.jpg)
10
Using final to prevent overriding
Overriding is one of java’s most powerful features, there will be times when we will want to prevent it from occurring. To disallow a method from being overridden, we specify final as a modifier at the start of its declaration.
![Page 11: 1 Abstract Class, Packages and interface from Chapter 9 Lecture 10](https://reader035.vdocuments.us/reader035/viewer/2022062417/551c5807550346a5458b508f/html5/thumbnails/11.jpg)
11
Example – with a compilation error
Because show() is declared as final,
it cannot be overridden.
![Page 12: 1 Abstract Class, Packages and interface from Chapter 9 Lecture 10](https://reader035.vdocuments.us/reader035/viewer/2022062417/551c5807550346a5458b508f/html5/thumbnails/12.jpg)
12
Using final to prevent inheritance
Sometimes, we want to prevent a class from being inherited.To do this, we can precede the class declaration with final.Declaring a class as final implies that all of its methods as final.
![Page 13: 1 Abstract Class, Packages and interface from Chapter 9 Lecture 10](https://reader035.vdocuments.us/reader035/viewer/2022062417/551c5807550346a5458b508f/html5/thumbnails/13.jpg)
13
Example – with a compilation error
![Page 14: 1 Abstract Class, Packages and interface from Chapter 9 Lecture 10](https://reader035.vdocuments.us/reader035/viewer/2022062417/551c5807550346a5458b508f/html5/thumbnails/14.jpg)
14
Package Packages are one of the basic components of a Java Program. In general, a Java source can contain any of the following:A single package statementAny number of import statementsA single public class declarationAny number of classes private to the package
![Page 15: 1 Abstract Class, Packages and interface from Chapter 9 Lecture 10](https://reader035.vdocuments.us/reader035/viewer/2022062417/551c5807550346a5458b508f/html5/thumbnails/15.jpg)
15
Create a packageTo create a package is easy. The user simply includes a package command as the first statement in a Java source file.The package statement defines a name space in which classes are package, which has no name. The format is:package pkg; Name of the
package (directory), not
class
![Page 16: 1 Abstract Class, Packages and interface from Chapter 9 Lecture 10](https://reader035.vdocuments.us/reader035/viewer/2022062417/551c5807550346a5458b508f/html5/thumbnails/16.jpg)
16
Package Hierarchy
The package hierarch is:package java.awt.image;
It access the package in java/awt/image
![Page 17: 1 Abstract Class, Packages and interface from Chapter 9 Lecture 10](https://reader035.vdocuments.us/reader035/viewer/2022062417/551c5807550346a5458b508f/html5/thumbnails/17.jpg)
17
Example (1)Make a directory called YourPackage under your current directorymkdir YourPackage
Use a notepad to create a file as follows and put it under the directory of YourPackage
![Page 18: 1 Abstract Class, Packages and interface from Chapter 9 Lecture 10](https://reader035.vdocuments.us/reader035/viewer/2022062417/551c5807550346a5458b508f/html5/thumbnails/18.jpg)
18
Example (2)Compile it
![Page 19: 1 Abstract Class, Packages and interface from Chapter 9 Lecture 10](https://reader035.vdocuments.us/reader035/viewer/2022062417/551c5807550346a5458b508f/html5/thumbnails/19.jpg)
19
Example (3)
![Page 20: 1 Abstract Class, Packages and interface from Chapter 9 Lecture 10](https://reader035.vdocuments.us/reader035/viewer/2022062417/551c5807550346a5458b508f/html5/thumbnails/20.jpg)
20
More Example (1)
Create a file on left-hand side.Put it into a directory called MypackCompile it
![Page 21: 1 Abstract Class, Packages and interface from Chapter 9 Lecture 10](https://reader035.vdocuments.us/reader035/viewer/2022062417/551c5807550346a5458b508f/html5/thumbnails/21.jpg)
21
More Example (2)
![Page 22: 1 Abstract Class, Packages and interface from Chapter 9 Lecture 10](https://reader035.vdocuments.us/reader035/viewer/2022062417/551c5807550346a5458b508f/html5/thumbnails/22.jpg)
22
Access ProtectionIn Java, there are at least three control mechanism.private, public and protectedPackage adds another dimension to access control. Java provides many levels of protection over the visibility of variables and methods within classes, subclasses and packages
![Page 23: 1 Abstract Class, Packages and interface from Chapter 9 Lecture 10](https://reader035.vdocuments.us/reader035/viewer/2022062417/551c5807550346a5458b508f/html5/thumbnails/23.jpg)
23
Importing Packages
Java includes the import statement to bring certain classes or entire packages into visibility.Once imported, a class can be referred to directly, using only its name. The format is:import pkg1[.pkg2](classname)
![Page 24: 1 Abstract Class, Packages and interface from Chapter 9 Lecture 10](https://reader035.vdocuments.us/reader035/viewer/2022062417/551c5807550346a5458b508f/html5/thumbnails/24.jpg)
24
Examples of Importing Packages
import java.util.Date(import Date Package undre java, utility)
import java.io.*(import IO packages under java)
![Page 25: 1 Abstract Class, Packages and interface from Chapter 9 Lecture 10](https://reader035.vdocuments.us/reader035/viewer/2022062417/551c5807550346a5458b508f/html5/thumbnails/25.jpg)
25
Example (1)
![Page 26: 1 Abstract Class, Packages and interface from Chapter 9 Lecture 10](https://reader035.vdocuments.us/reader035/viewer/2022062417/551c5807550346a5458b508f/html5/thumbnails/26.jpg)
26
Example (2)Create a file using import YourPackage.* (it will import any classes under this directory)
![Page 27: 1 Abstract Class, Packages and interface from Chapter 9 Lecture 10](https://reader035.vdocuments.us/reader035/viewer/2022062417/551c5807550346a5458b508f/html5/thumbnails/27.jpg)
27
Your Class under YourPackage
![Page 28: 1 Abstract Class, Packages and interface from Chapter 9 Lecture 10](https://reader035.vdocuments.us/reader035/viewer/2022062417/551c5807550346a5458b508f/html5/thumbnails/28.jpg)
28
More Example
![Page 29: 1 Abstract Class, Packages and interface from Chapter 9 Lecture 10](https://reader035.vdocuments.us/reader035/viewer/2022062417/551c5807550346a5458b508f/html5/thumbnails/29.jpg)
29
More Example - ExplanationCreate a add class which has add and sub methodsCreate a lecture108 and import the add classPass the value from agrs[0] and pass to add class to perform add and sub.
![Page 30: 1 Abstract Class, Packages and interface from Chapter 9 Lecture 10](https://reader035.vdocuments.us/reader035/viewer/2022062417/551c5807550346a5458b508f/html5/thumbnails/30.jpg)
30
SummaryAbstract classes – used as a familyUsing final with inheritance – the last methodPackages – create class pathAccess protection – private, public protect (Packages)Importing packages – import other classes