You are on page 1of 6

Java Modifier Types Access Control Modifiers: Java provides a number of access modifiers to set access levels for

classes, variables, methods and constructors. The four access levels are: 1. 2. 3. 4. 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).

Non Access Modifiers: Java provides a number of non-access modifiers to achieve many other functionality.

The static modifier for creating class methods and variables The final modifier for finalizing the implementations of classes, methods, and variables. The abstract modifier for creating abstract classes and methods. The synchronized and volatile modifiers, which are used for threads.

The finalize( ) Method: It is possible to define a method that will be called just before an object's final destruction by the garbage collector. This method is called finalize( ), and it can be used to ensure that an object terminates cleanly. For example, you might use finalize( ) to make sure that an open file owned by that object is closed. The finalize( ) method has this general form: protected void finalize( ) { // finalization code here }

"this" keyword in java: The keyword this is useful when you need to refer to instance of the class from its method. The keyword helps us to avoid name conflicts. As we can see in the program that we have declare the name of instance variable and local variables same. Now to avoid the confliction between them we use this keyword.

Static Keyword
The static keyword mainly can be used in 2 scenarios 1) static variables 2) static methods static variable

It is a variable which belongs to the class and not to object(instance) Static variables are initialized only once , at the start of the execution . These variables will be initialized first, before the initialization of any instance variables A single copy to be shared by all instances of the class A static variable can be accessed directly by the class name and doesnt need any object Syntax : <class-name>.<variable-name>

static method

It is a method which belongs to the class and not to the object(instance) A static method can access only static data. It can not 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 doesnt need any object Syntax : <class-name>.<method-name> A static method cannot refer to "this" or "super" keywords in anyway

Inheritance in JAVA
The following kinds of inheritance are there in java.

Simple Inheritance Multilevel Inheritance

Pictorial Representation of Simple and Multilevel Inheritance

Simple Inheritance Simple Inheritance

Multilevel Inheritance

When a subclass is derived simply from it's parent class then this mechanism is known as simple inheritance. In case of simple inheritance there is only a sub class and it's parent class. It is also called single inheritance or one level inheritance.

Example:
class A { int x; int y; int get(int p, int q){ x=p; y=q; return(0); } void Show(){ System.out.println(x); } } class B extends A { public static void main(String args[]){ A a = new A(); a.get(5,6); a.Show(); } void display(){ System.out.println("B"); } }

Multilevel Inheritance

It is the enhancement of the concept of inheritance. When a subclass is derived from a derived class then this mechanism is known as the multilevel inheritance. The derived class is called the subclass or child class for it's parent class and this parent class works as the child class for it's just above ( parent ) class. Multilevel inheritance can go up to any number of level. Example:
class A { int x; int y; int get(int p, int q) { x=p; y=q; return(0); } void Show() { System.out.println(x); } } class B extends A { void Showb(){ System.out.println("B"); } } class C extends B { void display(){ System.out.println("C"); } } class D { public static void main(String args[]) { C c = new C(); c.get(5,6); c.Show(); c.Showb(); c.display(); } }

Java does not support multiple Inheritance super keyword The super is java keyword. As the name suggest super is used to access the members of the super class. It is used for two purposes in java.

1. to access the hidden data variables of the super class hidden by the sub class. 2. to call super class constructor in the subclass. e.g. Suppose class A is the super class that has two instance variables as int a and float b. class B is the subclass that also contains its own data members named a and b. then we can access the super class (class A) variables a and b inside the subclass class B just by calling the following command. super.member;
Class A{ int a; float b; void Show(){ System.out.println("b in super class: } } class B extends A { int a; float b; B( int p, float q) { a = p; super.b = q; } void Show() { super.Show(); System.out.println("b in super class: " + super.b); System.out.println("a in sub class: " + a); } public static void main(String[] args) { B subobj = new B(1, 5); subobj.Show(); } }

" + b);

Output: C:\>java B b in super class: 5.0 b in super class: 5.0 a in sub class: 1 Use of super to call super class constructor: The second use of the keyword super in java is to call super class constructor in the subclass.

super(param-list); Here parameter list is the list of the parameter requires by the constructor in the super class. Super must be the first statement executed inside a super class constructor. If we want to call the default constructor then we pass the empty parameter list.
class A{ int a; int b; int c; A(int p, int q, int r){ a=p; b=q; c=r; } } class B extends A{ int d; B(int l, int m, int n, int o){ super(l,m,n); d=o; } void Show(){ System.out.println("a = " + a); System.out.println("b = " + b); System.out.println("c = " + c); System.out.println("d = " + d); } public static void main(String args[]){ B b = new B(4,3,8,7); b.Show(); } }

You might also like