Java knowledge question

Interface vs Abstract class

  • Interface定义了接口,即有哪些必须(It is basically a contract or a promise the class has to make.)要实现的method,但是仅仅是abstract method,并不固定如何implement

        - Interface中variable都是final的
        - Interfaces give the idea what is to be done but not how it will be done. So implementation completely depends on developer by following the given rules
    
  • Abstract可以保持abstract method,也可以定义具体的实施过程,这个具体的Implementation将被extends的所有class所使用

        - An abstract class is a basis for different subclasses that share behaviour which does not need to be repeatedly created.
        - subclass can override the behavior as well
    
public interface LoginAuth{
   public String encryptPassword(String pass);
   public void checkDBforUser();
}

Now suppose you have 3 databases in your application. Then each and every implementation for that database needs to define the above 2 methods:

public class DBMySQL implements LoginAuth{
          // Needs to implement both methods
}
public class DBOracle implements LoginAuth{
          // Needs to implement both methods
}
public class DBAbc implements LoginAuth{
          // Needs to implement both methods
}

But what if encryptPassword() is not database dependent, and it's the same for each class? Then the above would not be a good approach.

Instead, consider this approach:

public abstract class LoginAuth{
   public String encryptPassword(String pass){
            // Implement the same default behavior here 
            // that is shared by all subclasses.
   }

   // Each subclass needs to provide their own implementation of this only:
   public abstract void checkDBforUser();
}

Inheritance

通过继承可以使对对象的描述更加清晰,可以实现代码的复用,可以实现重写类中的变量或方法,可以实现在无源代码的情况下修改被继承的类。

childClass extends parentClass

多态

指一个对象的行为方式可以有很多种操作形态,根据不同对象,会有不同的操作。所以多态寄托于对象。在面向对象编程中,多态主要是通过方法的重载和覆盖体现的。方法是通过给不同对象发送相同的信息,根据不同的对象来完成不同的工作。

  • 多态存在的必要条件是
    1. 继承
    2. 子类要重写父类方法
    3. 父类引用指向子类对象
Parent instance = new Child();
instance.foo(); //==> Child foo()

重载

一个方法名,参数不同,这叫方法重载。(Overload)

方法覆盖

父类与子类有同样的方法名和参数,这叫方法覆盖。(Override)

Generic type

  • avoid casting in client
  • discover type mismatch errors at compile-time instead of runtime
//stack linkedlist implementation can use Item for generic type
//as for the array implementation, java doesnt support generic type array
//have to do this
(Item[]) new Object[capacity];
  • for the primitive ytpes, they all have wrapper object type to automict cast between a primitive type and its wrapper (autoboxing)
  • Java arrays are covariant but Java generics are not: that is, {String[]}String[] is a subtype of {Object[]}Object[], but {Stack}Stack is not a subtype of {Stack}Stack.

Iteration

  • Implement Iterable interface, which is a class has a method returns a Iterator
  • Iterator has methods hasNext() and next()
  • In this way, client codes are much simplerfor (String s : stack)

Immutability of keys

  • Assumption: client doesnt change keys while they are on the PQ
  • Best practice: use immutable keys, final key word, which means cant change the data type value once created e.g String, Integer, Double, etc Mutable: stack, stringbuffer

results matching ""

    No results matching ""