Java Technology's third assignment-object-oriented-inheritance, abstract class, interface

Source: Internet
Author: User

A) Study summary

1. Read the procedure below to see if you can compile the pass? If not, explain why. How should I modify it? What is the result of running the program? Why do I have to call the construction method of the parent class before the constructor of the subclass is run? Can you turn around?

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() {                System.out.println("Parent Created");        super("Hello.Grandparent.");    }}class Child extends Parent {    public Child() {        System.out.println("Child Created");    }}public class Test{    public static void main(String args[]) {        Child c = new Child();    }}

The above program cannot be compiled. Because the super statement must be placed in the first row of the subclass construction method. Place the modified statement in the first row of the subclass construction method.

Run the result as

The constructor of the parent class is first called to initialize the property in the parent class, which means that there is a parent class instance before the child class instance, not the other way around.

2. Read the procedure below, analyze what errors exist in the program, explain why, and how to correct them? What is the result of running the correct program?

class Animal{  void shout(){      System.out.println("动物叫!");  }}class Dog extends Animal{      public void shout(){            System.out.println("汪汪......!");       }      public void sleep() {       System.out.println("狗狗睡觉......");      } }public class Test{    public static void main(String args[]) {        Animal animal = new Dog();         animal.shout();        animal.sleep();        Dog dog = animal;        dog.sleep();         Animal animal2 = new Animal();        dog = (Dog)animal2;        dog.shout();    }}

Errors that exist:
1, the parent class upward transformation declaration of properties, subclasses can only use it to invoke the method contained in the parent class;
2. When you move down, you must confirm that there is a relationship between the subclass and the parent class
After correction

3. Run the following procedure

class Person {    private String name ;    private int age ;    public Person(String name,int age){          this.name = name ;          this.age = age ;    } }public class Test{        public static void main(String args[]){              Person per = new Person("张三",20) ;              System.out.println(per);             System.out.println(per.toString()) ;   } }

Run the result as

So, what is the result of the program running? Use Eclipse to open the source code of the println (per) method and see which methods are called in the method, can you explain the results of this example?

The Write method is called in the Print method, and the class itself is printed.
(3) Add the following method to the person class

public String toString(){         return "姓名:" + this.name + ",年龄:" + this.age ;  

After modification

class Person {    private String name ;    private int age ;    public Person(String name,int age){          this.name = name ;          this.age = age ;    }    public String toString(){        return "姓名:" + this.name + ",年龄:" + this.age ; } }public class Test{        public static void main(String args[]){              Person per = new Person("张三",20) ;              System.out.println(per);             System.out.println(per.toString()) ;   } }

Run results

The ToString method is covered, and the calling method is the content of the overwrite, and the print class name also defaults to the Overwrite method.
4. Car rental companies, taxi types have passenger cars, trucks and pickup three kinds, each car in addition to have a number, name, rent three basic attributes, passenger cars have capacity, goods vehicles have cargo capacity, pickup is both carrying capacity and cargo capacity. This paper analyzes the above problems with object-oriented programming, and represents them as appropriate classes, abstract classes or interfaces, and illustrates the design ideas. Now to create a list of rental cars, how should I create them?
Define an abstract class (or interface) "Taxi", with an abstract method carrying capacity, define bus class, train class, pickup class to inherit the taxi (or implement the interface), to implement the abstract method in the parent class,
The bus realizes abstract method, print carrying capacity, truck realizes abstract method, print cargo capacity, pickup implements abstract method, print carrying capacity and cargo capacity,
Define a car hire class, declare an array of taxi classes, initialize by constructing method parameters and on-transition.

5. Read the procedure below to analyze if the code can be compiled, if not, explain why, and make corrections. If you can, list the results of the operation

interface Animal{            void breathe();        void run();        void eat();    }    class Dog implements Animal{        public void breathe(){            System.out.println("I‘m breathing");        }        void eat(){            System.out.println("I‘m eating");        }    }    public class Test{        public static void main(String[] args){            Dog dog = new Dog();            dog.breathe();            dog.eat();        }    }

Cannot: Method in interface, default is public abstract so when subclasses implement abstract methods, they should be decorated with public
And you need to implement all the abstract methods in the interface, where you need to practice the Run method in the interface.
(ii) Experimental summary
1, Pet Shop, always can not be entered into the array of things, can not be very good to write the relevant code;
2, after declaring the array, you must instantiate each member in the group, or there will be a null pointer phenomenon
3. Inheritance must override all methods of the parent class.
4, on the transition

Object transformation

On the Transition object

On a Transform object: Make the parent class object reference to a child class instance
On transformation Format: Parent class Name Parent class object =new subclass name (subclass instance);
The term for transformation: Parent class object XXX is the object of the child class on the top of the transformation

Characteristics of the objects on the transition:

The on-the-spot object cannot operate on a newly added variable or method of the child class
Methods that are called on a transformed object can only be methods that are overridden by subclasses and inherited and not modified
On a transformed object can call a subclass of a variable that is inherited or hidden

(c) Code hosting (be sure to link to your project)

Https://gitee.com/hebau_java_cs16/Java_CS01ZY/tree/master
Code Cloud Commit History

Java Technology's third assignment-object-oriented-inheritance, abstract class, interface

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.