Whats the difference between subClass sc = new subClass() and superClass sc = new subClass?

后端 未结 6 1292
离开以前
离开以前 2021-01-03 05:14
class superClass {}

class subClass extends superClass{}

public class test
{

    public static void main()

{

    superClass sc1 = new subClass();
    subClass sc         


        
6条回答
  •  醉话见心
    2021-01-03 05:54

    In addition to the above answers, because the constructed object is really an object of type SubClass you can cast it to SubClass and call the methods:

    SuperClass superClass = new SubClass();
    superClass.method1();
    ((SubClass)superClass).method2();
    

提交回复
热议问题