OOP Java - rizrazi/1st-Assignment-of-OOP-Java GitHub Wiki

Rizwan Ali

Roll No: 37

Assignment of OOP Java

What is class and how to Declare?

A class is a template for manufacturing objects. You declare a class by specifying the class keyword followed by a non-reserved identifier that names it. A pair of matching open and close brace characters ({ and }) follow and delimit the class's body. Let's take a look at the standard syntax for declaring a Java class:

class identifier { // class body }

By convention, the first letter of a class's name is uppercased and subsequent characters are lowercased (for example, Employee). If a name consists of multiple words, the first letter of each word is uppercased (such as SavingsAccount). This naming convention is called camelcasing.

The following example declares a class named Book:

class Book { // class body }

What are Local and Class Variables?

Local Variable

A variable that is declared inside the method is called local variable.

Instance Variable

A variable that is declared inside the class but outside the method is called instance variable . It is not declared as static.

Static variable

A variable that is declared as static is called static variable. It cannot be local.

We will have detailed learning of these variables in next chapters. Example to understand the types of variables

class A{  
int data=50;//instance variable  
static int m=100;//static variable  
void method(){  
int n=90;//local variable  
}  
}//end of class  

what is constructor in java?

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 that is why it is known as constructor. 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

What is Java Access Modifier?

Java provides a number of access modifiers to set access levels for classes, variables, methods and constructors. The four access levels are:

Visible to the package. the default. No modifiers are needed.

Visible to the class only (private).

Visible to the world (public).

Visible to the package and all subclasses (protected).

Default Access Modifier - No keyword:

Default access modifier means we do not explicitly declare an access modifier for a class, field, method, etc.

A variable or method declared without any access control modifier is available to any other class in the same package. The fields in an interface are implicitly public static final and the methods in an interface are by default public. Example:

Variables and methods can be declared without any modifiers, as in the following examples:

String version = "1.5.1";

boolean processOrder() { return true; }

Private Access Modifier -Private:

Methods, Variables and Constructors that are declared private can only be accessed within the declared class itself.

Private access modifier is the most restrictive access level. Class and interfaces cannot be private.

Variables that are declared private can be accessed outside the class if public getter methods are present in the class.

Using the private modifier is the main way that an object encapsulates itself and hide data from the outside world. Example:

The following class uses private access control:

public class Logger { private String format; public String getFormat() { return this.format; } public void setFormat(String format) { this.format = format; } }

Here, the format variable of the Logger class is private, so there's no way for other classes to retrieve or set its value directly.

So to make this variable available to the outside world, we defined two public methods: getFormat(), which returns the value of format, and setFormat(String), which sets its value.

Public Access Modifier - public:

A class, method, constructor, interface etc declared public can be accessed from any other class. Therefore fields, methods, blocks declared inside a public class can be accessed from any class belonging to the Java Universe.

However if the public class we are trying to access is in a different package, then the public class still need to be imported.

Because of class inheritance, all public methods and variables of a class are inherited by its subclasses. Example:

The following function uses public access control:

public static void main(String[] arguments) { // ... }

The main() method of an application has to be public. Otherwise, it could not be called by a Java interpreter (such as java) to run the class.

Protected Access Modifier - protected:

Variables, methods and constructors which are declared protected in a superclass can be accessed only by the subclasses in other package or any class within the package of the protected members' class.

The protected access modifier cannot be applied to class and interfaces. Methods, fields can be declared protected, however methods and fields in a interface cannot be declared protected.

Protected access gives the subclass a chance to use the helper method or variable, while preventing a nonrelated class from trying to use it. Example:

The following parent class uses protected access control, to allow its child class override openSpeaker() method:

class AudioPlayer { protected boolean openSpeaker(Speaker sp) { // implementation details } }

class StreamingAudioPlayer { boolean openSpeaker(Speaker sp) { // implementation details } }

Here, if we define openSpeaker() method as private, then it would not be accessible from any other class other than AudioPlayer. If we define it as public, then it would become accessible to all the outside world. But our intension is to expose this method to its subclass only, thats why we used protected modifier.

What are Getters and Setters?

Getters ( Accessors)

Getters, or accessors, are methods that provide access to an object's instance variables. In essence, we are providing a layer of indirection. Simple classes often have getters that return the associated instance variable and nothing more. For example:

public String getTitle() { return title; }

public String getAuthor() { return author; }

public int getRating() { return rating; }

simply return the title, author, and rating instance variables, respectively. Note that the name of the first getter is getTitle. Prefixing the corresponding instance variable with "get" is a common naming convention for getters.

Getter methods shine in complex classes. For example, an object may need to perform network or database I/O to access the requested value. As such, having a getter method abstracts away the details of retrieving the desired value.

Setters (Mutators)

Setters, or mutators, are methods that provider the caller with an opportunity to update the value of a particular instance variable. Similar to getters, setters are often named by prefixing the corresponding instance variable with "set". For example:

public void setTitle(String title) { this.title = title; }

public void setAuthor(String author) { this.author = author; }

public void setRating(int rating) { this.rating = rating; }

Note that, unlikely getters, setters have no return value. A setter's job is usually limited to changing the value of an instance variable. Like getters, setters provide a useful layer of indirection to manipulating instance variables. Suppose we want our Book class to only using rating values between 0 and 5. The setter provides an excellent opportunity for enforcing these constraints:

public void setRating(int rating) { if (rating > 5) { this.rating = 5; } else if (rating < 0) { this.rating = 0; } else { this.rating = rating; } }

This improved setRating setter can also be used by Book's constructor to ensure that we only initialize objects with valid ratings:

public Book(String title, String author, int rating) { this.title = title; this.author = author; setRating(rating); }