Javase Getting Started learning 15:java object-oriented inheritance

Source: Internet
Author: User

Today we look at the inheritance of Java's three main object-oriented features.

a Java inheritance

Inheritance is a cornerstone of Java object-oriented programming technology because it allows classes of hierarchical hierarchies to be created. Inheritance can be understood as an object from another

The process by which an object gets properties.

If Class A is the parent class of Class B, and Class B is the parent class of Class C, we also call C a subclass, and Class C inherits from Class A. In Java, the class's secondary

is a single inheritance, that is, a subclass can have only one parent class.

The two most commonly used keywords in inheritance are extends and implements. The use of these two keywords determines whether an object and another object are

Is-a (is a) relationship. By using these two keywords, we can implement one object to get the properties of another object.

All Java classes are inherited by the Java.lang.Object class, so object is the ancestor class of all classes, and except for object, all classes must be

Must have a parent class.

By passing the extends keyword you can declare that a class inherits from another class, and the general form is as follows:

Instance

A.java Source file Code:

public class A {    private int i;    protected int J;    public void Func () {           //implementation details    }}

B.java source file code:
public class B extends A {     //implementation Details}

The code snippet above shows that B is inherited from a, and B is a subclass of a. A is a subclass of object, which can be declared without a display. As a child

Class, the instance of B has all member variables of a, but does not have access to the private member variable B, which guarantees the encapsulation of a.

two is-a relationship

Is-a is saying that one object is a classification of another object. The following is the implementation of inheritance using the keyword extends.

Instance:

Animal.java Source file Code:

public class animal{public String name;public int age;public void Eat () {System.out.println ("Animal has the ability to eat");}}
mammal. Java source file code:

public class mammal extends animal{}
Reptile. Java source file code:

public class reptile extends animal{}
Dog. Java source file code:

public class Dog extends mammal{}

Based on the example of the source code file in the same folder, later this folder is called a package, the following statement is correct:

1) The animal class is the parent class of the mammal class.

2) The animal class is the parent class of the reptile class.

3) The mammal class and the reptile class are subclasses of the animal class.

4) The Dog class is both a subclass of the mammal class and a subclass of the animal class.

Analyze the is-a relationship in the example above, as follows:

Mammal Is-a Animal

Reptile Is-a Animal

Dog is-a Mammal

So: Dog is-a Animal

By using the keyword extends, subclasses can inherit all the methods and properties of the parent class, but cannot use private (private) methods and properties.

We can determine mammal is-a Animal by using the instanceof operator.

Instance:

Test.java Source file Code:

public class test{public      static void Main (String args[]) {          Animal a = new Animal ();          Mammal m = new mammal ();          Dog d = new Dog ();          System.out.println (M instanceof Animal);          System.out.println (d instanceof Mammal);          System.out.println (d instanceof Animal);}      

The results of the above example compilation run as follows:

after introducing the extends keyword, let's look at how the Implements keyword is used to represent is-a relationships. Implements keyword Usage

In the case where the class inherits the interface, the keyword extends cannot be used.

Instance:

Animal.java Source file Code:

public Interface animal{}

mammal. Java source file code:

public class mammal implements animal{}

Dog. Java source file code:

public class Dog extends mammal{}

three instanceof keywords

You can use the instanceof operator to verify that the mammal and dog objects are an instance of the animal class.

Continue with the example of the above interface:

Test.java Source file Code:

public class Test{public static void Main (String args[]) {           mammal m = new mammal ();           Dog d = new Dog ();           System.out.println (M instanceof Animal);           System.out.println (d instanceof Mammal);           System.out.println (d instanceof Animal);}

The results of the above example compilation run as follows:

four has-a relationship

Has-a represents a dependency between a class and its members. This facilitates the reuse of code and reduces code errors.

Instance:

Vehicle.java Source file Code:

public class vehicle{}
Speed.java Source file code:

public class speed{}

Van . Java source file code:

public class Van extends vehicle{private speed sp;}

The van class and the speed class are has-a relationships (van has a speed) so that you don't have to paste all the code of the Speed class into the Van class, and

The speed class can also be reused with multiple applications. In object-oriented features, the user does not have to worry about how the inside of the class is implemented. The van class will implement the fine

Section is hidden from the user, so the user only needs to know how to invoke the Van class to complete a function without having to know whether the van class is doing it itself or calling

Other classes to do the work.

Five Java inheritance features

Java only supports single inheritance, which means that a class cannot inherit multiple classes. The following practices are not legal:

public class extends Animal, mammal{}

Java only supports single inheritance (inheriting basic classes and abstract classes), but we can use interfaces to implement (multi-inheritance interfaces), and script structures such as:

public class Apple extends Fruit implements Fruit1, fruit2{}

Generally we inherit the basic class and abstract class with the extends keyword, implement the inheritance of the interface class with the Implements keyword.







Javase Getting Started learning 15:java object-oriented inheritance

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.