Java object-Oriented learning notes (2)

Source: Internet
Author: User

A. Constructor

1. The greatest use of the constructor is to perform initialization when the object is created, and each Java class must contain one or more constructors. The general system provides an argument-free constructor, but it will not work again if we define a constructor ourselves. If we write a parametric constructor for a class, it is better to provide a parameterless constructor for the class.

2. Overloading of constructors

The same class can contain more than one constructor, and multiple constructors have different parameter lists, which are overloads called constructors. When the system calls the constructor through new, the system determines which constructor to call, based on the list of arguments passed in. The sample code is as follows:


<span style= "FONT-SIZE:18PX;" >package Test;public class Constructoroverload {public String name;public int count;//provides parameterless constructor public Constructoroverload () {}//provides a constructor with two parameters public Constructoroverload (String name,int count) {this.name=name;this.count= Count;} public static void Main (string[] args) {//Constructoroverload object created by the parameterless constructor constructoroverload Oc1 = new Constructoroverload ();//Create Constructoroverload object Constructoroverload oc2 = new Constructoroverload by a parametric constructor ("Longkaili ", 21); System.out.println (oc1.name+ "" +oc1.count); System.out.println (oc2.name+ "" +oc2.count);} </span>
if a constructor completely contains the executor of another constructor, it can be called through the This keyword, which can increase the maintainability of the code. The sample code is as follows:


<span style= "FONT-SIZE:18PX;" >package Test;public class Apple {public string Name;public string Color;public double weight;public Apple () {}public Ap PLE (String name,string color) {System.out.println ("the other constructor called me (0.0)"); this.name=name;this.color=color;} Public Apple (String name,string color,double weight) {//The initialization code of another constructor is called through this. This (name,color); this.weight=weight;} public static void Main (string[] args) {Apple ap = new Apple ("Longnkaili", "Red", 110); System.out.println (ap.name+ "" +ap.color+ "" +ap.weight);}} </span>


Two. Inheritance in Java

The biggest difference between Java inheritance and C + + is that Java is a single inheritance, and each subclass can have only one direct parent (of course, the indirect parent class is random).

Java inheritance is implemented by keyword extends, which inherits the parent class and can get all the methods and file of the parent class, but cannot get the constructor of the parent class.

Here is a description of where inheritance needs attention from several directions.

1. Overriding methods of the parent class

If a method of the parent class is visible to the child class (not private), and then a method is defined in the subclass that is exactly the same as the method, it is called the override of the methods, also known as method overrides. The rewrite of the method should obey the principle of "two and two small and one big". It should be noted that both the overriding method and the overridden method must be either class methods or instance methods, not one class method and one instance method. Here is a sample code:


Package Test;public class Bird {public void Fly () {System.out.println ("I fly freely in the sky ...");}}

Package Test;public class Ostrich extends bird{public void fly () {System.out.println ("I can only run on the ground ...");} public static void Main (string[] args) {Ostrich Os = new ostrich (); Os.fly ();}}

2.super limit

When a subclass method overrides a parent class method, the subclass object cannot access the overridden method in the parent class, and the overridden method in the parent class can be called in the method of the child class. If you need to call the parent class method in a subclass method, you can invoke the overridden method in the parent class by using super (the overridden instance method) or the parent class name (overridden by the class method) as the caller.

When a program creates a subclass object, the system allocates not only the memory space for the instance variables defined in the class, but also allocates memory for all instance variables that it inherits from the parent class (whether it is a quilt class override). The sample code is as follows;


Package Test;public class BaseClass {public     int a = 5;}

Package Test;public class Subclass extends baseclass{public     int a=7;     public void Accessower () {     System.out.println (a);     }     public void Accessbase () {     //through super to access the overridden variable     System.out.println (SUPER.A) in the parent class;     }}

3. Subclass calls the constructor of the parent class

You can use super to explicitly invoke the constructor of the parent class, the super call has the same format as this, and the super call must be placed on the first line, so super and this cannot occur at the same time. Without a super explicit invocation, a subclass must invoke the parameterless constructor of the parent class before its own constructor executes, and must start with the parent class at the beginning of the call. The sample code is as follows:


Package Test1;public class Creature {public creature () {System.out.println ("creature parameterless Constructor");}}

Package Test1;public class Animal extends Creature{public Animal (String name) {System.out.println ("Animal with a parameter to the constructor," + " The name of the animal is: "+name);} Public Animal (String Name,int age) {This (name); System.out.println ("Animal with two parameters of the constructor," + "whose age is:" + Age);}}

Package Test1;public class Wolf extends animal{public     Wolf () {     super ("Grey Wolf", 3);     System.out.println ("Wolf's parameterless constructor");     }     public static void Main (string[] args) {     new Wolf ();}     }


Java object-Oriented learning notes (2)

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.