A review of the basics of Java.
1, inheritance: 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, only one parent class at a time.
2. Rewrite: rewrite (override) is a subclass that re-writes the implementation of the method that allows access to the parent class! Neither the return value nor the formal parameter can be changed.
3, Overloading: Overloading (overload) is in a class, the method name is the same, and the parameters are different. The return type can be the same or different.
4. Polymorphism: Three conditions exist
- Inherited
- Rewrite
- Parent class reference to child class object
5, abstract class: Abstract class to define the abstraction classes.
6, abstract method: If a class contains an abstract method, then the class must be an abstract class.
7, encapsulation: Simple is the member private, through the set and get method to obtain.
8, Interface: is a collection of abstract methods, interfaces are usually declared in interface.
- Interface has no constructor method.
- All methods in an interface must be abstract methods.
- An interface cannot contain member variables, except static and final variables.
- The interface supports multiple inheritance.
9. Package: Java uses the package This mechanism is designed to prevent naming conflicts, access control, and provide search and location classes
Wrote a piece of code to use the above knowledge points.
Parent class: Animals. This is an abstract class because it declares the abstract method eat. For the member variable name, the encapsulated idea is used, declared as private, and accessible only through GetName and SetName.
There is also a move method that is used to override the subclass.
Sub-class: Cat. Implement the Eat, rewrite the move, reload the move and add your own method to sleep.
Sub-class: Dog. Implemented the eat and rewritten the move.
The above three classes are placed inside the animals package.
Main.java:
Import animals.*;p Ublic class Main {public static void main (String args[]) {//parent class pointer to subclass reference (UP) animals cat = new Cat ("Cat"); an Imals dog = new Dog ("Dog"), Cat.eat ();d og.eat (),//rewrite (unlike the parent Class) Cat.move ();d og.move ();//cat.sleep (); Because there is no sleep//cat inside animals. Move (5); error, because animals does not have an overloaded move (int) if (cat instanceof cat) {//is not an instance of cat class cat Newcat = (cat) cat;// Downward Transformation System.out.println (Newcat.getname ()); Newcat.sleep (); Newcat.move (5);//overloaded, functions with the same name, but with different argument lists}}}
Animals.java
The package animals;//can have only one public class//abstract function, so abstract class Animals{private string name;//encapsulation//constructor animals (string Name) {this.name = name;} The specific method is implemented by subclasses, so the definition is abstract public, abstract void eat ();//The same method used to override public void Move () {System.out.println ("Animal can Move");} Common method, the parent class implements public void SetName (String name) {this.name = name;} Public String GetName () {return name;}}
Cat.java
Package animals;//Cats public class Cat extends animals{//each class must have its own constructor public cat (String name) {super (name);} Because inherited the animal class, so the member name, as well as SetName, getname.move all have//because the parent class is an abstract class, you must implement the abstract method public void Eat () {System.out.println ("Cat eats Fish");} You can override a method that exists on the parent class public void Move () {//Call the parent class with the same name function Super.move (); System.out.println ("The Cat can run!" ");} Reload public void move (int num) {move (); System.out.println ("Cat ran" +num+ "S");} Add your own method public void sleep () {System.out.println ("slepping!");}}
Dog.java
Package Animals;public class Dog extends animals{//each class must have its own constructor public Dog (String name) {super (name);} Because inherited the animal class, so the member name, as well as SetName, getname.move all have//because the parent class is an abstract class, you must implement the abstract method public void Eat () {System.out.println ("What does a dog eat?") ");} You can override a method that exists on the parent class public void Move () {//Call the parent class with the same name function Super.move (); System.out.println ("Dogs can also run!") ");}}
Output:
Review of Java object-oriented fundamentals