Java Learning Notes---Inheritance and the use of super

Source: Internet
Author: User

Since the change of a video teaching, feel better than the original, is the school speed is too slag, a lot of video to see a card half a day, can only look at the downloaded already.

However, although it can not start from the beginning to reopen, but have seen once, in a look is also good, when the consolidation study.

Inherited keyword: extends

The format is as follows: Class subclass name extends parent class name {

...

}

Students, for example, inherit the father of the human species.

Class Student extends person{

...

}

If the declaration of a class does not use the keyword extends, the class inherits the object class by default. Object is the parent class of all classes. Object is a class in the Java.lang package.

When a subclass inherits a parent class, the child class can invoke all methods and member variables in the parent class. Subclasses can also declare methods or variables themselves individually.

The following code creates 4 classes. is Dog,cat,animal and main class containing the main function respectively. where dog and Cat inherit the parent class animal.

Cat inherits the Animal class:

Package com.cnblogs;

public class Cat extends animal{

Default constructor with no parameters

Public Cat () {

Super (); must appear in the first row of the construction method. This ();

Super ();

Super ("XX", 10); The code represents the constructor that invokes the parent class with a parameter

System.out.println ("Cat Builder"); }

/ * public String name;

public int age;

public void Walk () {

System.out.println ("Cat....walk");

}

public void sleep () {

System.out.println ("Cat....sleep");

}  */

Note hints

@Override

public void How () {

SYSTEM.OUT.PRINTLN (name + "" + "Cat....miaomiao");

Super enforces the call Show () method method of the parent class.

If there is no super,show (); is this.show (); The default notation.

The steps are: the show () method of the subclass is called first. Subclasses call the show () method of the parent class without the Show method

Super.show ();

}

public void Show () {

SYSTEM.OUT.PRINTLN (name + "" + "Cat----->show");

}

}

Declare the Dog class:

Package com.cnblogs;

public class Dog extends Animal {

/*public String name;

public int age;

public void Walk () {

System.out.println ("Dog....walk");

}

public void sleep () {

System.out.println ("Dog....sleep");

}  */

public void How () {

SYSTEM.OUT.PRINTLN (name + "" + "Dog....wangwang"); }

}

To create a animal parent class:

Package com.cnblogs;

Animal does not write inheritance is the default Inheritance object class. Abstract classes cannot be new. An instance object that is passed in as a subclass.

Public abstract class Animal extends object{

public String name;

public int age;

Default constructor with no parameters

Public Animal () {

This ("Baobao", 2);

This () is called the method of animal with the following parameter

System.out.println ("Animal---> ()");

}

constructor with parameter. Assigning an initial value to a property

Public Animal (String Name,int age) {

THIS.name = name;

This.age = age;

System.out.println ("Animal---> (name,age)");

}

public void Walk () {

SYSTEM.OUT.PRINTLN (name + "---->walk");

Sleep ();

how ();

This.show calls the show () method of the subclass, if the subclass does not have the show () method. Call the show () method of the parent class.

This.show ();

}

public void sleep () {

SYSTEM.OUT.PRINTLN (name + "---->sleep");

}

Abstract method.

public abstract void how ();

public void Eat () {

SYSTEM.OUT.PRINTLN (name + "---->eat");

}

public void Play () {

SYSTEM.OUT.PRINTLN (name + "---->play");

}

public void Show () {

SYSTEM.OUT.PRINTLN (name + "" + "Animal---->show");

}

}

Main Class (contains main function):

Package com.cnblogs;

public class Main {

public static void Main2 (string[] args) {

Build a Cat Object

Cat cat = new Cat ();

Cat.name = "Xiaohua";

Cat.age = 3;

Cat.walk ();

Cat.how ();

Generate a Dog object

Dog dog = new Dog ();

Dog.name = "Xiaomei";

Dog.age= 3;

Dog.walk ();

Dog.eat ();

Dog.how ();

Call the object that is being tuned out

Animal Animal = new Cat ();

Animal.name = "HH";

called the How () method of the subclass Cat ().

Animal.how ();

Animal animal2 = new Dog ();

Animal2.name = "XX";

How to call the Subclass dog ()

Animal2.how ();

Animal.walk ();

The Getmsg method requires that the subclass cat be passed in, so it must be cat, not animal.

Getmsg (CAT);

Getmsg (dog);

Getmsg (ANIMAL2);

}

/* public static void getmsg (Cat cat) {

Cat.how ();

}*/

The reference to the subclass's object passed to the parent class//is more flexible than the incoming individual cat above, typically using the following method

public static void Getmsg (Animal Animal) {

Animal.how ();

}

/** * * *. The new object first calls the first method of the subclass cat, runs Super (), and invokes the first method of the parent class animal

* *. The constructor method in the parent class executes this () method, and the This method calls the method with the animal parameter and outputs it; executes this () on the constructor that returns no parameters; The following output statement

. Execute super () in the return subclass cat; The following statement, output "Cat builder"

. Then Animal.how () calls the new Object cat's How () method

. Executes the How () method in the subclass Cat () method, and outputs.

*6.    Next, you run Super (). Show in the How () method, and then call the show () method of the parent class animal. and output the corresponding result. * */

public static void Main (String [] args) {

Animal Animal = new Cat ();

Animal.how (); }

}

Super (); call the constructor, methods, and properties of the parent class.

This (); invokes the current object of the method.

Today, the blog park can not be inserted code block, do not know what problems can only be handwritten, write a half-day. Look at the head is dizzy.

Java Learning Notes---Inheritance and the use of super

Related Article

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.