• No results found

Class level access modifiers :

In document Core Java Meterial (Page 83-88)

Operators & Assignments

Case 2 : Among static imports the Explicit static import will get the precedence.

2. Class level access modifiers :

Once we r creating a class, we should specify some meaning f… information (whether object creation is allowed or not) hid class creation is allowed) about our class to the JVM. We can specify, this information by using the corresponding access specified or modifier.

The only allowed modifiers, for the top level classes are : i. public

ii. default

iii. final (top level classes & inner classes) iv. abstract

v. strctfp

If we use any other modifier, we will get a compile-time error. Saying: - modifier < modifier-name> not allowed.

Public :

Public classes :

1. If a class declared as public, we can access that class from anywhere ie within or from outside the package or even from the N/W also . ⇒ from remote area also we can access.

Eg : Package pack 1 ; Compile : >javac –d  A.java.

/* Public */ class A {

Public void M1( ) {

SOP (“In A’S M1”) }

}

Package Pack 2;

// import Pack 1. A; Compile : >javac –d  B.java.

class B { P S V M (S[ ] a) { A al = new A( ); A1.Ml( ); } }

If class A in pack 1 is not declared as ‘public’, then while compiling class B, we will get a CTE, Saying :

Pack 1. A in pack 1 is not public and cannot be accessed from outside package. (Class A if not mentioned as public will be treated as default and default classes can’t be accessed from outside the package).

Issue can be resolved by mentioning class A in pack 1 as public Run : > java pack 2/B. O/P. in A’s M1.

Default Classes :

1. If a class is declared as the default, we r allowed to access that class from within the package only, if we r trying to access from outside package, it results in CTE.

2. Default access is also known as “Package Level Access”

3. There is no default keyword for class level modifiers. But “default” keyword is there for switch statement.

Abstract Classes :

1. Would be used for good programming practice. Most used by real-time experts. 2. “abstract” is a keyword which can be applied for classes and methods. i.e we

can’t apply abstract keyword for variables.

3. If we don’t know about implementation of a method, still we r allowed to declare such type of methods in our classes by using abstract modifier.

4. if a method is declared as abstract in a class, it indicates that we don’t know about implementation, child class is responsible for providing implementation. The following is the valid abstract method declaration

abstract int no of wheels ( );

Note : ( ; ) is mandatory and it indicates no implementation is defined here.

But, abstract int no of wheels ( ){ }; results in CTE.

Because { } indicates implementation which is not allowed for an abstract method.

5. ‘abstract’ is the term which never talks about implementation. Hence it is illegal to combine “abstract” keyword with any of the modifier which talks about implementation. So, the following combinations r illegal, in case of methods.

final syncharized abstract native private static strictfp

The valid combination r : abstract → public, protected. Concrete methods : Methods having body.

6. If a class declared as abstract, then we r not allowed to create an instance of that class. If the class contains, at least one abstract method, it indicates that the implementation is not complete. Then we r not allowed to create an instance of that class. Hence it must be declared as “abstract”. ⇒ Class containing atleast one abstract method must be declared as abstract, violation leads to CTE.

Eg : abstract class vehicle {

abstract int no of wheels ( ); }

7. It is not mandatory that abstract classes should at least contain single abstract method i.e abstract classes have a possibility of having zero (0) abstract method.

Eg : HhpServlet class is an abstract class which doesn’t contain a single abstract method.

What is d use of creating HhpServlet as abstract class ?

8. If u don’t want to create an instance of the class, declare that class as abstract class; whether it contains abstract methods or not.

9. Inside abstract classes we r allowed to keep constructors, i.e an abstract class may contain constructors but the programmer is not allowed to create an object of abstract class, but internally JVM is allowed to create an instance.

10. The 1st child class extending an abstract class is responsible to provide

implementation for all the abstract methods present in the parent class, otherwise the child class should also be declared as “abstract”.

Final Classes :

1. ‘final’ is a keyword which can be applied for the classes methods and variables. 2. final methods :

If a method declared as final, we r not allowed to over side this method in the child class violation leads to CTE.

Eg : Public class A {

final public void M1( ) {

SOP (“A’s M1 Method”); } } Class B extends A { Public void M1( ) { SOP (“B’s M1 Method”); } }

CTE : M1( ) in B can’t override M1( ) in A.; Overridden Method is final

If a class declared as final, then u r not allowed to create the child class. Violation leads to CTE.

Eg : final public class a class B extends A

{ { };

P V M1( ) CTE : Can’t inherit from final ‘A’. {

SOP (“Pandu”); }

}

Note : Until & unless there is no specific requirement, don’t use final keyword

in the real-time coding, because the programmer is missing the wonderful key concept of object – oriented programming – INHERITANCE

Eg : for final classes :

String, Wrapper Classes, Math…..etc String Buffer, String Builder…..

Comparison between final and abstract :

1. An abstract method means : it never talks about implementation.

A final method means its implementation is final. No one allowed to override this method. Hence, final and abstract combination is illegal combination, for the methods.

2. An abstract class means, we should create the child class to provide implementation for the abstract methods.

But, final class means, we should not create the child class because parent class implementation is final.

Hence final and abstract combination is illegal for classes also.

3. A final class never allows to constrain abstract methods but an abstract class may contain final methods.

Eg : final class A abstract class A

abstract void M1( ); final … void M1( );

} final void M1( ) { };

}

Strict fp : (floating point)

1. If U perform 10.0/3 the result is 3.333…. ‘3’ repeats after decimal point unit the O/S (or) processor – bit, size supports. We say Java as P/f independent but the floating point arithmetic results in P/f dependency. Which is sense less.

2. Strictfp is a keyword which can be applied only for methods and classes i.e we r not allowed to declare a variable as a strictfp.

3. If a method declared as a strictfp all the floating point calculations has to

In document Core Java Meterial (Page 83-88)

Related documents