|
| 1 | +Java is one of the most popular OOP (Object oriented programming) languages in the world. |
| 2 | +Abstrat classes and Interfaces is one of the most important concept in OOP,which is used for defining abstract objects and concepts. |
| 3 | +They are very simlar but also different. |
| 4 | + |
| 5 | +# Abstract Class |
| 6 | +We all know in OOP,everything is 'Object', all of the 'Object' are decribed using classes, however, not all classes can be used to describe |
| 7 | +a Object. If there are lack of information for initiating a specific Object,additional specific class is required to support it, then this |
| 8 | +class is called abstract class. like a new instance new Animal(),we call know there is a animal obj has been created,but we have no idea |
| 9 | +how this Animal look like, it has no specific concept except Animal, therefore, it is a abstract class, the specific class is required like |
| 10 | +cat,dog.. to do specific obj initiating. |
| 11 | + |
| 12 | +Since abstract concept has no specific obj in OOP,therefore abstract class cannot be instantiated reference because of the abstract concept. |
| 13 | +There are few important things need to keep in mind when designing your abstract class |
| 14 | +1. abstract class cannot be instantiated,the instantiation should be done by its subclasses, abstract class only need a reference. |
| 15 | +2. absratct method must be in the abstract class,it cannot have body and has to be overwritten by its subclass. |
| 16 | +3. absctract class can have none abstract method at all. |
| 17 | +4. abstract method in its subclass cannot be same name as its super class's abstract method. |
| 18 | +5. inheritance must happen,thus final key word cannot be modifier for the super class. |
| 19 | +6. abstract method cannot be declared using private,static,final. |
| 20 | + |
| 21 | +Example. |
| 22 | +public abstract class Animal { |
| 23 | + public abstract void cry(); // no method body, waiting to be overwritten |
| 24 | +} |
| 25 | + |
| 26 | +public class Cat extends Animal{ |
| 27 | + |
| 28 | + @Override |
| 29 | + public void cry() { |
| 30 | + System.out.println("cat crying"); |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +public class Dog extends Animal{ |
| 35 | + |
| 36 | + @Override |
| 37 | + public void cry() { |
| 38 | + System.out.println("dog crying"); |
| 39 | + } |
| 40 | + |
| 41 | +} |
| 42 | + |
| 43 | +public class Test { |
| 44 | + |
| 45 | + public static void main(String[] args) { |
| 46 | + Animal a1 = new Cat(); |
| 47 | + Animal a2 = new Dog(); |
| 48 | + |
| 49 | + a1.cry(); |
| 50 | + a2.cry(); |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +------ |
| 55 | +output: |
| 56 | +cat crying |
| 57 | +dog crying |
| 58 | + |
| 59 | +# Interfaces |
| 60 | +Interface is a 'class' that is more abstract than abstract classes, interface is not a class,becasue it cannot be Instantiated,because |
| 61 | +if we do new Interface(), it will not compile. |
| 62 | + |
| 63 | +Interface is used to build the protocol between classes, it is only a style with no instantiation. If a class implements a interface, |
| 64 | +it must override all of its method. |
| 65 | +Interface is an extension of the abstract class,java does not support multi-inheritance,meaning one class can only inherite from only one |
| 66 | +class,but interface is different,one class can implements multiple interfaces,this somehow acchieved the 'multi-inheritance' in java. |
| 67 | + |
| 68 | +Be aware of these when using interfaces |
| 69 | +1. Method in the interfaces must be declared public. |
| 70 | +2. Field could exit inside interfaces, but it can only be constant. It can only be declared using public static final. it can be accessed |
| 71 | +using implementClass.name. |
| 72 | +3. no specific implementation on the method inside interfaces. |
| 73 | +4. non abstract class has to override all of its interfaces method, abstract class may not override all of the interface method. |
| 74 | +5. interface cannot be instantiated.but a variable cann be declared and refer to a reference of a class that implemented this interface. |
| 75 | + instanceof can be used to check if a obj implements a specific interface. |
| 76 | +6. when using multiple interfaces, make sure their method is not named the same. |
| 77 | + |
| 78 | + |
| 79 | + |
0 commit comments