Java lec class, objects and constructors
Upcoming SlideShare
Loading in...5
×
 

Like this? Share it with your network

Share

Java lec class, objects and constructors

on

  • 1,730 views

JAVA

JAVA

Statistics

Views

Total Views
1,730
Views on SlideShare
1,730
Embed Views
0

Actions

Likes
0
Downloads
12
Comments
0

0 Embeds 0

No embeds

Accessibility

Categories

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

Java lec class, objects and constructors Presentation Transcript

  • 1. JAVA CLASSES, OBJECTS AND CONSTRUCTORS JAVA LECTURE BY: LJRABOY
  • 2. Object-Oriented Approach • Class – Instance variables – attributes of an object – Methods – functions that manipulates objects • Are subprograms • Divide-and-conquer model • One large programs – subdivide the program into smaller subprograms • Process needed in the program • Methods are usually named using a action words such as computeSum(), setValueOfX(), getValueOfY() and etc – A user-defined method can be used after the declaration of main() as a static method. – Static method – does not need an object to access its methods and instance variables.
  • 3. Common example of methods • main() • System.out.println(); • JOptionPane.showMessageDialog(); • String str = JOptionPane.showInputDialog(); • Int y = Integer.parseInt(str);
  • 4. The Class • The class contains both the variables and its methods • Class is represented by its own filename • example of a declaration: public class MyClass{ public static void main(String args *[]) { } }
  • 5. The Class (cont) • Is a general category of an object • it contains the very common behavior of an object. • A class does not necessarily needs main() because a class can be used in terms of using objects. Together with its instance variables and instance methods.
  • 6. The Object • Instantiate an object using a new reserved word. • So all the instance variables and instance methods of the class can now be accessed by an objects. • This instance variables and methods can be accessed using the dot “.” operator. ObjectName.instance_Variable ObjectName.instance_method()
  • 7. Getting Started • Creating a class – Instance variables – Instance methods
  • 8. Example: • Start with creating a new Java Basic Application • Name your class: – Example: MyClassMain – Select File, New  File
  • 9. Select Java Class
  • 10. Provide another filename for your class
  • 11. Two files for the project
  • 12. Inside MyClassMain class
  • 13. Inside MyOwnClass class • Public  access modifier , can be accessed anywhere in the package • Reserved word class • MyOwnClass  User- defined class name • public MyOwnClass(){ }  constructor Constructor – used for initialization
  • 14. Modifying MyOwnClass
  • 15. MyClassMain class
  • 16. Format of using a method public return_type methodname(parameters) { // body of the method } Where: public is an access modifier Return_type – any primitive type/class type Methodname – user-defined method name Parameters – optional, values to be passed to the method