<Introduction to Java Programming> Note 01

super

It can be used in two ways:

  • Calling Superclass Constructors
    The statement super() or super(arguments) must appearin the first line of the subclass constructor and is the only way to invoke a superclass constructor.
    A constructor may invoke an overloaded constructor or its superclass's constructor. If neither of them is invoked explicitly, the compiler puts super() as the first statement in the constructor.
    In any case, constructing an instance of a class invokes the constructors of all the superclasses along the inheritance chain.A superclass's constructor is called before the subclass's constructor. This is called constructor chaining.
  • Calling Superclass Methods
    To invoke a overridden method defined in superclass, from inside a overriding method in subclass, you must use super.
    an a subclass of Circle access the toString method defined in the GeometricObject class using a syntax such as super.super.toString()? No. This is a syntax error.
    Can an instance of Circle invoke the toString method defined in the GeometricObject class? No, not any more, since toString() in GeometricObject has been overridden in Circle.



Overriding vs. Overloading

  • An instance method can be overridden only if it is accessible. Thus a private method cannot be overridden, because it is not accessible outside its own class. If a method defined in a subclass is private in its superclass, the two methods are completely unrelated.
  • Like an instance method, a static method can be inherited. However, a static method cannot be overridden. If a static method defined in the superclass is redefined in a subclass, the method defined in the superclass is hidden. The hidden static methods can be invoked using the syntax SuperClassName.staticMethodName.



Data and Methods Visibility



Polymorphism

subtype and supertype

A class defines a type. A type defined by a subclass is called a subtype, and a type defined by its superclass is called a supertype.

Polymorphism means that a variable of a supertype can refer to a subtype object.

A method can be defined in a superclass and overridden in its subclass.


declared type and actual type

The type that declares a variable is called the variable's declared type.

The instance may be created using the constructor of the declared type or its subtype. The actual type of the variable is the actual class for the object referenced by the variable.


Which method is invoked by an object is determined by its actual type. This is known as dynamic binding.

Matching a method signature and binding a method implementation are two separate issues. The declared type of the reference variable decides which method to match at compile time. The compiler finds a matching method according to the parameter type, number of parameters, and order of the parameters at compile time. A method may be implemented in several classes along the inheritance chain. The JVM dynamically binds the implementation of the method at runtime, decided by the actual type of the variable.



Abstract Class

  • An abstract method cannot be contained in a nonabstract class. If a subclass of an abstract superclass does not implement all the abstract methods, the subclass must be declared abstract. In other words, in a nonabstract subclass extended from an abstract class, all the abstract methods must be implemented, even if they are not used in the subclass. Also note that abstract methods are non-static.
  • An abstract class cannot be instantiated using the new operator, but you can still define its constructors, which are invoked in the constructors of its subclasses. For instance, the constructors of GeometricObject are invoked in the Circle class and the Rectangle class.
  • A class that contains abstract methods must be abstract. However, it is possible to declare an abstract class that contains no abstract methods. In this case, you cannot create instances of the class using the new operator. This class is used as a base class for defining a new subclass.
  • A subclass can override a method from its superclass to define it as abstract. This is very unusual, but is useful when the implementation of the method in the superclass becomes invalid in the subclass. In this case, the subclass must be defined as abstract.
  • A subclass can be abstract even if its superclass is concrete. For example, the Object class is concrete, but its subclasses, such as GeometricObject, may be abstract.
  • You cannot create an instance from an abstract class using the new operator, but an abstract class can be used as a data type. Therefore, the following statement, which creates an array whose elements are of GeometricObject type, is correct.
    GeometricObject[] objects = new GeometricObject[10];
    You can then create an instance of GeometricObject and assign its reference to the array like this:
    objects[0] = new Circle();




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值