Topic 8 : Creating Classes
DDOOCP
COMPILED BY: ER. PRADIP KHARBUJA
Overview of a Class
› Class is a user defined data type or a template used for developing
programs, which consists of meth...
Class Example
COMPILED BY: ER. PRADIP KHARBUJA
Object
› Variables or Instance of a class are known as objects.
› The process of creating objects of a class is known as i...
Sample Class
COMPILED BY: ER. PRADIP KHARBUJA
SampleMain Class
COMPILED BY: ER. PRADIP KHARBUJA
Sample Class
› We can declare main() method in sample.java
COMPILED BY: ER. PRADIP KHARBUJA
FirstLayout Class
COMPILED BY: ER. PRADIP KHARBUJA
Methods
› Methods are the functions that are used to perform some specific
task.
› A method can have any valid name.
› Syn...
Types of Methods
› 4 types of methods
1. Method with no argument, no return type
2. Method with arguments, no return type
...
Types of Methods
COMPILED BY: ER. PRADIP KHARBUJA
Types of Methods
COMPILED BY: ER. PRADIP KHARBUJA
Working with properties
COMPILED BY: ER. PRADIP KHARBUJA
Using this object
› Within an instance method or a
constructor, this is a reference to the
current object i.e. this — the ...
Using this object
COMPILED BY: ER. PRADIP KHARBUJA
Constructor
› Constructors are the methods having the same name as the class
name and no return type (not even void).
› A ...
Default Constructor
› A constructor without any parameter is called a default
constructor.
› When no constructor is create...
Default Constructor
COMPILED BY: ER. PRADIP KHARBUJA
Parameterized Constructor
› Constructors having parameters.
There are more than one
constructor having same
name but diffe...
Method Overloading
› Method name must be same.
› Either number or type or order of parameters must be different.
› Return ...
Method Overloading
COMPILED BY: ER. PRADIP KHARBUJA
Inner Class
› The class that is created inside the other class is known as inner
class.
› The class in which the inner cla...
Inner Class
COMPILED BY: ER. PRADIP KHARBUJA
Packages
› A package can be considered as a directory or folder that allows
you to store various classes related to each o...
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. ja...
User-defined Packages
› The package created by the user.
› To create a package, the package statement is used.
› A package...
Access Modifier or Visibility
› Access level modifiers determine whether other classes can use a
particular field, methods...
The Accessors : Get & Set (Getter & setters)
› To maintain security of the program, it has been advised to declare,
where ...
The Accessors : Get & Set (Getter & setters)
COMPILED BY: ER. PRADIP KHARBUJA
The Accessors : Get & Set (Getter & setters)
COMPILED BY: ER. PRADIP KHARBUJA
The Accessors : Get & Set (Getter & setters)
COMPILED BY: ER. PRADIP KHARBUJA
Class Diagram
› Diagrammatic representation of Java or any other OOP class.
COMPILED BY: ER. PRADIP KHARBUJA
More Class Diagrams
COMPILED BY: ER. PRADIP KHARBUJA
References
› http://www.java2s.com/Tutorial/Java/0100__Class-
Definition/0020__Defining-Class.htm
› http://docs.oracle.com...
Upcoming SlideShare
Loading in...5
×

Topic 08: Creating Classes

1,485

Published on

Slides

Published in: Education
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
1,485
On Slideshare
0
From Embeds
0
Number of Embeds
4
Actions
Shares
0
Downloads
69
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Topic 08: Creating Classes

  1. 1. Topic 8 : Creating Classes DDOOCP COMPILED BY: ER. PRADIP KHARBUJA
  2. 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 COMPILED BY: ER. PRADIP KHARBUJA
  3. 3. Class Example COMPILED BY: ER. PRADIP KHARBUJA
  4. 4. Object › Variables or Instance of a class are known as objects. › The process of creating objects of a class is known as instantiation COMPILED BY: ER. PRADIP KHARBUJA
  5. 5. Sample Class COMPILED BY: ER. PRADIP KHARBUJA
  6. 6. SampleMain Class COMPILED BY: ER. PRADIP KHARBUJA
  7. 7. Sample Class › We can declare main() method in sample.java COMPILED BY: ER. PRADIP KHARBUJA
  8. 8. FirstLayout Class COMPILED BY: ER. PRADIP KHARBUJA
  9. 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. COMPILED BY: ER. PRADIP KHARBUJA
  10. 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 COMPILED BY: ER. PRADIP KHARBUJA
  11. 11. Types of Methods COMPILED BY: ER. PRADIP KHARBUJA
  12. 12. Types of Methods COMPILED BY: ER. PRADIP KHARBUJA
  13. 13. Working with properties COMPILED BY: ER. PRADIP KHARBUJA
  14. 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. COMPILED BY: ER. PRADIP KHARBUJA
  15. 15. Using this object COMPILED BY: ER. PRADIP KHARBUJA
  16. 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 COMPILED BY: ER. PRADIP KHARBUJA
  17. 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. COMPILED BY: ER. PRADIP KHARBUJA
  18. 18. Default Constructor COMPILED BY: ER. PRADIP KHARBUJA
  19. 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. COMPILED BY: ER. PRADIP KHARBUJA
  20. 20. Method Overloading › Method name must be same. › Either number or type or order of parameters must be different. › Return type doesn't effect method overloading. COMPILED BY: ER. PRADIP KHARBUJA
  21. 21. Method Overloading COMPILED BY: ER. PRADIP KHARBUJA
  22. 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. COMPILED BY: ER. PRADIP KHARBUJA
  23. 23. Inner Class COMPILED BY: ER. PRADIP KHARBUJA
  24. 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 COMPILED BY: ER. PRADIP KHARBUJA
  25. 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 COMPILED BY: ER. PRADIP KHARBUJA
  26. 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 COMPILED BY: ER. PRADIP KHARBUJA
  27. 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 COMPILED BY: ER. PRADIP KHARBUJA
  28. 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. COMPILED BY: ER. PRADIP KHARBUJA
  29. 29. The Accessors : Get & Set (Getter & setters) COMPILED BY: ER. PRADIP KHARBUJA
  30. 30. The Accessors : Get & Set (Getter & setters) COMPILED BY: ER. PRADIP KHARBUJA
  31. 31. The Accessors : Get & Set (Getter & setters) COMPILED BY: ER. PRADIP KHARBUJA
  32. 32. Class Diagram › Diagrammatic representation of Java or any other OOP class. COMPILED BY: ER. PRADIP KHARBUJA
  33. 33. More Class Diagrams COMPILED BY: ER. PRADIP KHARBUJA
  34. 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 COMPILED BY: ER. PRADIP KHARBUJA
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×