topic 08: creating classes
DESCRIPTION
SlidesTRANSCRIPT
Topic 8 : Creating ClassesDDOOCP
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
Class Example
Object
› Variables or Instance of a class are known as objects.
› The process of creating objects of a class is known as instantiation
Sample Class
SampleMain Class
Sample Class
› We can declare main() method in sample.java
FirstLayout Class
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.
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
Types of Methods
Types of Methods
Working with properties
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.
Using this object
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
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.
Default Constructor
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.
Method Overloading
› Method name must be same.
› Either number or type of parameters must be different.
› Return type doesn't effect method overloading.
Method Overloading
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.
Inner Class
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
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
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
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
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.
The Accessors : Get & Set (Getter & setters)
The Accessors : Get & Set (Getter & setters)
The Accessors : Get & Set (Getter & setters)
Class Diagram
› Diagrammatic representation of Java or any other OOP class.
More Class Diagrams
References
› http://www.java2s.com/Tutorial/Java/0100__Class-Definition/0020__Defining-Class.htm
› http://docs.oracle.com/javase/tutorial/java/javaOO/accesscontrol.html
› http://stackoverflow.com/questions/215497/in-java-whats-the-difference-between-public-default-protected-and-private