1.继承条件下类的访问权限

public: 外界可自由访问;

private: 外界不可访问;

protected: 同一包中的子类都可以访问,另一包中的子类(派生于同一个父类)也可以访问;

default: 如果不指明任何权限,则默认同一包中的类可以访问;

2.通过 super 调用基类构造方法,必须是子类构造方法中的第一个语句。

class Grandparent
{
    public Grandparent()
    {
        System.out.println("GrandParent Created.");
    }
    public Grandparent(String string)
    {
        System.out.println("GrandParent Created.String:" + string);
    }
}
class Parent extends Grandparent
{
    public Parent()
    {
        //super("Hello.Grandparent.");
        System.out.println("Parent Created");
        // super("Hello.Grandparent.");
    }
}
class Child extends Parent
{
    public Child()
    {
        System.out.println("Child Created");
    }
}
public class TestInherits
{
    public static void main(String args[])
    {
        Child c = new Child();
    }
}

运行结果:

 

 

 如果在 Parent()方法 里写super(“Hello.Grandparent.”);注意:一定是在第一条语句,不然会报错

运行结果:

 

 

 3.Java方法覆盖语法规则

(1)覆盖方法的允许访问范围不能小于原方法。

(2)覆盖方法所抛出的异常不能比原方法更多。

(3)声明为final方法不允许覆盖。 例如,Object的getClass()方法不能覆盖。

(4)不能覆盖静态方法。

4.判断对象是否可以转换:

可以使用instanceof运算符判断一个对象是否可以转换为指定的类型

注意代码的最后两行,被注释的两行

Object obj="Hello";
if(obj instanceof String)
    System.out.println("obj对象可以被转换为字符串");
//声明hello时使用Object类,则hello的编译类型是Object,Object是所有类的父类
        //但hello变量的实际类型是String
        Object hello = "Hello";
        //String是Object类的子类,所以返回true。
        System.out.println("字符串是否是Object类的实例:" + (hello instanceof Object));
        //返回true。
        System.out.println("字符串是否是String类的实例:" + (hello instanceof String));
        //返回false。
        System.out.println("字符串是否是Math类的实例:" + (hello instanceof Math));
        //String实现了Comparable接口,所以返回true。
        System.out.println("字符串是否是Comparable接口的实例:" + (hello instanceof Comparable));
        String a = "Hello";
        //String类既不是Math类,也不是Math类的父类,所以下面代码编译无法通过
//        System.out.println("字符串是否是Math类的实例:" + (a instanceof Math));

5.类型转换

class Mammal{}
class Dog extends Mammal {}
class Cat extends Mammal{}

public class TestCast
{
    public static void main(String args[])
    {
        Mammal m;
        Dog d=new Dog();
        Cat c=new Cat();
        m=d;
        //d=m;
        d=(Dog)m;
        //d=c;
        c=(Cat)m;
    }
}

6.多态,继承

package InheritsAndPolymorphismSourceCode;

public class ParentChildTest {
    public static void main(String[] args) {
        Parent1 parent = new Parent1();
        parent.printValue();
        Child1 child = new Child1();
        child.printValue();

        parent = child;
//        System.out.println(parent.myValue);//100
        parent.printValue();

        parent.myValue++;//基类的myValue值
//        System.out.println(parent.myValue);//101
        parent.printValue();

        ((Child1)parent).myValue++;
//        System.out.println(parent.myValue);//101
        parent.printValue();
    }
}
class Parent1{
    public int myValue=100;
    public void printValue() {
        System.out.println("Parent.printValue(),myValue="+myValue);
    }
}
class Child1 extends Parent1{
    public int myValue=200;
    public void printValue() {
        System.out.println("Child.printValue(),myValue="+myValue);
    }
}

要先parent=child才能写((Child1)parent).myValue++;不然会报错

运行结果

 

另一个例子

class Parent2
{
    public int value=100;
    public void Introduce()
    {
        System.out.println("I'm father");
    }
}

class Son extends Parent2
{
    public int value=101;
    public void Introduce()
    {
        System.out.println("I'm son");
    }
}
class Daughter extends Parent2
{
    public int value=102;
    public void Introduce()
    {
        System.out.println("I'm daughter");
    }
}

public class TestPolymorphism
{
    public static void main(String args[])
    {
        Parent2 p=new Parent2();
        p.Introduce();//I'm father
        System.out.println(p.value);//100
        p=new Son();//p指向son
        p.Introduce();//I'm son
        System.out.println(p.value);//仍然是他自己本身的value即100,除非类型转换
        p=new Daughter();//指向Daughter
        p.Introduce();//I'm daughter
        System.out.println(p.value);//仍然是自己的value即100
    }
}

运行结果:

7.抽象类

原文地址:http://www.cnblogs.com/hmy22466/p/16816336.html

1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长! 2. 分享目的仅供大家学习和交流,请务用于商业用途! 3. 如果你也有好源码或者教程,可以到用户中心发布,分享有积分奖励和额外收入! 4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解! 5. 如有链接无法下载、失效或广告,请联系管理员处理! 6. 本站资源售价只是赞助,收取费用仅维持本站的日常运营所需! 7. 如遇到加密压缩包,默认解压密码为"gltf",如遇到无法解压的请联系管理员! 8. 因为资源和程序源码均为可复制品,所以不支持任何理由的退款兑现,请斟酌后支付下载 声明:如果标题没有注明"已测试"或者"测试可用"等字样的资源源码均未经过站长测试.特别注意没有标注的源码不保证任何可用性