topic 08: creating classes

34
Topic 8 : Creating Classes DDOOCP

Upload: pradip-kharbuja

Post on 18-Jan-2015

549 views

Category:

Education


0 download

DESCRIPTION

Slides

TRANSCRIPT

Page 1: Topic 08: Creating Classes

Topic 8 : Creating ClassesDDOOCP

Page 2: Topic 08: Creating Classes

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

Page 3: Topic 08: Creating Classes

Class Example

Page 4: Topic 08: Creating Classes

Object

› Variables or Instance of a class are known as objects.

› The process of creating objects of a class is known as instantiation

Page 5: Topic 08: Creating Classes

Sample Class

Page 6: Topic 08: Creating Classes

SampleMain Class

Page 7: Topic 08: Creating Classes

Sample Class

› We can declare main() method in sample.java

Page 8: Topic 08: Creating Classes

FirstLayout Class

Page 9: Topic 08: Creating Classes

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.

Page 10: Topic 08: Creating Classes

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

Page 11: Topic 08: Creating Classes

Types of Methods

Page 12: Topic 08: Creating Classes

Types of Methods

Page 13: Topic 08: Creating Classes

Working with properties

Page 14: Topic 08: Creating Classes

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.

Page 15: Topic 08: Creating Classes

Using this object

Page 16: Topic 08: Creating Classes

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

Page 17: Topic 08: Creating Classes

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.

Page 18: Topic 08: Creating Classes

Default Constructor

Page 19: Topic 08: Creating Classes

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.

Page 20: Topic 08: Creating Classes

Method Overloading

› Method name must be same.

› Either number or type of parameters must be different.

› Return type doesn't effect method overloading.

Page 21: Topic 08: Creating Classes

Method Overloading

Page 22: Topic 08: Creating Classes

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.

Page 23: Topic 08: Creating Classes

Inner Class

Page 24: Topic 08: Creating Classes

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

Page 25: Topic 08: Creating Classes

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

Page 26: Topic 08: Creating Classes

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

Page 27: Topic 08: Creating Classes

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

Page 28: Topic 08: Creating Classes

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.

Page 29: Topic 08: Creating Classes

The Accessors : Get & Set (Getter & setters)

Page 30: Topic 08: Creating Classes

The Accessors : Get & Set (Getter & setters)

Page 31: Topic 08: Creating Classes

The Accessors : Get & Set (Getter & setters)

Page 32: Topic 08: Creating Classes

Class Diagram

› Diagrammatic representation of Java or any other OOP class.

Page 33: Topic 08: Creating Classes

More Class Diagrams

Page 34: Topic 08: Creating Classes

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