Thursday 27 April 2017

In Java protected Keyword

In Java protected Keyword

Protected specifiers allows the class itself, subclasses, and all classes in the same package to access the members. You should use the protected access level for those data members or member functions of a class, which you can be accessed by subclasses of that class, but not unrelated classes. You can see protected members as family secrets–you don’t mind if the whole family knows, and even a few trusted friends but you wouldn’t want any outsiders to know. A member can be declared protected using keyword protected.

public class Student
{
         protected int age;
         public String name;
         protected void protectedMethod()
        {
                  System.out.println("protectedMethod");
        }
}

Visit our site for Registration java coaching in jaipur

Private Keyword in Java

Private Keyword in Java

Private is the most restrictive access level. A private member is accessible only to the class in which it is defined. You should use this access to declare members that you are going to use within the class only. This includes variables that contain information if it is accessed by an outsider could put the object in an inconsistent state, or methods, if invoked by an outsider, could jeopardize the state of the object or the program in which it is running. You can see private members like secrets you never tell anybody.
To declare a private member, use the private keyword in its declaration. The following class contains one private member variable and one private method:
class First
{
private int MyPrivate; // private data member
private void privateMethod() // private member function
{
System.out.println("Inside privateMethod");
}
}
Objects of class First can access or modify the MyPrivate variable and can invoke privateMethod.Objects of other than class First cannot access or modify MyPrivate variable and cannot invoke privateMethod . For example, the Second class defined here:
class Second {
void accessMethod() {
First a = new First();
a. MyPrivate = 51; // illegal
a.privateMethod(); // illegal
}
}
cannot access the MyPrivate variable or invoke privateMethod of the object of First.
If you are attempting to access a method to which it does not have access in your program, you will see a compiler error like this:
Second.java:12: No method matching privateMethod()
found in class First.
a.privateMethod(); // illegal
1 error
One very interesting question can be asked, “whether one object of class First can access the private members of another object of class First”. The answer to this question is given by the following example. Suppose the First class contained an instance method that compared the current First object (this) to another object based on their iamprivate variables:
class Alpha
{
private int MyPrivate;
boolean isEqualTo (First anotherObject)
{
if (this. MyPrivate == anotherobject. MyPrivate)
return true;
else
return false;
}
}
This is perfectly legal. Objects of the same type have access to one another’s private members. This is because access restrictions apply at the class or type level (all instances of a class) rather than at the object level.

Visit our site for Registration java coaching in Jaipur

ACCESS CONTROL in Java

ACCESS CONTROL in Java


One of the objectives of having access control is that classes can protect their member data and methods from getting accessed by other objects. Why is this important? Well, consider this. You’re writing a class that represents a query on a database that contains all kinds of secret information; say student’s records or marks obtained by a student in final examination.
In your program you will have certain information and queries contained in the class. Class will have some publicly accessible methods and variables in your query object, and you may have some other queries contained in the class simply for the personal use of the class. These methods support the operation of the class but should not be used by objects of another type. In other words you can say–you’ve got secret information to protect.
How can you protect it?
Ok in Java, you can use access specifiers to protect both variables and methods of a class when you declare them. The Java language supports four distinct access specifiers for member data and methods: private, protected, public, and if left unspecified, package.
The following chart shows the access level permitted by each specifier.

The first column indicates whether the class itself has access to the members defined by the access specifier. As you can see, a class always has access to its own members.
The second column indicates whether subclasses of the class (regardless of which package they are in) have access to the member.
The third column indicates whether classes in the same package as the class (regardless of their parentage) have access to the member.
The fourth column indicates whether all classes have to the member.

Visit our site for java coaching in Jaipur

Superclass and Subclass in Java

Superclass and Subclass in Java


The superclass of a class A is the class from which class A is derived. In programming languages like C++, allow deriving a class from multiple classes at a time. When a class inherits from multiple super classes, the concepts is known as multiple inheritance. Java doesn’t support multiple inheritance. If there is a need to implement multiple inheritance.

A class derived from the superclass is called the subclass. Sometime-superclass is also called parent class or base class and subclass is called as child class or derived class. The subclass can reuse the data member and methods of the superclass that were already implemented and it can also extend or replace the behaviour in the superclass by overriding methods. Subclass can have its own data members and member functions.
You can see in this example program, the Employee class is used for tracking the hours an employ worked for along with the hourly wages, and the “attitude” which gives you a rough measure of their activeness or for what percentage of time they are actually productive.

public class Employee
{
protected double attitude;
protected int numHoursPerWeek, wagePerHour;
public Employee(int wage, int hours, double att) // constructor
{
wagePerHour = wage;
numHoursPerWeek = hours;
attitude = att;
}
public double getProductivity()
{
return numHoursPerWeek*attitude;
}
public double getTeamProductivity()
{
return getProductivity();
}
public int WeekSalary()
{
return wagePerHour*numHoursPerWeek;
}
}

If you look closely you will observe that Employee class possesses the very basic characteristics of an employee. So think quickly about different type of employees! Of course you can think about employees with special characteristics, for example, Manager Engineer, Machine-man etc. You are right Subclass of Employee, will have properties of Employee class as well as some more properties.


Visit our site for java training in jaipur

Inheritance in Java

Inheritance is a language property specific to the object-oriented paradigm. Inheritance is used for a unique form of code-sharing by allowing you to take the implementation of any given class and build a new class based on that implementation. Let us say class B, starts by inheriting all of the data and operations defined in the class A. This new subclass can extend the behaviour by adding additional data and new methods to operate on it. Basically during programming inheritance is used for extending the existing property of a class. In other words it can be said that inheritance is “from generalization- to-specialization”. In this by using general class, class with specific properties can be defined.

Now let us take the example of Employee class, which is declared as:

class BankAccount
{
data members
member functions
}

data members and member functions of BankAccount class are used to display characteristics of Withdraw, Deposit, getBalance of objects of a BankAccount class.

Now suppose you want to define a SavingAccount class. In the SavingAccount class definitely you will have basic characteristics of a Bankaccount class mentioned above. In your SavingAccount class you can use the properties of BankAccount class, without any modification in them. This use of properties of BankAccount class in Saving Account class is called inheriting property of BankAccount class into SavingAccount class.

To inherit a class into another class extends keyword is used. For example, SavingAccount class will inherit BankAccount class as given below.
class SavingAccount extends BankAccount
{
data members
ember functions
}
In this example, SavingAccount declares that it inherits or “extends” BankAccount.

Visit our site for java training in jaipur


Java inheritance in programming

In Object Oriented Programming, one of the major feature is reusability. You are already introduced to the reusability concept in this course. Reusability in Java is due to the inheritance. Inheritance describes the ability of one class or object to possess characteristics and functionality of another class or object in the hierarchy. Inheritance can be supported as “run-time” or “compile-time” or both. Here we are discussing inheritance with the Java programming language, and in our discussion we will generally refer to compile-time inheritance.

In this unit we will discuss importance of inheritance in programming, concept of superclass and subclass, and access controls in Java programming language. You will see through example programs how methods are overridden, and how methods of a super class are accessed by subclass. You will also see how multilevel inheritance is implemented, use of abstract classes, and demonstration of polymorphism with the help of example program.

Visit our site for java training in jaipur