Topic 08: Creating Classes
Upcoming SlideShare
Loading in...5
×
 

Like this? Share it with your network

Share

Topic 08: Creating Classes

on

  • 358 views

Slides

Slides

Statistics

Views

Total Views
358
Views on SlideShare
66
Embed Views
292

Actions

Likes
0
Downloads
21
Comments
0

3 Embeds 292

http://www.pradipkharbuja.com.np 289
https://m.facebook.com 2
http://webcache.googleusercontent.com 1

Accessibility

Categories

Upload Details

Uploaded via as Adobe PDF

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

Topic 08: Creating Classes Presentation Transcript

  • 1. Topic 8 : Creating Classes DDOOCP
  • 2. Overview of a Class › Class is a user defined data type or a template used for developing programs, which consists of methods and variables. › By convention, class names capitalize the initial of each word. › A class has a body. A class body may contain the following elements: 1. Fields (Attributes) 2. Constructor 3. Methods (Operations) 4. Other classes including inner classes
  • 3. Class Example
  • 4. Object › Variables or Instance of a class are known as objects. › The process of creating objects of a class is known as instantiation
  • 5. Sample Class
  • 6. SampleMain Class
  • 7. Sample Class › We can declare main() method in sample.java
  • 8. FirstLayout Class
  • 9. Methods › Methods are the functions that are used to perform some specific task. › A method can have any valid name. › Syntax › return_type : represents the type of value that a method is returning. If the method doesn’t return anything, its return type is void.
  • 10. Types of Methods › 4 types of methods 1. Method with no argument, no return type 2. Method with arguments, no return type 3. Method with no argument, return type 4. Method with arguments, return type
  • 11. Types of Methods
  • 12. Types of Methods
  • 13. Working with properties
  • 14. Using this object › Within an instance method or a constructor, this is a reference to the current object i.e. this — the object whose method or constructor is being called. › this is a keyword.
  • 15. Using this object
  • 16. Constructor › Constructors are the methods having the same name as the class name and no return type (not even void). › A constructor is automatically called when an object is initialized. › Constructor is used to initialize the starting values. › Two types of Constructor 1. Default Constructor 2. Parameterized Constructor
  • 17. Default Constructor › A constructor without any parameter is called a default constructor. › When no constructor is created explicitly, Java implicitly creates a constructor without parameter.
  • 18. Default Constructor
  • 19. Parameterized Constructor › Constructors having parameters. There are more than one constructor having same name but different arguments. This process is know as method overloading or constructor overloading.
  • 20. Method Overloading › Method name must be same. › Either number or type of parameters must be different. › Return type doesn't effect method overloading.
  • 21. Method Overloading
  • 22. Inner Class › The class that is created inside the other class is known as inner class. › The class in which the inner class resides is called outer class. › An inner class has full access to the member variables of the outer class. › The benefit of using inner class is that inner class is visible to its outer class only. › The object of the inner class can be created in its outer class.
  • 23. Inner Class
  • 24. Packages › A package can be considered as a directory or folder that allows you to store various classes related to each other. › Packages resolve the problem of class name collision. › Types of packages 1. Built-in packages 2. User-defined packages
  • 25. Built-In Packages 1. java.lang 2. java.math 3. java.util 4. java.io 5. java.awt 6. java.net 7. java.text 8. java.sql 9. javax.swing
  • 26. User-defined Packages › The package created by the user. › To create a package, the package statement is used. › A package statement must be the first statement of Java source file. › By convention, package name is written in lowercase. Package Demo
  • 27. Access Modifier or Visibility › Access level modifiers determine whether other classes can use a particular field, methods. › If there is no modifier (the default, also known as package-private), it is visible only within its own package. 1. public 2. private 3. default 4. protected Access Modifier Demo
  • 28. The Accessors : Get & Set (Getter & setters) › To maintain security of the program, it has been advised to declare, where possible variables/attributes as private. › The getters and setters are the methods that can be programmed to enable programmed access to private attributes with less security risk compared to declaring the attributes public. › It is better to declare attributes as private or protected but that is not always possible. › Methods are (generally) declared as public.
  • 29. The Accessors : Get & Set (Getter & setters)
  • 30. The Accessors : Get & Set (Getter & setters)
  • 31. The Accessors : Get & Set (Getter & setters)
  • 32. Class Diagram › Diagrammatic representation of Java or any other OOP class.
  • 33. More Class Diagrams
  • 34. References › http://www.java2s.com/Tutorial/Java/0100__Class- Definition/0020__Defining-Class.htm › http://docs.oracle.com/javase/tutorial/java/javaOO/accesscont rol.html › http://stackoverflow.com/questions/215497/in-java-whats-the- difference-between-public-default-protected-and-private