Class 12 Informatics Practices Revision Notes Ch 5 GUI Programming and Access Specifier


Revision Notes for CBSE Class 12 Informatics Practices Chapter 5 -Free PDF Download

Free PDF download of Best CBSE Class 12 Informatics Practices Chapter 5 GUI Programming and Access Specifier Quick Revision Notes & Short Key-notes prepared by our expert Informatics Practices teachers from latest edition of CBSE(NCERT) books.

 

Class 12 Informatics Practices Quick Revision notes Chapter 5 GUI Programming and Access Specifier

Object Oriented Programming
Object oriented programming is a programming paradigm in which the programs are organised in terms of objects rather than actions and more emphasis is given on data rather than logic. Because of this organisation object oriented programming provides various features as:

  1. Data hiding A class defines on the related data and when an instance of that class is run the code will not be able to accidentally access other program data.
  2. Reusability It provides reusability of code.
  3. User defined data types Concept of classes allows programmers to define their own data types.

In this, the programming task is divided into objects. The methods are subroutines or procedures that are capable of performing the defined task. Object oriented programming follows the bottom up approach in program design and emphasises on safety and security of data.
Principles of OOP
Different people interpret the principle of object oriented programming differently.
These are as follows:
Object
An object is a real world entity. It could be a person, place, table, chair or any item that the program may handle. Each object holds data and code to operate on the data. Any programming problem is analysed in terms of objects and the nature of communication. An object takes up space in the memory and has an associated address with it.
Class
It is a logical structure upon, which the objects are build or a class is a group of objects that share common properties and relationships. In other words, class is a collection of objects of similar type. Each object of a class possesses same attributes and common behaviour defined within the same class, e.g. Car, Truck, Bike and Cycle are the members of class vehicle.
Data Encapsulation
Process of wrapping up of data and methods in a single unit is known as encapsulation. In OOP, data and associated methods are bound together to make a single unit, class. Data is not accessible to outside the world and only class methods can get access to it. This insulation of data from direct access by the program is called as data hiding.
Data Abstraction
Abstraction is a concept, which is used to represent essential features without including the background details or explanations. It is an essential concept of OOP. The concept of data abstraction can be understood with the help of a simple example of an electric fan. A user is totally unaware of its internal working, he/she only needs to know how it gets ON/OFF. Thus, the user gets isolated from all the complex circuitry, which is well encapsulated.
Inheritance
The capability of one class to inherit (derive) the properties of any other class completely or partially, is known as inheritance. It is the process by which objects of one class can get the properties of objects from another class. Basically, it provides the idea of reusability. That means, we can include additional features to an existing class without modifying it. This is possible by deriving a new class from the existing class. The class, from which properties are inherited, is known as superclass (or base class or parent class) and the class which inherits the properties from super class is known as subclass (or derived or child class).
Polymorphism
The term polymorphism literally means the capacity to take ‘different forms from one’. It is a feature that allows creating several methods with the same name, which differ from each other in the type. In context of object oriented programming, polymorphism refers to a programming language’s ability to process objects differently, depending on their data type or class.
It is the ability to appear in many forms. Polymorphism is broadly used in implementing inheritance. It means that the same operations may behave differently on different classes. It allows us to write generic, reusable code more easily. Suppose that our program uses many kinds of shapes, such as circle, box, etc. So, here, we design a superclass called Shape. In example, all shapes have a method called Draw( ). In Java, polymorphism is provided by function (or method) overloading and method overriding.
Data Members and Methods
Data member is a variable that belongs to an object, e.g. a class car can have attribute say color. So, here whenever we create an object of class car it would have its own color, that’s why color is a data member of every class object. The methods are used to specify some actions within a class. For performing an action through a method, a message can be sent to the respective method. A message to an object is call to the object’s method requesting that it performs some specified actions.
The methods are useful because they helps us to provide solution of complex problems, to hide the background details, i.e. how the task is being performed and to reuse a method (i.e. to repeat the same task whenever needed) again.
For using a method in your program it must first be defined and then it should be called.
1. Method Definition
The method must be defined before it is used in a program.
The general form of method definition is as follows:

[access specifier][non-access specifier] return_ type method_ name (arguments) { : . body of method: : . }

access specifier The access specifier determines the access of the member of a class. It is the feature of a class, which is put down before the declaration of a variable or methods. Access specifier are also known as access modifier.
non-access specifier Non-access specifier do not change the accessibility of variables and methods, but they do provide them special properties such as final, static, etc.
return_type The return type defines the type of value which will be returned after the execution of the method. If no value is to be returned, then it would be defined as void.
method_name The method name is an identifier, i.e. the name of the method.
Some conventions for method naming are as follows:

  1. The method name should begin with a lowercase letter.
  2. It should be meaningful and convey the behaviour of the method, so that it is easily identifiable.

arguments The arguments or parameters in method definition are the formal parameters, separated by commas. A method can be defined even without any arguments. In such case, the list of arguments will be empty.

e.g. public class Employee { int addvalue(int a, int b) //A method with two arguments, formal parameters. { return(a+b); } void dispO //A method without any argument { System.out.printlnC"Kritika and Rohini”); } }

In a Java program, there can be many classes containing several methods. Every Java program must contain one main() method in a class. The main() method is very important in any Java program as it determines the starting point of any program. NetBeans IDE automatically generates a main() method, from where the execution of the program starts and the compiler invokes the top level form of the GUI application.
2. Method Calling
After defining a method, you need to call it in order to execute the statements written inside that method. For calling a method first you need to create an object of the class and then call the method using the following syntax.

     object_name.method_name (arguments); e.g. Employee obj = new Employee();      //object named obj of Employee class created      obj.dispO; //Method calling.

Arguments provided to a method can be of the following types:

  1. The primitive data types, there can be char, byte, short, int, long, float, double, boolean,
  2. The reference data types, there could be objects or arrays.

While calling a method actual values of parameters are passed, hence, the parameters used in calling a method are called actual parameters.
A method can be invoked in either of the two ways; these are call by value and call by reference.

  1. Call by Value In call by value method, the value of actual parameters are copied into the formal parameters. There will be a separate copy of arguments is created in the memory and then it would be used. If any changes are made to formal arguments then it will not reflect to in the value of actual arguments.
  2. Call by Reference Under the call by reference, new copy is not created, rather the original values are used. So, if there is any modification in the variables inside the method, it will be reflected to the original variables.

In Java, there are scope of variable or method, which can be understood with the following points:

  1. The data declared at the class level can be used and accessed by all the methods of that class.
  2. The data declared in a particular method are known as local data and they can be used within that method only.
  3. The variables declared inside a block are accessed only in that block. These variables are not accessible outside the block.
  4. The variables declared outside the block and before the block are accessible inside the block.

Access Specifier
The access specifier determines the access of the members of a class. In Java, there are four types of access specifiers namely: private, protected, public and default.
1. private Access Specifier
The private access specifier denotes a variable or method as being private to the class and cannot be accessed outside the class. Accessing can be done through calling one of the public class methods. This is the most restrictive access level and private methods can be accessed only through the other member methods of the same class. To declare a variable or method as a private, we have to use the keyword private.

e.g. class ABC { private int x; private void add() { : . } }

Here, in this example data member named ‘x’ and member method add( ) cannot be accessed even by the object of class ABC.
2. protected Access Specifier
The protected access specifier denotes a variable or method as being public to subclasses of this class but private to all other classes outside the current package. So, derived classes have the ability to access protected variable or method of its parent class. The protected access specifier allows the method to be accessed by the class itself, subclasses and all subclasses in the same package as well as in other package.
For declaring a method as protected, we have to use the keyword protected.

e.g. package mypack; class XYZ { protected int a; protected void add() { : . } }

Here, in the above example data member ‘a’ and member method add( ) can only be accessed by the subclasses of class XYZ, in the same package only.
3. public Access Specifier
The public access specifier is used to access a member from other classes. It means when we declare a member as public, it can be accessed from all other classes. Any member declared as public can be accessed from anywhere, any package just by using the object of class. The public member of class can be accessed through the dot operator.

e.g. package mypack; class XYZ { public int a; public void add() { : . } }

Here, in the above example data member a and member method add() both are declared public. Hence, it can be accessed from anywhere using the object of class XYZ.
4. default (Friendly) Access Specifier
The default access specifier is friendly which means, if we do not explicitly define any access specifier before any methods or variables. Java assumes the same as friendly to all classes in the same package, i.e. public for the classes within the same package.

e.g. package mypack; class XYZ { int a; void add() { : . } }

Accessibility of Different Types of Access Specifier
The accessibility of different types of access specifiers can be stated through the following table:
Class 12 Informatics Practices Notes Chapter 5 GUI Programming and Access Specifier 1
Java Libraries
A library refers to a set of ready made software routines (class definitions) that can be reused in new programs. In Java language, there are many libraries for performing different types of tasks, as per requirement. Commonly used libraries are io library, util library, etc.
For using and activating a library in a program we have to use import statement.

import java.io.*;

Working with Strings
In Java language, we work with character data, i.e. single character or set of characters. A set of characters is called a string.
There are three classes available in Java language to work with strings:

  1. Character Class The Character class is that class, whose instances or objects can hold single character data. Such types of classes are offering methods to manipulate or inspect single-character data.
  2. String Class The String class is that class, whose instances or objects can hold immutable or unchangeable strings. It means once the objects or instances are initialised, they cannot be changed or modified.
  3. StringBuffer Class The StringBuffer class is that type of class, whose instances or objects can hold mutable or changeable strings. It means these objects or instances could be modified after their initialisation.

Creating Strings
Using String class, we can create strings whose size once fixed cannot be modified later.
To create a string in a Java program, we have to declare the object of String class and initialise the same with a string literal.

e.g. String country=“India is my country”;

Strings can also be created by using other method i.e. using ‘new’ keyword

String capital=new String(“Delhi is the capital of India”);

Accessor Methods of String class
The accessor methods are used to get the information about the objects. There are many methods in the class String to perform different operations on string.
Some of the most commonly used accessor methods are defined here under:

  1. char charAt(int index) This method returns the character at the specified index, e.g. char charAt(lO) will return 10th character.
  2. int compareTo(Stringl, String2) This method is used to compare two strings alphabetically, i.e. the way in which they are written in dictionaries.
  3. String concat(String str) This method is used to concatenate the specified string at the end of current string object.
    We can also use the concatenation operator (+) to achieve the same result. 

    e.g. String str = Stringl + String2; or      String str = Stringl .concat(String2);
  4. Boolean endsWith(String str) This method is used to test whether the current string object is ending with a specified character or not.
  5. Boolean equals(String str) This method compares the specified string with current string and determines whether both are equal or not. Here, comparing is case sensitive, i.e. uppercase and lowercase characters are treated differently.
  6. Boolean equalsIgnoreCase(String str) This method compares the specified string with current string, ignoring the case considerations and determines whether both are equal or not.
  7. int indexOf(char ch) This method is used to return the index of first occurrence of the specified character within the current string.
  8. int IastIndexOf(char ch) This method is used to return the index of last occurrence of the specified character within the current string.
  9. int length( ) This method returns the length of current or this string.
  10. String replace(char oldChar, char newChar) This method returns a new string after replacing all occurrences of oldChar character with newChar character from the current string.
  11. Boolean startsWith(String str) This method checks whether the current string starts with a particular (i.e. the specified) character or not.
  12. String substring(int startlndex) This method returns a substring from the current string, which is starting from the start Index character to the end of invoking StringBuffer object.
  13. String substring(int beinglndex, int endlndex) This method returns a substring from the current string, which is starting from the beinglndex character and ends at endlndex character.
  14. String toLowerCase( ) This method converts all the characters of a given string to lowercase (i.e. small letters).
  15. String toString( ) This method returns the string itself, i.e. in original form, without changing the case pattern.
  16. String toUpperCase( ) This method converts all the characters of a given string to uppercase (i.e. capital letters).
  17. String trim( ) This method removes the white spaces from the beginning and ending of the current or this String.

String Buffer Class
The StringBuffer class is a part of java.lang package. The StringBuffer class is an alternative to the String class. The length of buffer is referred as the capacity of buffer and it is not same as the length of the string and buffer’s capacity is always greater than string length. Whenever we need to change the length of the string in StringBuffer object, we can use the setLength( ) method. When the setLength is longer than the string, that the StringBuffer holds, then extra null characters will be appended at the end of the string. Otherwise, it is shorter than the underlying string and remaining characters from the string will be truncated.
Creating a StringBuffer
To create a StringBuffer object, we have to use new operator.
There are following ways to create a StringBuffer:
1. To create an empty buffer, we can use the following method:

StringBuffer mybuffer = new StringBuffer();

(It will create an empty StringBuffer object with “mybuffer” name.)
2. To create a buffer with a literal assigned to it

StringBuffer mybufferl = new StringBuffer(“India”);

(It will create a StringBuffer object with mybufferl name and initialise string value India to it.)
3. To create a buffer assign it with a capacity of holding a specified number of characters

int x = 10; StringBuffer mybuffer2 = new StringBuffer(x);

(It will create a StringBuffer object with mybuffer2 name, which has a capacity of holding 10 characters).
StringBuffer methods
The commonly used StringBuffer methods are briefly described as follows:

  1. append(x) This method is used to append specified number of characters, i.e. x to the end of StringBuffer object.
  2. insert(offset, x) This method is used to add specified number of characters, i.e. at a specific location.
  3. setCharAt(index, c) This method is used to alter one character. It replaces character at index (i.e. the specified location) with c.
  4. delete(beg, end) This method is used to delete the characters from the index beg to the index end.
  5. reverse( ) This method is used to reverse the content of the StringBuffer.
  6. setLength(n) This method is used to set the length of string to n characters. If the string is bigger than the specified number, the remaining characters will be truncated and if the string is smaller than null characters (‘u0000’) will be appended to it.
  7. int capacity( ) This method will return the maximum number of characters that can be entered in the current string object (this).

Math Class
Whenever we need mathematical computation in any program, we have to use the mathematical methods. These methods are defined in the Math class.
The general syntax of using a method defined under math class is given under:

Math.method_name(arguments)

Commonly used mathematical methods are as follows:
1. pow( ) This method is used to find the value of a number raised to another number,
i. e. in the form a b. The general syntax is as follows:

     pow(double a, double b) e.g. Math.pow(3, 2) will return 9

2. round() This method is used to find the value of a number as rounded, i.e. a number to its nearest integer. The general syntax of this method are as follows:

public static long round(double a) public static int round!float a)

When we pass a double type value to this method, then as a result a long type value will be returned and when we pass a float type value to this method, then an int type value will be returned.

e.g. Math. round(4.5) will return 5      Math.round(-4.5) will return -4