You must know
The way of programming can be divided into 2 categories:
1. Structural/Procedural languages
2. object oriented languages
You must know
Object word is taken from the real world examples….
Class can be considered as a blueprint for various objects.
Ch-8
Classes and Objects in Java
Class and object
Understanding an example
class Room |
length, width, height, nWindows |
setAttr(), area(), display() |
class RoomDemo |
Contains main() function |
Contains objects:r1 and r2 |
Program name : RoomDemo.java
Class Room
{
float length, width, height;
byte nWindows;
void setAttr(float l, float w, float h, byte n)
{
length = l;width = w;height = h;
nWindows = n;
}
double area()
{ return (length * width); }
void display()
{
System.out.println(“\n Length : “ + length);
System.out.println(“\n Width : “ + width);
System.out.println(“\n Height : “ + height);
System.out.println(“\n Number of windows : “ + nWindows);
}
}
class RoomDemo
{
public static void main(String args[])
{
Room r1;
r1 = new Room();
Room r2 = new Room();
r1.display();
r2.display();
r1.setAttr(18, 12.5f, 10, (byte)2);
r2.setAttr(14, 11, 10, (byte)1);
r1.display();
r2.display();
System.out.println(“\n Area of r1 is : “ + r1.area());
System.out.println(“\n Area of r2 is : “ + r2.area());
}
}
Understanding program
Classification of Variables
Local Variables
Local Variable
Instance Variables
Instance variable
Local Variable
ClassVariables
Instance variable
Local Variable
Class variable
Polymorphism
Polymorphism
Class printLine
{
static void printline()
{
for(int i=0;i<=40;i++)
System.out.print(‘=‘);
System.out.println();
}
static void printline(int n)
{
for(int i=0;i<=n;i++)
System.out.print(‘#’);
System.out.println();
}
Methods with the same name
Havin g different signature.
Method overloading
Constructor
Default constructor
Constructor Overloading
For eg.
class Room
{
float length, width,height;
Room(float l, float w, float h)
{
length = l;width = w;height=h;
}
Room(float l, float w)
{
length=l;width=w;
}
Constructor overloading with
Different signatures.
User defined constructor
class Room
{
float length, width,height;
Room();
Room(float l, float w, float h)
{
length = l;width = w;height=h;
}
Room(float l, float w)
{
length=l;width=w;
}
Visibility Modifier
Access Modifier public package protected private
Visibility widest 🡪 🡪 🡪 🡪 🡪 narrowest
public
package
protected
private
class Rectangle
{
private double length, width;
{
Rectangle(double x, double y)
{
length =x; width = y;
}
Rectangle(){};
double area() {return length * width;}
void display()
{System.out.println(“Rectangle with length=“+length + “width” +width);}
}
Class RectangleVisibility1
{
public static void main(String[] s)
{ Rectangle rect1;
rect1 = new Rectangle();
Rectangle rect2 = new Rectangle(10,15);
rect1.display();rect2.display();
System.out.println(“Area of rectangle with length” +rect2.length+”width”+ � rect2.width + “is” + rect2.area());
}
}
Length & width can’t be accessed
due to private.
class Rectangle
{
private double length, width;
{
Rectangle(double x, double y)
{
length =x; width = y;
}
Rectangle(){};
double area() {return length * width;}
void display()
{System.out.println(“Rectangle with length=“+length + “width” +width);}
double getlength(){return length;} double getwidth() {return witdh;}
}
Class RectangleVisibility1
{
public static void main(String[] s)
{ Rectangle rect1;
rect1 = new Rectangle();
Rectangle rect2 = new Rectangle(10,15);
rect1.display();rect2.display();
System.out.println(“Area of rectangle with length” +rect2.getlength()+”width”+ rect2.getwidth() + “is” + rect2.area());
}
}
Accessor and Mutator Methods
Inheritance
Inheritance
Room |
Length:float Width:float Height:float nWindows:byte |
Room() Room(float l, float w, float h, byte n) Room(float l, float w) Void display() |
Classroom |
nBenches:int nSeatsBench:int |
Classroom() Classroom(float l, float w, float h, byte n, int nB, int nSB) Void show() Void display() Int getSeats() |
Super class or Parent class
sub class or child class
Inheritance
Where Room is super class and Classroom is its subclass.
Any method or variable available in superclass can be used from its subclass by following statement.
For eg. To call display method of a superclass we can use the statement.
super.display();
How many types of variables are there in Java?
3
Variables defined inside methods or blocks are called _______
Local Variables
Which type of variables contain highest availability scope?
Class variables
__________ variables are variables defined within a class but outside any method.
Instance variables
Which variables are allocated memory only once per class and is shared by all its objects.
Class Variables
___________ means many forms.
Polymorphism
_________ is a special kind of method that is invoked when a new object is created.
Constructor
What is the main purpose of constructor?
To perform initializing actions
Each and every class contains a constructor known as ________.
Default constructor
How many visibility modifiers are there?
4
Name all the visibility modifiers.
Public , package , protected and private
Each and every class contains a constructor known as ________.
Default constructor
Which constructor is not available in presence of user defined constructor?
Default Constructor
If we want to make a variable visible to all the classes outside this class, declare the method or variable __________
public
What is the visibility of main function?
public
_________ is indicated by the lack of any access modifier keyword in a declaration.
package
Which visibility provides data encapsulation technique.
private
Which level of protection has wider visibility than private but narrower than public or package.
protected
Which level of protection is used to allow the access only to subclasses or to share with the methods declared as friend.
protected
Which is the default level of protection.
package
Which level of protection has wider visibility than private but narrower than public or package.
protected