- A obj = new A();
- obj.gretting();
- obj.getName();
- B obj2 = new B();
- obj2.gretting();
- obj2.getName();
複製代碼- class A{
- private String name1 = "A";
- A(){
- }
- void getName(){
- System.out.println(name1);
- }
- void gretting()
- {
- System.out.println("Class A has");
- }
- }
- class B extends A{
- private String name1 = "B";
- @Override
- void getName(){
- System.out.println(name1);
- super.getName();
- }
-
- @Override
- void gretting()
- {
- System.out.println("Class B has");
- }
- }
複製代碼 |