1 | 1. Java is an Object-Oriented Language. |
2 | 2. In this chapter, we will look into the concepts Classes and Objects. |
3 | 3. Object - Objects have states and behaviors. |
4 | 4. Example: A dog has states - color, name, breed as well as behaviors -wagging, barking, eating. |
5 | 5. An object is an instance of a class. |
6 | 6. Class - A class can be defined as a template/blue print that describes the behaviors/states that object of its type support. |
7 | 7. Let us now look deep into what are objects. |
8 | 8. If we consider the real-world we can find many objects around us, Cars, Dogs, Humans, etc. |
9 | 9. All these objects have a state and behavior. |
10 | 10. If you compare the software object with a real world object, they have very similar characteristics. |
11 | 11. Software objects also have a state and behavior. |
12 | 12. A software object's state is stored in fields and behavior is shown via methods. |
13 | 13. So in software development, methods operate on the internal state of an object and the object-to-object communication is done via methods. |
14 | 14. A class is a blue print from which individual objects are created. |
15 | 15. A class can contain any of the following variable types. |
16 | 16. Local variables: Variables defined inside methods, constructors or blocks are called local variables. |
17 | 17. The variable will be declared and initialized within the method and the variable will be destroyed when the method has completed. |
18 | 18. Instance variables: Instance variables are variables within a class but outside any method. |
19 | 19. These variables are initialized when the class is instantiated. |
20 | 20. Instance variables can be accessed from inside any method, constructor or blocks of that particular class. |
21 | 21. Class variables: Class variables are variables declared with in a class, outside any method, with the static keyword. |
22 | 22. A class can have any number of methods to access the value of various kinds of methods. |
23 | 23. In the above example, barking(), hungry() and sleeping() are methods. |
24 | 24. Below mentioned are some of the important topics that need to be discussed when looking into classes of the Java Language. |
25 | 25. When discussing about classes, one of the most important sub topic would be constructors. |
26 | 26. Every class has a constructor. |
27 | 27. If we do not explicitly write a constructor for a class the Java compiler builds a default constructor for that class. |
28 | 28. Each time a new object is created, at least one constructor will be invoked. |
29 | 29. The main rule of constructors is that they should have the same name as the class. |
30 | 30. A class can have more than one constructor. |
31 | 31. Java also supports Singleton Classes where you would be able to create only one instance of a class. |
32 | 32. Note: We have two different types of constructors we are going to discuss constructors in detail in coming chapters. |
33 | 33. As mentioned previously, a class provides the blueprints for objects. |
34 | 34. So basically an object is created from a class. |
35 | 35. In Java, the new key word is used to create new objects. |
36 | 36. Declaration: A variable declaration with a variable name with an object type. |
37 | 37. Instantiation: The 'new' key word is used to create the object. |
38 | 38. Initialization: The 'new' keyword is followed by a call to a constructor. |
39 | 39. This call initializes the new object. |
40 | 40. Instance variables and methods are accessed via created objects. |
41 | 41. As the last part of this section let's now look into the source file declaration rules. |
42 | 42. There can be only one public class per source file. |
43 | 43. A source file can have multiple non public classes. |
44 | 44. The public class name should be the name of the source file as well which should be appended by .java at the end. |
45 | 45. If the class is defined inside a package, then the package statement should be the first statement in the source file. |
46 | 46. If import statements are present then they must be written between the package statement and the class declaration. |
47 | 47. If there are no package statements then the import statement should be the first line in the source file. |
48 | 48. Import and package statements will imply to all the classes present in the source file. |
49 | 49. It is not possible to declare different import and/or package statements to different classes in the source file. |
50 | 50. Classes have several access levels and there are different types of classes; abstract classes, final classes, etc. |
51 | 51. Apart from the above mentioned types of classes, Java also has some special classes called Inner classes and Anonymous classes. |
52 | 52. In simple, it is a way of categorizing the classes and interfaces. |
53 | 53. When developing applications in Java, hundreds of classes and interfaces will be written, therefore categorizing these classes is a must as well as makes life much easier. |
54 | 54. In Java if a fully qualified name, which includes the package and the class name, is given then the compiler can easily locate the source code or classes. |
55 | 55. Import statement is a way of giving the proper location for the compiler to find that particular class. |
56 | 56. For our case study, we will be creating two classes. |
57 | 57. They are Employee and EmployeeTest. |
58 | 58. First open notepad and add the following code. |
59 | 59. Remember this is the Employee class and the class is a public class. |
60 | 60. Now, save this source file with the name Employee.java. |
61 | 61. The Employee class has four instance variables name, age, designation and salary. |
62 | 62. The class has one explicitly defined constructor, which takes a parameter. |
63 | 63. As mentioned previously in this tutorial, processing starts from the main method. |
64 | 64. Therefore in-order for us to run this Employee class there should be main method and objects should be created. |
65 | 65. We will be creating a separate class for these tasks. |
66 | 66. Next session will discuss basic data types in Java and how they can be used when developing Java applications. |
67 | 67. © Copyright 2015. |
68 | 68. All Rights Reserved. |
Snippet is not live.
Travelled to 12 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt
No comments. add comment
Snippet ID: | #3000421 |
Snippet name: | Sentences from #3000030 |
Eternal ID of this version: | #3000421/1 |
Text MD5: | 105b6928f9ed71f22f6de86263a8d326 |
Author: | someone |
Category: | |
Type: | New Tinybrain snippet |
Gummipassword: | #1000858 |
Uploaded from IP: | 176.5.92.56 |
Public (visible to everyone): | Yes |
Archived (hidden from active list): | No |
Created/modified: | 2016-10-26 15:58:22 |
Source code size: | 5801 bytes / 68 lines |
Pitched / IR pitched: | No / No |
Views / Downloads: | 431 / 104 |
Referenced in: | [show references] |