1 / 31

Abstract Class and Packages from Chapter 9

Abstract Class and Packages from Chapter 9. Lecture. Review. Abstract classes Using final with inheritance Packages Access protection Importing packages. Abstract Classes.

lorie
Télécharger la présentation

Abstract Class and Packages from Chapter 9

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Abstract Class and Packages from Chapter 9 Lecture

  2. Review • Abstract classes • Using final with inheritance • Packages • Access protection • Importing packages

  3. Abstract Classes • Sometimes, 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.

  4. More about Abstract Classes • The 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)

  5. Example –page 217

  6. Explanation • In 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.

  7. Final • Final with inheritance has three uses. • First, it can be used to create the equivalent of a named constant • Secondly, to apply final to prevent overriding • Thirdly, to use final to prevent inheritance

  8. 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.

  9. Example – with a compilation error Because show() is declared as final, it cannot be overridden. error

  10. 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.

  11. Example – with a compilation error error

  12. 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 statement • Any number of import statements • A single public class declaration • Any number of classes private to the package

  13. Create a package • To 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

  14. Package Hierarchy • The package hierarch is: package java.awt.image; • It access the package in java/awt/image directory

  15. Manual Method Example (1) • Make a directory called YourPackage under your current directory mkdir YourPackage • Use a notepad to create a file as follows and put it under the directory of YourPackage

  16. Example (2) • Compile it YourPackage

  17. Example (3) input: 4, output 4*4 = 16

  18. More Example (1) • Create a file on left-hand side. • Put it into a directory called Mypack • Compile it

  19. More Example (2) The Class Path

  20. Creating Packages Using Eclipse

  21. Snap for Creating Package

  22. Viewing the newly created Package

  23. Access Protection • In Java, there are at least three control mechanism. • private, public and protected • Package adds another dimension to access control. • Java provides many levels of protection over the visibility of variables and methods within classes, subclasses and packages

  24. 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)

  25. Examples of Importing Packages import java.util.Date • (import Date Package undre java, utility) import java.io.* • (import IO packages under java)

  26. Example (1) Your Class is created under YourPackage

  27. Example (2) • Create a file using import YourPackage.* (it will import any classes under this directory)

  28. Your Class under YourPackage

  29. More Example class add

  30. More Example - Explanation • Create a add class which has add and sub methods • Create a lecture108 and import the add class • Pass the value from agrs[0] and pass to add class to perform add and sub. Here, the add class consists of add() and sub() method

  31. Summary • Abstract classes –used as a family • Using final with inheritance –the last method • Packages –create class path • Access protection –private, public protect (Packages) • Importing packages –import other classes

More Related