Java 面向对象概念
2013-08-12 09:43
211 查看
Interface 接口
An interface defines a protocol of communication between two objects.An interface declaration contains signatures, but no implementations, for a set of methods, and might also contain constant definitions.
A class that implements an interface must implement all the methods declared in the interface. 接口中的方法都是implicitly abstract。
An interface name can be used anywhere a type can be used.
Polymorphism 多态
不同的子类overridden 父类的同一方法,每个子类具有单独的实现。Instance Methods(实例方法)vs Class Methods (类方法)
public class Animal { public static void testClassMethod(){ System.out.println("The class method" + " in Animal"); } public void testInstanceMethod(){ System.out.println("The instance method"+" in Animal "); } }
public class Cat extends Animal { /* *The overriding method has the same name, number and type of parameters, and return type as the method it overrides */ //Class Method类方法,需要通过类名来调用,子类hidden父类的方法 public static void testClassMethod(){ System.out.println("The class Method in Cat"); } //Instance Method实例方法,通过类的实例来调用,子类overridden父类的方法。 public void testInstanceMethod(){ System.out.println("The instance Method in Cat"); } public static void main(String[] args) { Cat myCat = new Cat(); Animal myAnimal = myCat; myAnimal.testInstanceMethod(); // 总是调用覆盖过的子类方法 super.testInstanceMethod(); //可以通过super来调用父类被覆盖的方法。 Animal.testClassMethod(); // 取决于是子类调用,还是父类调用 } }
super
super 可用来调用父类被覆盖的方法。super 可用来调用父类构造器constructor。
默认自动调用父类无参构造器,可以显示调用含参数的构造器。
Final
final 可用来声明方法不可被覆盖,类不可被继承。final static用于定义不变的变量。
Abstract Methods and Classes
An abstract classis a class that is declaredabstract—it may or may not include abstract methods. Abstract classes cannot be instantiated, but they can be subclassed.
An abstract method is a method that is declared without an implementation (without braces, and followed by a semicolon),like
//含有抽象方法的类必须为抽象 public abstract class GraphicObject { // declare fields // declare non-abstract methods abstract void draw(); }
子类必须实现了父类的所有抽象方法,否则子类必须也为抽象,留给它的子类去实现剩余的抽象方法。
Abstract Classes vs Interfaces
abstract classes can contain fields that are notstaticand
final, and they can contain implemented methods.
If an abstract class contains only abstract method declarations, it should be declared as an interface instead.
Multiple interfaces can be implemented by classes anywhere in the class hierarchy, whether or not they are related to one another in any way.
abstract class can implements an interface, 提供部分的方法实现。
抽象方法不能被实例化。
相关文章推荐
- Java面向对象程序设计的基本概念
- Java--面向对象之一:面向对象概念、类和对象的关系、封装、构造函数、this关键字、static关键字
- Java基础知识:面向对象-基本概念
- 黑马程序员——Java基础:面向对象一些概念的区分(三)
- Java面向对象-面向对象编程之基本概念
- Java面向对象概念
- 黑马程序员——Java基础:面向对象一些概念的区分(一)
- Java-面向对象概念
- Java面向对象高级--抽象类的基本概念------abstract
- 《Java从入门到放弃》JavaSE入门篇:面向对象概念(入门版)
- 【java基础】--(2)面向对象特征之概念、封装、继承
- javaSE_8系列博客——Java语言的特性(一)--关于面向对象基本概念的理解(2)--对象
- Java学习—面向对象概念
- Java学习个人备忘录之面向对象概念
- 《Java 面试题》 对象、类、面向对象和面向对象编程概念比较
- 《Java从入门到放弃》JavaSE入门篇:面向对象概念(入门版)
- 黑马程序员之java面向对象概念学习
- Java基础——对象和类1(面向对象基本概念)
- java面向对象须知的一些基本概念
- 第三章 java面向对象基本概念