What is the difference between php and java object-oriented

青灯夜游
Release: 2023-03-12 16:50:01
Original
2039 people have browsed it

Difference: 1. PHP’s class keyword cannot have modifiers other than abstract and final; while Java can. 2. The class name after the new keyword in php can be a string type variable, but not in java. 3. When accessing the parent object, php uses parent and java uses super.

What is the difference between php and java object-oriented

The operating environment of this tutorial: windows7 system, PHP7.1&&java8 version, DELL G3 computer

PHP and Java oriented Incomplete comparison of objects

PHP is a weakly typed language that can be programmed in a process-oriented or object-oriented manner. Java is a strongly typed language, purely object-oriented. The two languages ​​have some similarities in object-oriented aspects. With Java learning experience, it is easy to learn PHP by just looking at these keywords (class, extends, interface, implementations, final, public, protected, private, static) There is a very familiar feeling.

I have briefly summarized the similarities and differences in several aspects. I am not an academic, I only seek practicality. If there are any mistakes, please correct me.

Comparison 1: Class and interface declarations, class inheritance, interface implementation declarations

##Interface implementationUse the implements keyword to implement multiple interfacesNoneClass instantiationUsing the new keyword returns a reference to the object, and object assignment is also a reference assignmentThe class name after the new keyword in php It can be a string type variable, but java cannotAccess the parent object Use the parent keyword in php and super in java Keywords
Comparison Points Same points Different points
Class declaration all use the class keyword. It can be modified with abstract and final. The meanings of these two modifiers are the same. The class keyword in PHP cannot have modifiers other than abstract and final. The class keyword in Java can have public, proctected, private modifier, a Java file can only have one public class
Interface declaration Use the interface keyword Same as above
Class inheritance Use extends keyword, single inheritance None
Interface inheritance Use extends keyword None

Comparison 2: Access modifiers and access methods

Comparison pointsSame pointsDifferencesModifier keywordsare all public, protected and private NoneModifier behaviorpublic: accessible both inside and outside the class, protected: accessible inside the class and subclasses, private: Only accessible within the classThe access control of php should be looserDefault modifierNonephp default Is public, java defaults to privateAccess methods of instance members and instance methodsNonephp must use $this->, java Use this., or you can omit

Comparison 3: Class members and class methods

Comparison pointsSame pointsDifferent points##KeywordsScopeAccess method
static None
All instances of this class None
Class name::Variable or function name, can be accessed without instantiation None
Comparison 4: Class Constant

Comparison points##KeywordsconstJava usually uses final, PHP cannot use finalScopeThe All instances of the classNoneAccess methodClass name::Variable or function name, can be accessed without instantiationnone

Comparison 5: Abstract methods and abstract classes are basically the same. Abstract methods and abstract classes are both modified with the abstract keyword. A class with at least one abstract method is called an abstract class. Abstract classes cannot be instantiated. If a subclass wants to be instantiated, it must implement all abstract methods of the parent class.

Comparison 6: Rewriting/overwriting/overloading

The subclass method has exactly the same signature as the parent class method, which is called rewriting/overwriting. At this time, the parent class method Not visible in subclasses. Since Java is a strongly typed language, it has an overloading mechanism (same method name, different signatures, which method is called based on the actual parameter type and number when calling). PHP is weakly typed and does not have such overloading. Instead, it has overloading to dynamically create class attributes and methods, which is achieved through magic methods (__get, __set, __unset, __isset, __callStatic).

Recommended learning:

php training

Same points Different points

The above is the detailed content of What is the difference between php and java object-oriented. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!