Professional Documents
Culture Documents
Access Modifier
LEVEL – LEARNER
Icon Used
Check Your
Objective Lend A Hand Summary Knowledge
2
Overview
3
Objective
4
Check Your Understanding
Hi guy’s. You would have already learnt the concept and implementation of
Access Modifier ,Method Overloading, Static Keyword, Static Method and
Constructor Overloading in the primary language. Now lets have a recap of
what you have learnt :
5
Just a Minute
6
Just a Minute
3. What is the most restrictive access modifier that will allow members of one
class to have access to members of another class in the same package or
assembly?
a. public b. abstract
c. protected d. synchronized e. default access
4. In which of the following should the methods of a class differ if they are to
be treated as overloaded methods?
1. Type of arguments
2. Return type of methods
3. Number of arguments
4. Names of methods
5. Order of arguments
7
Access Modifier in Java and C#
8
Public Access Modifier in Java and C#
9
Private Access Modifier in Java and C#
Only code inside the same class can access the Private variable,
or call the Private method.
10
Protected Access Modifier in Java and C#
11
Default Access Modifier in Java
12
Internal Access Modifier in C#
Example:1
class BaseClass // internal by default
{
// Only accessible within the same assembly
internal static int x = 0;
}
13
Protected Internal Access Modifier in C#
14
Example : Protected Internal Access
Modifier in C#
class AccessModifier
{
// String Variable declared as protected internal
protected internal string name;
public void print()
{
Console.WriteLine("\nMy name is " + name);
}
}
class Program
{
static void Main(string[] args)
{
AccessModifier ac = new AccessModifier();
Console.Write("Enter your name:\t");
// Accepting value in protected internal variable
ac.name = Console.ReadLine();
ac.print();
Console.ReadLine();
}
}
}
15
Method Overloading in Java and C#
C# and Java allows us to define multiple Methods with the same name within
same class differing in the number , type , kind and order of arguments termed
as Method Overloading.
16
Method Overloading in Java and C#
17
Static Keyword in Java and C#
Static Keyword in Java Static Keyword in C#
• static keyword can be applied with nested • A static class is never instantiated. In the
class. static keyword can not be applied static class, we access members directly on
on top level class. the type. This eliminates misuse of the class.
• static keyword can be applied with • static keyword can be applied with variable,
variable, method. method, properties, constructor.
• A nested static class can have non-static • A static class cannot have non-static
members. All methods, fields and members. All methods, fields and properties
properties need not to be static. in it must also be static.
• When a variable is declared static ,used to • When a field is declared as static then that
refer the common property of all objects, field will exists only one copy for any number
static variable gets memory only once in of instances of the class. Hence static fields
class are at the time of class loading are used to store the data that is to be shared
by all instances of the class
18
Example : Static Keyword in Java and C#
Static Keyword in Java Static Class and Members in C#
public class NestedStaticExample static class Shape
{ {
public static void main(String args[]) public static string message;
{ public static double GetArea(double Width,
StaticNested nested = new StaticNested(); double height)
nested.name(); {
} return Width * Height;
}
//static nested class in java }
private static class StaticNested class Ractangle
{ {
public void name() private void GetRactangleArea() {
{ double Area;
System.out.println("static nested class Area = Shape.GetArea(10, 5);
example in java"); }
} }
} class Program
} {
static void Main() {
Shape.message=“This is a Rectangle”;
Console.WriteLine(Shape.message);
}}
19
Static Method in Java and C#
• Static method can be public ,private and • Static method can be public ,private and
protected protected
20
Constructor in Java and C#
A constructor looks more like a method but without return type. The advantage
of constructors over methods is that they are called implicitly whenever an
object is created.
Programmer uses constructor to initialize variables, instantiating objects
Both Java and C# have constructors as class member, and are implemented in
similar fashion.
21
Constructor overloading in Java and C#
22
Example : Constructor overloading in Java
and C#
Constructor overloading in Java Constructor overloading in C#
class Student{ class Student{
int id; int id;
String name; String name;
int age; int age;
Student(int i,String n){ Student(int i,String n){
id = i; id = i;
name = n; name = n;
} }
Student(int i,String n,int a){ Student(int i,String n,int a){
id = i; id = i;
name = n; name = n;
age=a; age=a;
} }
void display(){System.out.println(id+" void display()
"+name+" "+age);} {Console.WriteLine(id+" "+name+" "+age);
}
public static void main(String args[])
{ public static void Main(String args[]){
Student s1 = new Student(111,“SAM"); Student s1 = new Student(111,“SAM");
Student s2 = new Student s2 = new
Student(222,"Anni",25); Student(222,"Anni",25);
s1.display(); s1.display();
s2.display(); s2.display();
} }
} }
23
Lend a Hand
Create a class and members with different access modifier and try to access
in some other class in JAVA
Create a class with static and non-static members both fields and methods
C#
Create a class with static and non-static members both fields and methods in
JAVA
24
Lend a Hand Solution
//Create a class and members with different access modifier and try to access in some other class in JAVA
class Vehicle
{
// Instance fields
int noOfTyres; // no of tyres
private boolean accessories; // check if accessorees present or not
protected String brand; // Brand of the car
// Static fields
private static int counter;
// No of Vehicle objects created
// Constructor
Vehicle()
{
System.out.println("Constructor of the Super class called");
noOfTyres = 5;
accessories = true;
brand = "X";
counter++;
}
// Instance methods
public void switchOn() {
accessories = true;
}
25
Lend a Hand Solution
26
Lend a Hand Solution
Eaxmple:1
class Person
{
private string name;
private int age;
// Copy constructor.
public Person(Person previousPerson)
{
name = previousPerson.name;
age = previousPerson.age;
}
// Instance constructor.
public Person(string name, int age)
{
this.name = name;
this.age = age;
}
28
Lend a Hand
// Get accessor
public string Details
{
get { return name + " is " + age.ToString(); }
}
}
class TestPerson
{
static void Main()
{
// Create a new person object.
Person person1 = new Person("George", 40);
//Output
George is 40
29
Lend a Hand solution
//Create a class with static and non-static members both fields and methods C#
using System;
namespace staticDemo
{
class TestClass
{
public static int i;
public static void StaticDisplay()
{
i=10;
Console.WriteLine(“Value from StaticDisplay Method:”+i);
}
public void NonStaticDisplay()
{
int j=20;
Console.WriteLine(“Value from NonStaticDisplay Method:”+j);
}
static void Main(string[] args) //Output
{ Value from StaticDisplay
TestClass obj = new TestClass(); Method: 10
TestClass. StaticDisplay(); Value from NonStaticDisplay
obj. NonStaticDisplay(); Method:20
Console.Read();
}
}
30}
Lend a Hand solution
//Create a class with static and non-static members both fields and methods in JAVA
class TestClass
{
public static int i;
public static void StaticDisplay()
{
i=10;
System.out.println(“Value from StaticDisplay Method:”+i);
}
public void NonStaticDisplay()
{
int j=20;
System.out.println(“Value from NonStaticDisplay Method:”+j);
}
static void main(string[] args)
{
TestClass obj = new TestClass();
TestClass. StaticDisplay(); //Output
obj. NonStaticDisplay(); Value from StaticDisplay
Console.Read(); Method: 10
} Value from NonStaticDisplay
} Method:20
31
Lend a Hand solution
32
Best Practices : Object Casting in C#
33
Questions
34
Summary
35
Source
• http://msdn.microsoft.com/en-us/library/ms173121.aspx
• http://www.javatpoint.com/access-modifiers
• http://msdn.microsoft.com/en-us/library/79b3xss3(v=vs.80).aspx
• http://msdn.microsoft.com/en-us/library/vstudio/ace5hbzh.aspx
• http://www.javatpoint.com/constructor
Disclaimer: Parts of the content of this course is based on the materials available from the Web sites and books
listed above. The materials that can be accessed from linked sites are not maintained by Cognizant Academy and
we are not responsible for the contents thereof. All trademarks, service marks, and trade names in this course are
the marks of the respective owner(s).
36
Comparative Study(C# with JAVA)
38