About Java object-oriented analytic problems

Source: Internet
Author: User
Tags modifier

Tag: Return does not have access to void bool default SQL Nal Div

Analyze the program to see if there are any problems, if any, say why.
-----------------------------------------------------------------------------
1.

 abstract   Class   Name { private   String name;  public  abstract  boolean  isstupidname (String name) {} //  There is a method body, but the method body is empty. Will error. }

Error.
The abstract method must end with a semicolon without curly braces.
In Java, a method that does not have a concrete method body should be defined as an abstract method.
In a class, if there is an abstract method, the class must be defined as an abstract class.
-----------------------------------------------------------------------------
2.

 Public class Something {   void  dosomething () {       private String s = ""; c18/>int l = s.length ();}   }

Error.
  No access modifiers (private,public and protected) can be placed before local variables.
Final can be used to modify local variables. The final modified local variable becomes constant.
-----------------------------------------------------------------------------
3.

Abstract class Something {   privateabstract  String dosomething ();}

error.
abstract methods cannot be decorated with private.
abstract method is to let the subclass implement (Implementation) specific details, how can you use private to block the abstract method? ?

---------------------------------------------------------------------- -------
4.

 Public class Something {   publicint addone (finalint  x) {         return + +x;   }}

Error.
int x is final decorated, meaning that x cannot be modified in the AddOne method.
-----------------------------------------------------------------------------
5.

 Public classSomething { Public Static voidMain (string[] args) {Other O=NewOther (); NewSomething (). AddOne (o); }    Public voidAddOne (FinalOther O) {       //o = new Other (); //object o re-points to the new address value, but the object o is final decorated, so this sentence will be an error! o.i++;//correct   }}classOther { Public inti;}

That's right.



But here it modifies the member variable i (member vairable) of O, and O's Reference (reference) does not change.
---------------------------------------------------------------------- -------
6.

 1  class   Something { 2  int   I;  3  public   void   dosomething () { 4  SYSTEM.OUT.P Rintln ("i =" + 5  }  6  } 

That's right.
The output is "i = 0". int I belongs to the member variable/instance variable (instant variable). Member variables have default values (default value). The default value for int i is 0.
-----------------------------------------------------------------------------
7.

class Something {    finalint  i;      Public void dosomething () {        System.out.println ("i =" + i);}    }

error.
The member variable that is final decorated has no default value, and must be given a definite value before the constructor (constructor) ends.

-----------------------------------------------------------------------------
8.

 public   Class   Something { public  static  void   main (String        [] args) {Something s  = new   Something ();    System.out.println ( s.dosomething () returns "+ dosomething ());  public   String dosomething () {    Span style= "COLOR: #0000ff" >return  "do something ..." ; }}

error.
There seems to be no problem in calling DoSomething () in main, after all, two methods are in the same class.
But look closely, the main method is static. A static method cannot call a non-static method directly.

Similarly, static methods cannot access non-static member variables (non-static instant variable).
---------------------------------------------------------------------- -------
9.

here, the file of the something class is called Otherthing.java class Something {    privatestaticvoid  main (string[] something_to_do) {                System.out.println ("Do something ...");     }

That's right.
No one has ever said that Java's class name must be the same as its file name. However, public class A {} must have the same name as the file name.
-----------------------------------------------------------------------------
10.

InterfaceA//because the default modifier for all member variables of an interface is: public static final   intx = 0;}
classB {intx = 1;}
classCextendsBImplementsA { Public voidPX () {System.out.println (x); } Public Static voidMain (string[] args) {NewC (). PX (); }}

Error.
Errors occur at compile time (error description different JVM has different information),
This means that there is an ambiguous x call, and two x matches (as if you were declaring a date directly when you import Java.util and java.sql two packets at the same time).
For a variable of the parent class, it can be explicitly called with super.x, whereas the member variable (property) of the interface has a default implied modifier of public static final int x =; can be explicitly called by a.x.
-----------------------------------------------------------------------------
11.

InterfacePlayable {//because the default modifier for all member methods of an interface is: Public abstract    voidPlay ();//The complete form is: Public abstract void Play ();}
Interfacebounceable {voidplay ();}
InterfaceRollableextendsPlayable, bounceable {//because the default modifier for all member variables of an interface is: public static finalBall Ball =NewBall ("Pingpang");//This is a member variable. The complete form is: public static final ball ball = new ball ("Pingpang");}//that is, Ball is a final variable (constant) whose type is the reference data type, which means that ball is no longer new.

classBallImplementsRollable {PrivateString name; PublicString GetName () {returnname; } PublicBall (String name) { This. Name =name; } Public voidPlay () { ball=NewBall ("Football"); System.out.println (Ball.getname ()); }}

Error.
Interfaces can inherit multiple interfaces, that is, interfaces can inherit more than one interface.
Any interface variable declared in the interface, also called a member variable, the default modifier is public static final.
That is: ball ball = new ball ("Pingpang"), is a member variable, is actually public static final ball ball = new ball ("Pingpang");.
In the ball class play () method, "ball = new ball" ("Football"); " Changed the ball's reference, and here the ball comes from the rollable interface,
The ball in the Rollable interface is public static final, and the final modified object cannot be changed by reference (reference).
So the compiler will be in ball = new ball ("Football"); The error is shown here.
-----------------------------------------------------------------------------

About Java object-oriented analytic problems

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.