Abstract Classes
Abstract classes are used in cases when you want to define an abstraction with some common functionality.
why should I use an abstract class?
- define an abstraction and leave concrete implementation details to subclasses.
- ability to offer default implementation
- perfect placeholder for common functionality required
abstract class Shape {
public double area() { return 0; } // default implementation
// other members
}
public abstract double area(); // note: no implementation (i.e., no method body definition)
public abstract double area() { return 0; } // compiler error!
abstract class Shape {
public abstract double area(); // no implementation
// other members
}
class Rectangle extends Shape { }
class Rectangle extends Shape {
private int length, height;
public double area() { return length * height; }
// other members ...
}
- The abstract keyword can be applied to a class or a method but not to a field.
- An abstract class cannot be instantiated.
- An abstract class can extend another abstract class or can implement an interface.
- An abstract class can be derived from a concrete class.
- An abstract c lass need not declare an abstract method. if a class has an abstract method, it should be declared as an abstract class.
- A subclass of an abstract class needs to provide implementation of all the abstract methods; otherwise you need to declare that subclass as an abstract class.
- An abstract class may have methods or fields declared static.
the “final” Keyword
Final Classes
A final class is a non-inheritable class, if you declare a class as final, you cannot subclass it.
OOP suggests that a class should be open for extension but closed for modification (Open/Closed Principle).
Two important reasons
- To prevent a behavior change by subclassing
- Improved performance
for example
String (java.lang.String) and System (java.lang.System)
Final Methods and Variables
The final method cannot be overridden
public abstract class Shape {
//class members...
final public void setParentShape(Shape shape){
//method body
}
public Shape getParentShape(){
//method body
}
}
Final variables can be assigned only once. If you try to change a final variable after initialization, you will get a complaint from your Java compiler.
Points to Remember
- The final modifier can be applied to a class, method, or variable. All methods of a final class are implicitly final (hence non-overridable).
- A final variable can be assigned only once. If a variable declaration defines a variable as final but did not initialize it, then it is referred to as blank final.
- The keyword final can even be applied to parameters. The value of a final parameter
- cannot be changed once assigned.
Using the “static” Keyword
- A static variable is associated with its class rather than its object; hence they are known as class variables.
- A static variable is initialized only once when execution of the program starts.
- A static variable shares its state with all instances of the class.
- You access a static variable using its class name (instead of an instance).
Static Block
This static block will be executed by JVM when it loads the class into memory.
Points to Remember
- you cannot override static methods, and runtime polymorphism is not possible with static methods.
- A static method cannot use the this keyword in its body.
- A static method cannot use the super keyword in its body
- Since static methods cannot access instance variables (non-static variables), they are most suited for utility functions.
Nested Classes
Classes defined within the body of another class (or interface) are known as nested classes.
Benefits of nested classes
- you can put related classes together as a single logical group.
- nested classes can access all class members of the enclosing class
- anonymous inner classes are useful for writing simpler event-handling code with AWT/Swing
There are four types or flavors of nested classes in Java:
- Static nested class
- Inner class
- Local inner class
- Anonymous inner class
Anonymous Inner Classes
an anonymous inner class does not have a name. The declaration of the class automatically derives from the instance-creation expression.
Points to Remember
- Anonymous classes are defined in the new expression itself, so you cannot create multiple objects of an anonymous class.
- You cannot explicitly extend a class or explicitly implement interfaces when defining an anonymous class.
Enum Data Types
where you want to restrict the user to providing input from a predefined list.
// define an enum for classifying printer types
enum PrinterType {
DOTMATRIX, INKJET, LASER
}
// test the enum now
public class EnumTest {
PrinterType printerType;
public EnumTest(PrinterType pType) {
printerType = pType;
}
public void feature() {
// switch based on the printer type passed in the constructor
switch(printerType){
case DOTMATRIX:
System.out.println("Dot-matrix printers are economical and almost obsolete");
break;
case INKJET:
System.out.println("Inkjet printers provide decent quality prints");
break;
case LASER:
System.out.println("Laser printers provide best quality prints");
break;
}
}
public static void main(String[] args) {
EnumTest enumTest = new EnumTest(PrinterType.LASER);
enumTest.feature();
}
}
you can define methods or fields in an enum definition
public enum PrinterType {
DOTMATRIX(5), INKJET(10), LASER(50);
private int pagePrintCapacity;
private PrinterType(int pagePrintCapacity) {
this.pagePrintCapacity = pagePrintCapacity;
}
public int getPrintPageCapacity() {
return pagePrintCapacity;
}
}
Points to Remember
Enums are implicitly declared public, static, and final, which means you cannot extend them.
No comments:
Post a Comment