Download presentation
Presentation is loading. Please wait.
Published byOlivia Carson Modified over 9 years ago
1
When is a class not a class? When it is either abstract or an Interface
2
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.
3
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.
4
You can make a class abstract by declaring it in the following way: public abstract class MyClass You can do the same to a method by declaring it in the following way: public abstract void myMethod( ); Notice that there is no code provided for the abstract method. Once you make a method abstract then the entire class is made abstract.
5
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.
6
public class Parent{ protected int x; public Parent(int x){ this.x = x; } public abstract void doIt(int n){ }
7
public class Child extends Parent{ private int a; public Child(int a, int x){ super(x); this.a = a; } public void doIt(int n){ a *= n; x += n; }
8
Notice that the Parent class was not defined as abstract but the doIt method was so the entire Parent class by default becomes abstract. The Child class defined the doIt method that was the abstract method in the Parent class.
9
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.
10
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)
11
public interface Parent { //notice the semicolons at //the ends of these signatures void method1( ); void method2( ); int method3(double d); }
12
public class Child implements Parent { public int statevar1; public void method1( ) { //some code…} public void method2( ) { //some code…} public int method3(double c ) { //some code…} //some other methods… }
13
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 MouseListener, ActionListener, KeyListener
14
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(); ArrayList zoo = new ArrayList (); zoo.add(new Cat()); The same could would work if Animal is an Interface and Dog and Cat implement it.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.