1 / 14

Chapter 14 Abstract Classes and Interfaces

Chapter 14 Abstract Classes and Interfaces. 1. What is abstract class?. Abstract class is just like other class, but it marks with abstract keyword. In abstract class, methods that we want to be overridden in its subclass must mark with

lbarger
Download Presentation

Chapter 14 Abstract Classes and Interfaces

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. Chapter 14 Abstract Classes and Interfaces 1

  2. What is abstract class? Abstract class is just like other class, but it markswith abstract keyword. In abstract class, methods that we want to be overridden in its subclass must mark with abstract too. Moreover, those methods mustnot contain any code. However, abstract class can have normalproperties, constructors, and other methods. 2

  3. How to make aclass to beabstract (1) Here is an example: public abstract class Shape { private String color; public Shape(){} public String getColor() return color; } public void setColor(String this.color = color; } public abstract double getArea(); public abstract double getPerimeter(); }

  4. How to make aclass to beabstract (2) • And then in subclass,themethod that mark with abstract keyword, it will automatically request without any excuse. public class Circle extends Shape{ private double radius public Circle(){} @Override public double getArea(){ to be override return radius*radius*Math.PI; } @Override public double getPerimeter(){ return 2*radius*Math.PI; } }

  5. How to use abstract class? (1) • You can use an abstract class by extends keyword. inheriting it using public } class Circle extends Shape { • Abstract class can also be a type. Shape sh;//Shape is a type of sh variable • Because abstract class canalso be a type, we can use polymorphism as well. Shape sh = new Circle(); sh.getArea();

  6. How to use abstract class? (2) • You CANNOT create instances of abstract classes using the new operator. Shape shape = new Shape();// Compile Error • We can make an abstract class bynot making any method abstract also. Thereis no any error. public abstract class Shape { public String getColor(){ return ""; } }

  7. Importance of abstract class • Abstract class is always a superclass. It means when you make an abstract class, you have to think that the class must be a superclass later. Abstract class is the way to guarantee that its closed subclasses MUST override abstract methods. The only reason that we have to make abstract class is because of polymorphism. It makes no sense if we make abstract class, but we don’t use any polymorphism. • • •

  8. Interfaces What is an interface? Why is an interface useful? How do you define an interface? How do you use an interface? 8

  9. What is an interface? Why is an interface useful? An interface is a classlike construct that contains only constants and abstract methods. In many ways, an interface is similar to an abstract class, but the intent of an interface is to specify behavior for objects. For example, you can specify that the objects are comparable, edible, cloneable using appropriate interfaces. 9

  10. Define an Interface To distinguish an interface from a class, Java uses the following syntax to define an interface: public interface InterfaceName { constant declarations; method signatures; } Example: public interface Edible { /** Describe how to eat */ public abstract String howToEat(); } 10

  11. Interface is a Special Class An interface is treated like a special class in Java. Each interface is compiled into a separate bytecode file, just like a regular class. Like an abstract class, you cannot create an instance from an interface using the new operator, but in most cases you can use an interface more or less the same way you use an abstract class. For example, you can use an interface as a data type for a variable, as the result of casting, and so on. 11

  12. Example You can now use the Edible interface to specify whether an object is edible. This is accomplished by letting the class for the object implement this interface using the implements keyword. For example, the classes Chicken implement the Edible interface. 12

  13. Omitting Modifiers in Interfaces All data fields are publicfinalstatic and all methods are publicabstractin an interface. For this reason, these modifiers can be omitted, as shown below: A constant defined in an interface can be accessed using syntax InterfaceName.CONSTANT_NAME (e.g., T1.K). 13

  14. Interfaces vs. Abstract Classes In an interface, the data must be constants; an abstract class can have all types of data. Each method in an interface has only a signature without implementation; an abstract class can have concrete methods. 14

More Related