Abstract Classes and Interfaces When is a class

  • Slides: 14
Download presentation
Abstract Classes and Interfaces When is a class not a class? When it is

Abstract Classes and Interfaces When is a class not a class? When it is either abstract or an Interface

Abstract Classes �As the name indicates, an abstract class is abstract. �If we think

Abstract Classes �As the name indicates, an abstract class is abstract. �If we think of a class like a blueprint, an abstract class is like a blueprint with a hole in it to be filled in by the Child classes. �Something it can be difficult to decide whether a class is abstract but the question to ask is whether a method in the class is not well defined and whether you want instances of that class in the program.

Examples �Animal – While animals do exist, we never think of there just being

Examples �Animal – While animals do exist, we never think of there just being an animal. Someone would never tell you that they adopted an animal �Vehicle – How does a vehicle move? You would need a more specific type of vehicle to define that method such as a car, plane, or ship �Electronic Device – What happens when you turn the device on? All electronic devices can be turned on but it all depends on what the specific device is.

How does it work �You can make a class abstract by declaring it in

How does it work �You can make a class abstract by declaring it in the following way: public abstract class My. Class �You can do the same to a method by declaring it in the following way: public abstract void my. Method( ); �Notice that there is no code provided for the abstract method. �Once you make a method abstract then the entire class is made abstract.

Child Classes �If a class extends an abstract class then it will have to

Child Classes �If a class extends an abstract class then it will have to define each abstract method in the parent class, unless it itself is abstract.

public class Parent{ protected int x; public Parent(int x){ this. x = x; }

public class Parent{ protected int x; public Parent(int x){ this. x = x; } public abstract void do. It(int n){ } }

public class Child extends Parent{ private int a; public Child(int a, int x){ super(x);

public class Child extends Parent{ private int a; public Child(int a, int x){ super(x); this. a = a; } public void do. It(int n){ a *= n; x += n; } }

Some things to note �Notice that the Parent class was not defined as abstract

Some things to note �Notice that the Parent class was not defined as abstract but the do. It method was so the entire Parent class by default becomes abstract. �The Child class defined the do. It method that was the abstract method in the Parent class.

Interfaces �JAVA does not allow multiple inheritance where a class extends more than one

Interfaces �JAVA does not allow multiple inheritance where a class extends more than one other class. �JAVA will though allow you to cheat and say that you implement multiple Interfaces. �An Interface is essentially an abstract class where every single method is abstract and it contains no variables. �This prevents conflict between variables and methods that exist in both parent classes.

Logistics �No variables can be declared in an Interface �No methods can have code

Logistics �No variables can be declared in an Interface �No methods can have code in them in an Interface �It is said that a class implements and interface, not that it extends it (the keyword implements is used)

public interface Parent { //notice the semicolons at //the ends of these signatures void

public interface Parent { //notice the semicolons at //the ends of these signatures void method 1( ); void method 2( ); int method 3(double d); }

public class Child implements Parent { public int statevar 1; public void method 1(

public class Child implements Parent { public int statevar 1; public void method 1( ) { //some code…} public void method 2( ) { //some code…} public int method 3(double c ) { //some code…} //some other methods… }

Some things to note �When a class implements an Interface then it must declare

Some things to note �When a class implements an Interface then it must declare each and every method in the Interface even if they do nothing (they have to be at least defined as doing nothing) �A class can only extend one other class but can implement an infinite number of Interfaces. For example: public class Game extends JPanel implements Mouse. Listener, Action. Listener, Key. Listener

Declaring and Storing Interfaces �You can declare an Interface or a Abstract Class or

Declaring and Storing Interfaces �You can declare an Interface or a Abstract Class or a Data Structure containing Interfaces or an Abstract Class just like any regular class. In this example, assume that Animal is an Abstract Class and Dog and Cat are classes that extend it: Animal rex = new Dog(); Array. List<Animal> zoo = new Array. List<Animal>(); zoo. add(new Cat()); �The same could work if Animal is an Interface and Dog and Cat implement it.