Topics

6/recent/ticker-posts

Some Consequences of applying access specifier & modifiers on class, variables, constructors & methods: (part 2 )

Access modifiers Part 2

In the previous post we have seen both class and variables . Here we see about constructors and methods: 

constructors: 

    looks like a method with the same name as class name and with no return type.

we can write i) no parameter and ii) parametrized constructor

useful in:

  • creating object of the class
  • initializes class instance variables 
  • (constructor receives parameters, we will pass arguments while calling constructor)

private: if class constructor is private, then class cannot be inherited or instantiated

class A extends Object {

private A() {

super();

}

}

class B extends A {

B() {

super(); // error

}

class C {

A a=new A(); // error

}

default: if developer writes a public class with default access specifier constructor,

then class can be inherited and instantiated within the package, but cannot be inherited or 

instantiated outside the package.

class A {

A() {} // default constructor

}


public class A {

public A() {} // default constructor

}

  • Q) What is default constructor?

  1. If we dont write any constructor in a class, during compilation time, compiler 
  2. Automatically generates one no parameter constructor with access specifier as same as class access specifier.

Note: default constructor is not generated if developer generates a constructor

public class A {

A() {} 

// explicitly written no parameter constructor. default access specifier constructor in public class

}


protected: 

class A {

protected A() {} // explicit constructor

}

  • class can be both inherited or instantiated within the directory/package classes. 

  • Because class is having default access specifier it cannot be accessed outside the package

public class A {

protected A() {} // explicit constructor

}

  • The class can be both inherited or instantiated within the package classes and can be only inherited outside the package class, cannot be instantiated outside the package class

public: 

  • public class with public constructor can be both inherited or instantiated within and outside the package.
  • class can be accessible anywhere within the system

public class A {

public A() {}

}

methods:

private: can be accessible only within the class

default: can be accessible only within the package (subclass or instantiated class)

protected: can be accessible within the package and upto sub class outside the package

public: can be accessible anywhere

abstract: 

  • method which contains only declaration/without implementation is called as abstract. 
  • class which contains abstract method must also be declared as abstract.
  • abstract methods are implemented in abstract class sub class

abstract int sub(int a, int b); // only declaration

int sub(int a, int b) {} // implementation

final: 

  • final method cannot be overridden in sub class.

final double calAvg (int marks[]) {

int len = marks.length;

int sum=0;

double avg=0.0;

for(int i=0;i<marks;i++) {

sum+=marks[i];

}

avg=sum/len;

return avg;

}

static:

  • Methods which dont want instance/dynamic memory, method which want class/static memory is declared as static
  • As same as variables, static methods can be called classname

normal method:

class A {

void m1() {}

}

class B extends A {

void m1() {}

}

class C extends A {

}

A a=new A(); // memory is allocated to m1() method in A class memory

B b=new B(); // memory is allocated to m1() method in B class memory

C c=new C(); // memory is allocated to m1() method of A class memory

final method:

class A {

final void m1() {}

}

class B extends A {

}

class C extends A {

}

B b=new B(); In B class object memory's A class object memory m1() method memory is allocated

C c=new C(); In C class object memory's A class object memory m1() method memory is allocated


static and final method:

class A {

static final void m1() {}

}

class B extends A {

}

class C extends A {

}

B b=new B(); // In current class object's super class object memory, memory is

not allocated to m1(), a separate A class memory contains m1() method memory

C c=new C(); // same as above

Post a Comment

0 Comments