Some Consequences
class:
default: class Hello {}
- no package declaration, no public declaration to class.
- Hence default class can be accessed only within the directory
public: public class A {}
- public class without package declaration (or) package declaration without public class has no use.
- public classes can be accessed both within and outside the package
abstract:
when to declare class as abstract?
how to declare abstract class?
abstract class Math {
int add(int a, int b) { // having implementation
return a + b;
}
int mul(int a, int b) { // having implementation ; implemented method
return a * b;
}
abstract int sub(int a, int b); // only declaration; such methods are called as abstract methods
}
- what to do with abstract class?
- We cannot instantiate abstract class. Math m=new Math(); // gives error
- We can only inherit abstract class
- Inherited sub class must implement abstract super class, abstract method
- Then sub class can be instantiated and all the methods can be called/accessed
- If sub class don't implement abstract class-abstract method, then the sub class also must be declared as abstract
Inheritance
class MyMath extends Math {
int sub(int a, int b) { // implementing abstract method
return a - b;
}
}
class MyMathClient {
public static void main(String rags[]) {
// Instantiation
MyMath mm=new MyMath();
int output=mm.add(10,20);
System.out.println(output);
int output=mm.sub(10,20);
System.out.println(output);
int output=mm.mul(10,20);
System.out.println(output);
}
}
final:
- when to declare class as final?
- how to declare class as final?
example before final class:
class NFC {
void m1() {}
void m2() {}
final void m3() {}
final void m4() {}
}
class NFCSC extends NFC {
void m1() {} // overriding / rewriting super class method
// void m3() {} // we will get error; final methods cannot be overriden
// void m4() {} // we will get error; final methods cannot be overriden
}
class NFCIC {
public static void main(String rags[]) {
NFC nfc=new NFC();
nfc.m1();
nfc.m2();
nfc.m3();
nfc.m4();
}
}
- what to do with final class?
- final class cannot be inherited. we cannot write sub classes to final class
- it can be only instantiated
- if we don't want to override all our class methods in sub class, then instead of declaring every methods as final, declare class itself as final.
- When there is no inheritance there is no overriding
final class FC {
void m1() {}
void m2() {}
void m3() {}
void m4() {}
}
class FCSC extends FC { // we will get error saying final class cannot be sub classed
}
class FCIC {
public static void main(String rags[]) {
FC fc=new FC();
fc.m1();
fc.m2();
fc.m3();
fc.m4();
}
}
Variables:
private: var declared as private can be accessible only within the class
default: var declared as default can be accessible within the directory/package
protected: var declared as protected can be accessible within the directory/package and upto subclass outside the package
public: var declared as public can be accessible within and outside the package
final: final variables must be initialized at the time of declaration, they cannot be reinitialized.
class FVC {
final int MIN_AGE=18;
void printFinalVar() {
MIN_AGE=20; // compilation error saying final variable cannot be reinitialized
System.out.println(MIN_AGE);
}
}
Static:
- Variables declared in a class are having instance memory for every object of class. Hence variables declared in class are called as instance variables/dynamic variables. The same thing happens to final variable also.
- Though final variable is having a constant/unchanged value, in every instance of class the memory is allocated to final variables also which yields to unnecessary memory occupation/memory wastage.
- Because final variable values are constant for any number of instances of the class, for such variables dynamic memory is not recommended from memory point of view, declare memory only once for final variable for any number of instances of the class.
- How to allocate only one time memory for a variable?
Declare such variables as static. static variables will have memory in class memory but not in instance memory. The example
for such static final variable is MIN_AGE.
Sometimes we need to declare static alone variables also without final. Whichever variables want a common/single memory for
any number of objects of a class are called as static variables. static variables will not have instance memory.
class Emp {
int eid; // 0; non-static var/dynamic var/instance var/class instance var
String ename; // null
double sal; // 0.0
String desig; // null
static final int MIN_AGE=18; // static var/class variable
static int counter;
static String hiddenName="My Emp";
// no parameter constructor
Emp() {
counter++;
}
// parameterized constructor
// object creation and initialization time can be greately reduced with constructor
Emp(int eid, String en, double sal, String desig) {
this.eid=eid;
ename=en;
this.sal=sal;
this.desig=desig;
counter++;
}
}
class EmpClient {
public static void main(String rags[]) {
/*
Emp e1=new Emp();
e1.eid=1;
e1.ename="Abdul";
e1.sal=50000.00;
e1.desig="Sr Programmer";
*/
Emp e1=new Emp(1, "Abdul", 50000.00, "Sr Programmer");
Emp e2=new Emp(2, "Kashyap", 50000.00, "Designer");
Emp e3=new Emp(3, "Kanchan", 50000.00, "Programmer");
System.out.println(e1.eid);
System.out.println(e2.ename);
System.out.println(e3.desig);
System.out.println(Emp.counter);
System.out.println(Emp.hiddenName.length());
System.out.println(Emp.hiddenName.startsWith("M"));
System.out.println(Emp.hiddenName.endsWith("p"));
System.out.println(Emp.hiddenName.substring(2,4));
}
}
transient: comes in I/O stream topic
volatile: comes in threads topic
0 Comments