Ch-2 Objects and Classes
What is class?
A class can be defined as a template/blue print that describes the behaviors/states that object of
its type support.
A class can have any number of methods to access the value of various kinds of methods.
class class_name
void method1()
void method2()
void method3()
What is method?
A method is basically a behavior.
A class can contain many methods.
It is in methods where the logics are written, data is manipulated and all the actions are
executed.
In Java, as in other object-oriented programming languages, classes can be derived from other
classes.
The derived class (the class that is derived from another class) is called a subclass. The class
from which its derived is called the superclass.
What is constructor?
Constructor in java is a special type of method that is used to initialize the object.
Java constructor is invoked at the time of object creation.
It constructs the values i.e. provides data for the object.
Rules for creating java constructor
There are basically two rules defined for the constructor.
• Constructor name must be same as its class name.
• Constructor must have no explicit return type.
Types of java constructors
There are two types of constructors:
• Default constructor (no-arg constructor)
• Parameterized constructor
Default Constructor
• A constructor that have no parameter is known as default constructor.
Parameterized constructor
• A constructor that have parameters is known as parameterized constructor.
METHOD OVERLOADING
1) Method overloading is used to increase the readability of the program.
2) Method overloading is performed within class.
3) In method overloading, parameter must be different.
4) Method overloading is the example of compile time polymorphism.
Access modifiers of Java
The access modifiers are categorized into four types -
1) Public
2) Private
3) Protected
4) Default
Public –
The access level of a public modifier is everywhere.
It can be accessed from within the class, outside the class, within the package and outside the
package.
Private –
The access level of a private modifier is only within the class.
It cannot be accessed from outside the class.
3) Protected –
The access level of a protected modifier is within the package and outside the package through
child class.
Default –
The access level of a default modifier is only within the package.
It cannot be accessed from outside the package. If you do not specify any access level, it will be
the default.
Static Variables
• Class variables also known as static variables are declared with the static keyword in a
class, but outside a method, constructor or a block.
• Static variables are stored in the static memory.
• Static variables are rarely used other than being declared as constants.
• We can create static variables at class-level only.
Static methods in Java
• Static method in Java is a method which belongs to the class and not to the object.
• A static method can access only static data.
• A static method can access only static data. It cannot access non-static data (instance
variables).
• A static method can call only other static methods and can not call a non-static method
from it
• A static method can be accessed directly by the class name and doesn’t need any object
• A static method cannot refer to “this” or “super” keywords in anyway
Packages
Package in Java is a mechanism to encapsulate a group of classes, sub packages and interfaces.
A package is nothing but a physical folder structure (directories) that contains a group of related
classes, interfaces, and sub-packages according to their functionality.
Packages are divided into two categories:
• Built-in Packages (packages from the Java API)
• User-defined Packages (create your own packages)
• Built-in Packages
These packages consist of a large number of classes which are a part of Java API.
1) java.lang: Contains language support classes(e.g classed which defines primitive data
types, math operations).
This package is automatically imported.
2) java.io: Contains classed for supporting input / output operations.
3) java.util: Contains utility classes which implement data structures like Linked List,
Dictionary and support ; for Date / Time operations.
4) java.applet: Contains classes for creating Applets.
5) java.awt: Contain classes for implementing the components for graphical user interfaces
(like button , ;menus etc).
6) java.net: Contain classes for supporting networking operations.
User-defined packages
These are the packages that are defined by the user.
First we create a directory myPackage (name should be same as the name of the package).
Then create the MyClass inside the directory with the first statement being the package
names.
// Name of the package must be same as the directory
// under which this file is saved package myPackage;
public class MyClass
public void getNames(String s)
{
System.out.println(s);
• Providing controlled access: protected and default have package level access control. A
protected member is accessible by classes in the same package and its subclasses.
• A default member (without any access specifier) is accessible by classes in the same
package only.
• Packages can be considered as data encapsulation (or data-hiding).
Package Definition constraints
• Preventing naming conflicts. For example there can be two classes with name Employee
in two packages.
• Making searching/locating and usage of classes, interfaces, enumerations and annotations
easier.
Advantages of using packages
• Re-usability: The classes contained in the packages of another program can be easily
reused
• Name Conflicts: Packages help us to uniquely identify a class
• Controlled Access: Offers access protection such as protected classes, default classes
and private class
• Data Encapsulation: They provide a way to hide classes, preventing other programs
from accessing classes that are meant for internal use only