Trending ▼   ResFinder  

ICSE Class X Notes 2024 : Computer Applications

2 pages, 1 questions, 0 questions with responses, 0 total responses,    0    0
Computer Applications
  
+Fave Message
 Home > anivinutha >

Formatting page ...

Encapsulation Encapsulation in Java is a mechanism of wrapping the data (variables) and code acting on the data (methods) together as a single unit. in encapsulation, the data in a class is hidden from other classes, so it is also known as datahiding. Benefits of Encapsulation The fields of a class can be made read-only or write-only. Other classes will not be able to access data directly because variables are private A class can have total control over what is stored in its fields and manipulations within class It improves flexibility and reusability. Scope The scope of a variable defines the section of the code in which the variable is visible. As a general rule, variables that are defined within a block are not accessible outside that block. The different types of variables are Instance variables Instance variables are those that are defined within a class itself and not in any method or constructor of the class. They are known as instance variables because every instance of the class (object) contains a copy of these variables. The scope of instance variables is determined by the access specifier that is applied to these variables. Class/Static Variables Class variables also known as static variables are declared with the static keyword in a class, but outside a method, constructor or a block. There would only be one copy of each class variable per class, regardless of how many objects are created from it. Difference between Instance and Class variable Instance variable Class variable Each object will have its own copy of Only one copy of a static variable per class instance variable irrespective of how many objects we create. Changes made in an instance variable using one object will not be reflected in other objects as each object has its own copy of instance variable. We can access instance variables through object references Any changes made in static variable will be reflected in other objects as static variables are common to all object of a class. Static Variables can be accessed directly using without an object. Argument variables These are the variables that are defined in the header of constructor or a method. The scope of these variables is the method or constructor in which they are defined. The lifetime is limited to the time for which the method keeps executing. Once the method finishes execution, these variables are destroyed. Local variables A local variable is the one that is declared within a method or a constructor or in a block(not in the header). The scope and lifetime are limited to the method itself. Access Specifiers/ Modifiers The access modifier in java specifies accessibility (scope) of a data member, method, constructor or class. These Specifiers determine whether a field or method in a class, can be used or invoked by another method in another class or sub-class. Access Specifiers can be used to restrict access. Types Of Access Specifiers : In Java we have four Access Specifiers and they are: 1.public 2.private 3.protected 4. default(no specifier) public The public access modifier is accessible everywhere. It has the widest scope among all other modifiers. Classes, methods, and fields declared as public can be accessed from any class in the Java program, whether these classes are in the same package or in another package. private Private Specifiers achieves the lowest level of accessibility. Private methods and fields can only be accessed within the same class to which the methods and fields belong. Private methods and fields are not visible within subclasses and are not inherited by subclasses. default If a variable/method is defined without any access modifier keyword, then that will have a default modifier. A variable or method declared without any access control modifier is available to any other class in the same package. protected The protected access modifier is accessible within package and outside the package but through inheritance only. Methods and fields declared as protected can only be accessed by the subclasses in other package or any class within the package. The protected access modifier can be applied on the data member, method and constructor. It can't be applied on the class. Access Modifiers Visibility public Visible to All classes. protected Visible to classes with in the package and the subclasses of other package. No Access Modifier (Default) Visible to the classes with the package private Visible with in the class. It is not accessible outside the class.

Formatting page ...

 

  Print intermediate debugging step

Show debugging info


 

 

© 2010 - 2025 ResPaper. Terms of ServiceContact Us Advertise with us

 

anivinutha chat