Hanshunping Java Note 9th 10th Lecture 11th Lecture 12th Lecture Abstract Encapsulation Inheritance Polymorphic method overloaded method override

Source: Internet
Author: User

Four features of Java object-oriented programming

Abstraction, encapsulation, inheritance, polymorphism

1. Abstraction: The common attributes and behaviors of a class of things are extracted to form a physical simulation, this method of research problems.

2. Encapsulation: The abstraction of data and the operation of the data is encapsulated together, the data is protected inside, the rest of the program only through the authorized operation (Member method)

In order to manipulate the data.

2.1 Public class demo{

public static void Main (string[] args) {

Clerk CLERK1 = new Clerk ("Floret", 24,4567.6f);

System.out.println ("name is", + clerk1.name + "salary" + clerk1.getsal ());

}

}

Class Clerk {

public String name;

private int age;

private float salary;

Public Clerk (String name,int age,float sal) {

THIS.name = name;

This.age = age;

This.salary = sal;

}

public float getsal () {

return this.salary;

}

}

2.2 Access Modifiers

Access level access control modifier similar to Bun class different package

1 publicly y y y y

2 protect y y y N

3 no modifier y y n N

4 private Y n n N

3. Inheritance

These same properties and methods are defined in the parent class, and all subclasses do not need to redefine these properties and methods, requiring the extends statement declaration to inherit the parent class

Subclasses can inherit at most one parent class (direct inheritance)

All Java classes are subclasses of the Obiect class

Package com.abc;

public class demo117{

public static void Main (string[] args) {

Pupil P1 = new pupil ();

P1.printname ();

}

}

Class stu{

protected int age;

public String name;

public float fee;

Private String job;//is not inherited, and you do not want to inherit a property and method declared as private

public void Printname () {

System.out.println ("name" + this.name);

}

}

Class Pupil extends stu{

public void Pay (float fee) {

This.fee=fee;

}

}

Class Pre extends pupil{

public void Pay (float fee) {

this.fee=fee*1.5f;

}

}

4. polymorphic

  4.1 Method overloading

(1) Same method name

(2) method parameter type, number, order, at least one of the different.

(3) The return type of the method can be different

(4) The modifier of the method can be different

 4.2 Method overrides

(1) The return type, parameter, and method name of the method of the subclass are the same as the parent class

(2) Subclass methods cannot reduce access to the parent class method

public class demo{

public static void Main (string[] args) {

Cat CAT1 = new Cat ();

Cat1.cry ();

Dog dog1 = new Dog ();

Dog1.cry ();

}

}

Class animal{

int age;

String name;

public void Cry () {

System.out.println ("I am an animal, do not know to be called");

}

}

Class Cat extends animal{

public void Cry () {

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

}

}

Class Dog extends animal{

public void Cry () {

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

}

}

super.xxx; Invokes a variable or method reference of the parent class XXX.

Super.xxx (); Direct access to the method that invokes the parent class.

Super (); Call the parent class initialization method public xxx () method.

    4.3 polymorphic

public class demo123{

public static void Main (string[] args) {

Animal an = new Cat ();

An.cry ();

an = new Dog ();

An.cry ();

Master master = new Master ();

Master.feed (New Dog (), New Bone ());

Master.feed (New Cat (), New Fish ());

}

}

Class master{

public void Feed (Animal an,food f);

An.eat ();

F.showname ();

}

Class Animal () {

public void Eat () {

}

}

Class Dog extends Animal () {

public void Eat () {

}

}

Class Cat Extends Animal () {

public void Eat () {

}

}

Class food{

public void show Name () {

}

}

Class Fish extends food{

public void show Name () {

}

}

Class Bone extends food{

public void show Name () {

}

}

 

Hanshunping Java Note 9th 10th Lecture 11th Lecture 12th Lecture Abstract Encapsulation Inheritance Polymorphic method overloaded method override

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.