12 java object-oriented inheritance

/*

  • Characteristics of object-oriented inheritance
    * 1. Why should inherit?
  • When the presence of the same properties and behavior of a plurality of classes, the extracted content to a single class,
  • So more than one class do not need to define these properties and behavior, as long as you can inherit that class.
  • A plurality of class herein referred to as a subclass (derived class), a separate class is called the parent class (superclass or base class).
  • 2. The class inheritance syntax rules:
  • class Subclass extends SuperClass{
  • }
  • Once the subclass inherits the parent class after a subclass to get all the properties and methods of the parent class declaration.
  • If the parent class and private attributes and methods, will be acquired private subclasses of the parent class structure, but because of the influence of the package, can not be directly retrieved subclass
  • Subclass inherits the parent class in the future can also define their own unique properties and methods to achieve the extension.
  • Subclass relationship and the parent class, different from the set of subsets relationship, the subclass function is typically stronger than the parent class.
  • 3. inherit advantages:
  • Reduce code redundancy, improve code reusability.
  • Facilitating extension.
  • Provides the premise for the use of polymorphism
  • 4.java provisions on inheritance
  • A class can have multiple sub-categories,
  • A class can have only one parent, only supports single inheritance, do not support multiple inheritance
  • Class can inherit a multi-layer, sub-class of the parent class can have subclasses subclasses (like Sun)
  • Direct subclass inherits the parent class is a direct superclass, the indirect parent class is inherited indirect parent
  • After the subclass inherits the parent class, you get the direct parent class attributes and methods as well as all indirect parent class
  • All java classes (except class java.lang.Object) directly or indirectly inherits class java.lang.Object
  • That is, all the java classes have java.lang.Object function declaration.
  • The succession of other classes in the package needs to guide package, other packages inherit parent class needs permission to public.
  • Permissions for the parent class's attributes and methods can be protected in the process of calling the constructor body subclass different packet, but calls can not instantiate an object subclass of
  • Debug Tool:
  • Investigation bug ways:
  • 1, set the output statements in a program, see the various stages of the operation for the case of a small amount of code
  • 2, the applicable Eclips Debug tool, set breakpoints, to see if changes in each variable in line with expectations
  • Debug tools common operations:
  • 1step into rows into the current methods used to view the source code
  • 2step over the current line statement. Go to the next line
  • 3step return after executing a method where the current line, the next line
  • 4drop to frame back to the first line of the method of the current line where the re-browse method body
  • 5resume complete implementation of all the current line of code where the breakpoint, go to the next break, if not followed by break ends.
    * /

Create a parent class

package object_chapter2;

public class ExtendsTest {
	public String name = "testname";
	private int age = 18;
	int number = 100;
	protected int argus = 33;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	public int getNumber() {
		return number;
	}
	public void setNumber(int number) {
		this.number = number;
	}
	public int getArgus() {
		return argus;
	}
	public void setArgus(int argus) {
		this.argus = argus;
	}
			
}

Create a subclass of different packages

package exercise;

import object_chapter2.*;

//import exercise.ObjectTest; 
public class Test {
	public static void main(String[] args) {
		Subclass sc = new Subclass();
		sc.test();
		System.out.println(sc.name);
		System.out.println(sc.getArgus());
		Subclass sc1 = new Subclass(43);
		System.out.println(sc1.getArgus());
		System.out.println(new Subclass().getArgus());
	}	
}
class Subclass extends ExtendsTest{
	public Subclass() {
		
	}
     public Subclass(int argus) {
		this.argus = argus;
	}
	 public void test() {
		 argus = 23;
		 name = "KK";
	 }
	 public int getArgus() {
		 return argus;
	 }
}
Published 47 original articles · won praise 1 · views 1062

Guess you like

Origin blog.csdn.net/wisdomcodeinside/article/details/104105276