Differences between Java and C # in Object-Oriented syntax,

Source: Internet
Author: User

Differences between Java and C # in Object-Oriented syntax,

After several years of development, I did not summarize anything. I went back to my hometown to become a trainer in a training class and taught me about software development. A little bit of knowledge is coming from the ground up. Or you have missed too much in the past, or you have never systematically studied the teaching materials. Although both teaching and learning are superficial, in order to help students understand and then think about why? This kind of thinking comes from the most basic and keeps giving feedback. It turns out that this kind of thinking can make your own progress very fast. Although you are still thinking about the problems at the entry stage, you have benefited a lot. How much can I get if I think about all the problems in the previous development? Write a blog and keep telling students that this is a good habit, but you have never practiced it yourself. So, from now on... object-oriented

Object-oriented is a development idea. The most important thing to remember is that everything is an object. In order to make programs better understood and written, integrating the methods and ideas for describing things in real life has become an object-oriented idea. To integrate things in life into a program, we need to describe and describe them into two aspects: Features and behaviors. Object Features and behaviors of different categories are significantly different, to better define a way to describe each type of things, the concept of classes in the programming world is extracted, which is equivalent to the concept of classes in life, everything should be of a type. Then, if things in life are classified by different aspects, they can be classified into different categories. classes in all programming are also justified and abstract, it is quite uncertain and random.

Class composition:

Classes in Java, including attributes and methods. Attributes are the variables in the class. They can be static variables, instance variables (global variables), and local variables (exist in methods, and the declaration period is limited to the call phase of the method)

Class in C #, including fields, attributes, and methods. The field corresponds to the property in java. The get and set accessors in the property object java in C # encapsulate the field. The same method is used to describe the behavior.

Class Member calls:

Instance members are called by objects. Static members are called by classes. However, in java, static members can also be called by instances, which is equivalent to a problem in which every student in the class can control the role at will. C # strictly limits this. Static members can only be called by classes.

Three features-encapsulation

Encapsulation: A method to hide internal implementation details and achieve data security protection and code reuse.

Encapsulation is everywhere. It seems simple but can be infinitely extended. There is no explicit keyword to indicate encapsulation. Since it is an idea and a means, there is no syntax difference between java and c. Only when they use access modifiers to achieve encapsulation results, the access modifiers in the two languages are different.

In Java:

Private: private, accessible only internally

Dufault: by default, it can be accessed within the same package.

Protected: protected, accessible in the same package or in sub-classes of different packages.

Public: public, accessible anywhere.

Feature: the private <default <protected <public

C # (introduce the concept of assembly. A namespace is similar to a package in java, but it is a logical group. Unlike a physical group in java, an assembly is similar to a project ):

Private: private, accessible only internally.

Intenal: Internal, accessible within the same assembly, same as default.

Protected: It is protected and can be accessed in subclass. It is different from protected in java. The scope here is smaller, and non-subclass of the same assembly cannot be accessed.

Proteted intenal: the Union of intenal and protected access ranges.

Public: public, accessible anywhere.

Features: the access range of intenal and protected cannot be clearly divided without a clear ing relationship between the sizes.

 

Three main features-inheritance: The purpose is to give a class the attributes and methods of another class.

In Java: extends indicates inheritance

Requirements for rewriting: a, method name, return value type, and parameter are the same; B. The access scope of the access modifier must be greater than or equal to the access modifier of the parent method;

Access parent class members: Use the super keyword, and use super (parameter). In the constructor, specify to call a constructor of the parent class.

C #: Use: indicates using inheritance

Rewrite requirements: a, method name, return value type, parameter, and access modifier are the same; B. The parent class method is virtual, And the subclass method is override.

Successful access to the parent class: The base keyword is used, and the base (parameter) is used after the constructor is used. The base cannot be used in a static environment to call the parent class constructor, you cannot call static members of the parent class.

Overwrite: use the new keyword. Introduce the overwrite content in c #. overwrite the non-virtual method of the parent class, that is, the method that cannot be rewritten. overwrite the method of the parent class. My Opinion on overwriting is to make up for the problems that may arise when the method must be modified by virtual to rewrite this restriction, but it does not need to be used if it is not necessary, in other words, I have not really understood the actual functions and Use Cases of coverage, and some people can comment on it.

The basis for determining whether the rewrite is successful: Use the reference of the parent class to point to the object of the subclass. If the method calls the parent class method, the rewrite is unsuccessful. If the method is called to the subclass method, this indicates that the rewrite is successful.

Three major features-Polymorphism

Polymorphism: multiple forms of the same behavior. The expressions are reload and rewrite.

Overload requirements: a. In the same class; B. The method name is the same; c. The parameters are different (number, type, and order of parameters ).

The method to call is determined based on the input parameters.

Abstract classes and interfaces

Abstract class: Classes modified using abstract are called abstract classes.

Source: in my opinion, the source of the abstract class is worth a careful consideration, which is conducive to a deeper understanding. There are many such problems in real life, that is, we know that this kind of thing will do this action (method), but we do not know how to do it. For example, we all know that animals are mobile, but you don't know how every animal moves. When defining this Animal class at this time, a move method is required, only the method header (which describes what will be done), and no method body (which describes how to do this ), this method is special. We mark it as an abstract method and use abstract modification.

So there are abstract methods in the Animal class. If you instantiate the Animal class, what problems will occur when you call the move method? Unknown, because it does not describe how to do it. To avoid this unknown situation, for example, defining the Animal class as an abstract class is obviously not instantiated. A non-static member of a class that cannot be instantiated cannot be called. What is the significance of such a class?

Therefore, abstract classes exist in inheritance. Abstract classes exist for abstract methods, but constructor methods cannot be instantiated. In terms of syntax, java and c # are the same in this respect.

Interface: a set of rules and specifications are formulated to allow implementation classes to meet these rules and specifications. in practical applications, a large number of programs increase program flexibility. For interface-oriented programming, I do not have a very deep understanding, or I can clearly explain one or two sentences. You can post your own experiences and learn from them on the back.

Difference: in C #, if the implementation class does not implement all the methods in the interface, for example, define itself as an abstract class and re-copy the unimplemented methods to define them as abstract methods.

Summary

I have been developing java for a long time. I learned c # Only when teaching is required. I learned it very quickly. Here we only pay attention to the syntax. We cannot see this as we are engaged in protocols and underlying mechanisms.

Only in this article, it's time to make a splash.


Comparison between java and C

On the Internet, c is the basis of java because c is the originator of advanced languages and the basis for learning any other advanced languages.
According to this idea, the assembly language is the basis of the C language, and the machine language is the basis of the assembly language ..
If you start from machine linguistics to java, I don't think you will ask questions on Baidu any more.

You have learned the course for any time you want to learn. Don't think too much about it. Go where your goal is. The goal is to learn java in java. The basic things can be supplemented later. First, you need to understand the java concept.
Try to do the same thing first.

What is the difference between java and c?

C is a typical process-oriented programming language. java is a fully object-oriented programming language,
But java is a c-like language. If you have the foundation of programming, you can directly learn java. If there is no basis,
We recommend that you first learn c. Many advanced languages are Class c. It is easier to learn other languages after learning c.

Related Article

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.