Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 14 Abstract Classes and Interfaces

Similar presentations


Presentation on theme: "Chapter 14 Abstract Classes and Interfaces"— Presentation transcript:

1 Chapter 14 Abstract Classes and Interfaces

2 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 abstract too. Moreover, those methods must not contain any code. However, abstract class can have normal properties, constructors, and other methods. 2

3 public abstract class Shape {
How to make a class to be abstract (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 a class to be abstract (2)
And then in subclass, the method 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) public class Circle extends Shape {
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 can also be a type, we can use polymorphism as well. Shape sh = new Circle(); sh.getArea();

6 public abstract class Shape { public String getColor(){ return ""; } }
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 by not making any method abstract also. There is 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 Example:
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 public final static and all methods are public abstract in 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. Variables Constructors Methods Abstract class No restrictions Constructors are invoked by subclasses through constructor chaining. An abstract class cannot be instantiated using the new operator. No restrictions. Interface All variables must be public static final No constructors. An interface cannot be instantiated using the new operator. All methods must be public abstract instance methods 14


Download ppt "Chapter 14 Abstract Classes and Interfaces"

Similar presentations


Ads by Google