The document discusses abstract classes in Java. It explains that abstract classes can define methods without implementations, requiring subclasses to implement these abstract methods. It provides an example of an abstract Triangle class that declares an abstract area() method, meaning any subclass of Triangle must provide an implementation of area(). The document also notes that abstract classes can include implemented methods and that abstract classes cannot be instantiated but can be used to create object references.
The document discusses abstract classes in Java. It explains that abstract classes can define methods without implementations, requiring subclasses to implement these abstract methods. It provides an example of an abstract Triangle class that declares an abstract area() method, meaning any subclass of Triangle must provide an implementation of area(). The document also notes that abstract classes can include implemented methods and that abstract classes cannot be instantiated but can be used to create object references.
• There are situations in which you will want to define a superclass that declares the structure of a given abstraction without providing a complete implementation of every method. • That is, sometimes you will want to create a superclass that only defines a generalized form that will be shared by all of its subclasses, leaving it to each subclass to fill in the details. • Such a class determines the nature of the methods that the subclasses must implement. • One way this situation can occur is when a superclass is unable to create a meaningful implementation for a method. • You may have methods that must be overridden by the subclass in order for the subclass to have any meaning. • Consider the class Triangle. It has no meaning if area( ) is not defined. In this case, you want some way to ensure that a subclass does, indeed, override all necessary methods. Java’s solution to this problem is the abstract method. • To declare an abstract method, use this general form: • abstract type name(parameter-list); • As you can see, no method body is present. • Any class that contains one or more abstract methods must also be declared abstract. • To declare a class abstract, you simply use the abstract keyword in front of the class keyword at the beginning of the class declaration. • Any subclass of an abstract class must either implement all of the abstract methods in the superclass, or be declared abstract itself. • Notice that no objects of class A are declared in the program. It is not possible to instantiate an abstract class. • One other point: class A implements a concrete method called callmetoo( ). This is perfectly acceptable. • Abstract classes can include as much implementation as they see fit. • Although abstract classes cannot be used to instantiate objects, they can be used to create object references. • Using an abstract class, you can improve the Figure class shown earlier. • Since there is no meaningful concept of area for an undefined two-dimensional figure, the following version of the program declares area( ) as abstract inside Figure. • This, of course, means that all classes derived from Figure must override area( ). Packages and Interfaces • Packages are containers for classes. They are used to keep the class name space compartmentalized. • For example, a package allows you to create a class named List, which you can store in your own package without concern that it will collide with some other class named List stored elsewhere. Defining a Package • To create a package is quite easy: simply include a package command as the first statement in a Java source file. • Any classes declared within that file will belong to the specified package. • Call this file AccountBalance.java and put it in a directory called mypack. • Next, compile the file. Make sure that the resulting .class file is also in the mypack directory. Then, try executing the AccountBalance class, using the following command line: • java mypack.AccountBalance • Advantage of Java Package 1) Java package is used to categorize the classes and interfaces so that they can be easily maintained. 2) Java package provides access protection. 3) Java package removes naming collision. • There are two types of packages available in Java. 1. Built-in packages Built-in packages are already defined in java API. For example: java.util, java.io, java.lang, java.awt, java.applet, java.net, etc. 2. User defined packages The package we create according to our need is called user defined package. • There are three ways to access the package from outside the package. – import package.*; – import package.classname; – fully qualified name. • Using packagename.* • If you use package.* then all the classes and interfaces of this package will be accessible but not subpackages. • The import keyword is used to make the classes and interface of another package accessible to the current package. Packages and Member Access
• Anything declared public can be accessed from different classes and
different packages. • Anything declared private cannot be seen outside of its class. When a member does not have an explicit access specification, it is visible to subclasses as well as to other classes in the same package. This is the default access. • If you want to allow an element to be seen outside your current package, but only to classes that subclass your class directly, then declare that element protected. Interfaces • Using interface, you can specify what a class must do, but not how it does it. • Interfaces are syntactically similar to classes, but they lack instance variables, and, as a general rule, their methods are declared without any body. • In practice, this means that you can define interfaces that don’t make assumptions about how they are implemented. • Once it is defined, any number of classes can implement an interface. Also, one class can implement any number of interfaces. Defining an Interface
When no access modifier is included, then default access results, and
the interface is only available to other members of the package in which it is declared. When it is declared as public, the interface can be used by any other code. • JDK 8 added a feature to interface that made a significant change to its capabilities. Prior to JDK 8, an interface could not define any implementation whatsoever. • This is the type of interface that the preceding simplified form shows, in which no method declaration supplies a body. • Thus, prior to JDK 8, an interface could define only “what,” but not “how.” JDK 8 changed this. Beginning with JDK 8, it is possible to add a default implementation to an interface method. • Furthermore, JDK 8 also added static interface methods, and beginning with JDK 9, an interface can include private methods. Thus, it is now possible for interface to specify some behavior. • However, such methods constitute what are, in essence, special-use features, and the original intent behind interface still remains. The relationship between classes and interfaces
• As shown in the figure above, a class extends
another class, an interface extends another interface, but a class implements an interface. Exception Handling • An exception is an event, which occurs during the execution of a program, that disrupts the normal flow of the program's instructions. • In Java, all exceptions are represented by classes. • All exception classes are derived from a class called Throwable. • Thus, when an exception occurs in a program, an object of some type of exception class is generated. • Java exception handling is managed via five keywords: try, catch, throw, throws, and finally. • Program statements that you want to monitor for exceptions are contained within a try block. • Your code can catch this exception (using catch) and handle it in some rational manner. • To manually throw an exception, use the keyword throw. • If a method generates an exception that it does not handle, it must declare that exception in a throws clause. • Any code that absolutely must be executed after a try block completes is put in a finally block. • When an exception is thrown, it is caught by its corresponding catch statement, which then processes the exception. • As the general form shows, there can be more than one catch statement associated with a try. • The type of the exception determines which catch statement is executed. • That is, if the exception type specified by a catch statement matches that of the exception, then that catch statement is executed (and all others are bypassed). • If no exception is thrown, then a try block ends normally, and all of its catch statements are bypassed. Execution resumes with the first statement following the last catch. • Thus, catch statements are executed only if an exception is thrown. Example