Skip to main content

ads1

class 12th Chapter-8 Classes and object in Java.

  

A class contains both data (referred to as attributes), and program code

Creating objects

Creating an object from a class requires the following steps :

Declaration : A variable (reference variable) name of type class is declared with syntax <class name> <variable name>

• Instantiation : Keyword new is used to create the object by allocating memory

• Initialization: Constructor (a special type of method) is called to initialize the newly created

Using new keyword, we can create an object. Operator new allocates the memory for an object and returns the address

When an object is created, in addition to allocating memory, a special method called 'constructor is executed to perform initial task.

rl = new Room(); Here, parentheses are important;

In object-oriented programming (OOP) languages, creating an object is also called object instantiation. An instance for an object is created by allocating memory to store the data for that object.

Instance variables define the attributes of an object. The class defines the kind of attribute. Each instance stores its own value for that attribute.

The new keyword returns a reference to an object that represents an instance of the class. All instances of class are allocated memory in data structure called heap.

Each object instance has its own set of data. Accessing instance variables and calling instance methods Instance variables and instance methods are accessed via objects. there is no need to use dot (.) operator.

Class variables and class methods :-Just like class variable, class method can be defined using static keyword in front of the method definition.

• Class variables and class methods can be accessed using a class name or reference variable.
To increase readability, it is advised to access with class name.

Class variables and class methods can be accessed from instance methods also.
Instance variables and instance methods can't be accessed from class methods, as class

Local variables : Variables defined inside methods or blocks are called local variables. Formal parameters of the methods are also local variables

Instance variables : Instance variables are variables defined within a class but outside any method.

Class variables : Class variables are variables defined with in a class, outside any method, with the static keyword. These variables are allocated memory only once per class and is shared

Constructors Constructor is a special kind of method that is invoked when a new object is created. Constructor can perform any action, but it is mainly designed to perform initializing actions.

Types of specifier
1. Public
2. Private
3. Protected
4. Default

The values of actual parameters are copied to formal parameters and then function is executed. Changes made to formal parameters are not affecting actual parameters.

Inheritance
Object-oriented programming languages provide reusability feature using inheritance. Inheritance allows us to build new class with added capabilities by extending existing class.

Parent class is also referred to as superclass or base class. In the same way; child class is also referred to as subclass, derived class or extended class.

When an object of subclass is instantiated, memory is allocated for all its attributes including inherited ones

Choose the correct Option

(1) Which of the following defines attributes and methods ?

(a) Class (b) Object (c) Instance (d) Variable

(2) Which of the following keyword is used to declare Class variables and class methods?

(a) static (b) private (c) public (d) package

(3) Which of the following operator creates an object and returns its reference ?

(a) dot() (b) new (c) colon (:) (d) assignment (=)

(4) Which of the following method can be called without creating an instance of a class ?
(a) Instance method (b) Class method (c) Constructor method (d) All of the above

(5) Which of the following refers more than one method having same name but different
parameters ?

(a) Overloaded methods (b) Overridden methods (c) Duplicate methods (d) All of the above

(6) Which method is invoked automatically with creation of an object ?

(a) Instance method (b) Constructor (c) Class method (d) All of the above

(7) Which of the following is the keyword used to refer a superclass constructor in subclass constructor ?

(a) extends (b) super (c) name of the superclass (d) new

(8) Which of the following is used to invoke an instance method in Java ?

(a) The name of the object, colon(t) and the name of the method
(b) The name of the object, dot(.) and the name of the method
(c) The name of the class, colon(:) and the name of the method
(d) The name of the class, dot(.) and the name of the method

(9) Which of the following is accessible by instance methods ?

(a) Only instance variables (b) Only class variables (c) Both instance variables and class variables (d) All of the above

(10) When methods in the superclass and subclass have same name and signature, what are they called ?

(a) Overloaded methods (b) Overridden methods (c) Inherited methods (d) All of the above 

Link to download :- 











ads2

Popular posts from this blog

11. Write a Java program to input basic salary of an employee and calculate its Gross salary according to following:

    11. Write a Java program to input basic salary of an employee and calculate its Gross salary according to following: Basic Salary <= 10000 : HRA = 20%, DA = 80% Basic Salary <= 20000 : HRA = 25%, DA = 90% Basic Salary > 20000 : HRA = 30%, DA = 95% Static Solution :-  class salary {     public static void main(String args[])     {     double basic=20000.50,gross,da,hra;     if(basic <=10000)     {         da = basic * 0.8;         hra = basic *0.2;     }              else if(basic <=20000)     {         da = basic * 0.9;         hra = basic *0.25;     }     else     {         da = basic * 0.95;         hra = basic * 0.3;     }     gross = basic + da + hra;     System.out.println("The Gross Salary is :-"+gross);     } } Output :-  Dynamic Solution :-  class salary {     public static void main(String args[])     {     double basic=20000.50,gross,da,hra;     Scanner in = new Scanner(System.in);     System.out.println("Enter the Basic Salary

1. Given the school result data, analyses the performance of the students on #different parameters, e.g subject wise or class wise.

1. Given the school result data, analyses the performance of the students on #different parameters, e.g subject wise  or class wise. Solution :-   # x-axis is shows the subject and y -axis # shows the markers in each subject # import pandas and matplotlib  import pandas as pd  import matplotlib.pyplot as plt # Simple Line Chart with setting of Label of X and Y axis, # title for chart line and color of line  subject = ['Physic','Chemistry','Mathematics', 'Biology','Computer'] marks =[80,75,70,78,82] # To draw line in red colour plt.plot(subject,marks,'r',marker ='*')     # To Write Title of the Line Chart plt.title('Marks Scored') # To Put Label At Y Axis plt.xlabel('SUBJECT')           # To Put Label At X Axis plt.ylabel('MARKS')             plt.show() Output :- 

24.Create a Data Frame quarterly sales where each row contains the item category, item name, and expenditure. Group the rows by the category and print the total expenditure per category.

24.Create a Data Frame quarterly sales where each row contains the item category, item name, and expenditure. Group the rows by the category and print the total expenditure per category. import pandas as pd  # initialize list of lists data = [['CAR','Maruti',1000000],['AC','Hitachi',55000],['AIRCOLLER','Bajaj',12000], ['WASHING MACHINE','LG',15000],['CAR','Ford',7000000],['AC','SAMSUNG',45000],['AIRCOLLER','Symphony',20000],['WASHING MACHINE','Wirlpool',25000]] Col=['itemcat','itemname','expenditure'] # Create the pandas DataFrame qrtsales = pd.DataFrame(data,columns=Col) # print dataframe.  print (qrtsales) qs=qrtsales.groupby('itemcat')  print('Result after Filtering Dataframe')  print(qs['itemcat','expenditure'].sum()) Output :-