In this article,Access Modifiers in Java is explained in detail with examples.
This concept represents scope/visibility/accessibility of different members of a class for usage.
Java supports four access modifiers:
a.Public
b.Protected
c.Default
d.Private
* If no modifier is used then it is default we use default in the switch case.
Using: D=direct access.
obj=object access.
Note: In java,abstraction,which is a feature of OOPS is implemented through access modifiers i.e, Private,default protected. (From topic Access Modifiers in Java)
Default Method
Protected Method
Public Method
Default Method
Private Method
Protected Method
Public Method
Default value a: 10
Private value b: 20
protected value c: 30
Public value d: 40
Default value a: 10
protected value c: 30
Public value d: 40
Ex
//program related to Access Modifiers in Java-- inheritance with access classifier.
class ParentClass
{
int a;
private int b;
protected int c;
public int d;
ParentClass()
{
a=10;
b=20;
c=30;
d=40;
}
void defaultMethod()
{
System.out.println("Default Method");
}
private void privateMethod()
{
System.out.println("Private Method");
}
protected void protectedMethod()
{
System.out.println("Protected Method");
}
public void publicMethod()
{
System.out.println("Public Method");
}
}
class ChildClass extends ParentClass
{
void ChildMethod()
{
defaultMethod();
//privateMethod();
protectedMethod();
publicMethod();
System.out.println("Default value a: "+a);
//System.out.println("Private value b: "+b);
System.out.println("protected value c: "+c);
System.out.println("Public value d: "+d);
}
}
class AccessSpecifier1
{
public static void main(String args[])
{
ChildClass ch=new ChildClass();
ch.ChildMethod();
System.out.println();
System.out.println("Default value a: "+ch.a);
//System.out.println("Private value b: "+ch.b);
System.out.println("protected value c: "+ch.c);
System.out.println("Public value d: "+ch.d);
}
}
Do check my new startup Surprise Planners in Hyderabad- Lavish Surprises our services are surprise party planners in Hyderabad, surprise gifts, surprise birthday party planners Hyderabad, Wedding anniversary surprises, surprise planners Hyderabad.
Hi Friends, Please comment down your views in the comment section below. Thank you..
This concept represents scope/visibility/accessibility of different members of a class for usage.
Java supports four access modifiers:
a.Public
b.Protected
c.Default
d.Private
* If no modifier is used then it is default we use default in the switch case.
Using: D=direct access.
obj=object access.
Ex
//Program related to Access Modifiers in Java--Level of access
class ExampleClass4
{
int a;
private int b;
protected int c;
public int d;
ExampleClass4()
{
a=10; //Direct Access
b=20;
c=30;
d=40;
}
void defaultMethod()
{
System.out.println("Default Method");
}
private void privateMethod()
{
System.out.println("Private Method");
}
protected void protectedMethod()
{
System.out.println("Protected Method");
}
public void publicMethod()
{
System.out.println("Public Method");
}
void generalMethod()
{
defaultMethod();
privateMethod();
protectedMethod();
publicMethod();
System.out.println("Default value a: "+a);
System.out.println("Private value b: "+b);
System.out.println("protected value c: "+c);
System.out.println("Public value d: "+d);
}
}
class AccessSpecifier
{
public static void main(String args[])
{
ExampleClass4 obj=new ExampleClass4();
obj.defaultMethod();
//obj.privateMethod();
obj.protectedMethod();
obj.publicMethod();
System.out.println();
obj.generalMethod();
System.out.println();
System.out.println("Default value a: "+a);
//System.out.println("Private value b: "+b);
System.out.println("protected value c: "+c);
System.out.println("Public value d: "+d);
}
}
OutputDefault Method
Protected Method
Public Method
Default Method
Private Method
Protected Method
Public Method
Default value a: 10
Private value b: 20
protected value c: 30
Public value d: 40
Default value a: 10
protected value c: 30
Public value d: 40
Ex
//program related to Access Modifiers in Java-- inheritance with access classifier.
class ParentClass
{
int a;
private int b;
protected int c;
public int d;
ParentClass()
{
a=10;
b=20;
c=30;
d=40;
}
void defaultMethod()
{
System.out.println("Default Method");
}
private void privateMethod()
{
System.out.println("Private Method");
}
protected void protectedMethod()
{
System.out.println("Protected Method");
}
public void publicMethod()
{
System.out.println("Public Method");
}
}
class ChildClass extends ParentClass
{
void ChildMethod()
{
defaultMethod();
//privateMethod();
protectedMethod();
publicMethod();
System.out.println("Default value a: "+a);
//System.out.println("Private value b: "+b);
System.out.println("protected value c: "+c);
System.out.println("Public value d: "+d);
}
}
class AccessSpecifier1
{
public static void main(String args[])
{
ChildClass ch=new ChildClass();
ch.ChildMethod();
System.out.println();
System.out.println("Default value a: "+ch.a);
//System.out.println("Private value b: "+ch.b);
System.out.println("protected value c: "+ch.c);
System.out.println("Public value d: "+ch.d);
}
}
Output
Default Method
Protected Method
Public Method
Default value a: 10
protected value c: 30
Public value d: 40
Default value a: 10
protected value c: 30
Public value d: 40
Continue to next topic Static Modifier in Java.
Begin your career in Digital Marketing,What is digital marketing? Digital Marketing online course. It's an current evolving technology which can give support to establish your own startup through Digital Marketing.
Hi Friends, Please comment down your views in the comment section below. Thank you..
No comments:
Post a Comment