Abstract Join AICTE Telegram Group
Abstract Join AICTE Telegram Group
class Honda extends Bike
{
void run()
{ System.out.println("running safely..");
}
public static void main(String args[])
{
Bike obj = new Honda();
obj.run();
}
}
//example of abstract class having constructor, field and method
abstract class Bike
{ int limit=30;
Bike( )
{ System.out.println("constructor is invoked");}
void getDetails() {
System.out.println("it has two wheels"); }
abstract void run();
}
class Honda extends Bike{
void run(){ System.out.println("running safely.."); }
public static void main(String args[]){
Bike obj = new Honda();
obj.run();
obj.getDetails();
System.out.println(obj.limit); } }
Methods with varargs
Varags represents variable length arguments in
methods.
It makes the code simpler and flexible.
<access specifier> <static> void method-name(object…arguments)
in which
Object is type of an argument.
Ellipsis (…) is the key to varargs and
arguments is the name of variable.
Thus varargs allows us to declare a method with the
unspecified number of parameters.
The varargs must be the final argument in the argument
list of a method.
Example :
Public void sample(String username, String password, String mailId);
class Exampleprg
{
Exampleprg (String… person)
{
for(String name: person)
{
System.out.println(“Hello ”+ name);
}
}
public static void main(String args[])
{
Exampleprg(“John”, “Janny”, “Janardan”);
}
}
VISIBILITY CONTROL IN JAVA
ACCESS MODIFIERS
Visibility modifiers also known as access modifiers.
To restrict the access to variables and methods outside class
access specifiers are used
Java provides the following access modifiers
Public
Friendly/Package (default)
Private
Protected
Private protected
PUBLIC ACCESS
Any variable or method is visible to the entire class in which
it is defined.
What if we want to make it visible to all classes outside the
current class??
This is possible by simply declaring the variable or method
as ‘public’.
public int number;
public void sum( )
{…..}
A variable declared as public has widest possible visibility
and accessible everywhere.
FRIENDLY ACCESS
When no access modifier is specified, the member is
known as ‘friendly’ level of access.
The difference between the public and the friendly
access is that –
public modifier makes the field visible in all classes
regardless of their package.
while the friendly access make them visible only within
the current package and not within the other packages.
Access Location
Same class Yes Yes Yes Yes Yes
public static void main(String args[]){
student s1 = new student(111,"Karan");
student s2 = new student(321,"Aryan"); Output:
s1.display();
s2.display();
0 null
} 0 null
}
Solution of the above problem by this keyword
class Student{
int id;
String name;
student(int id, String name)
{
this.id = id;
this.name = name;
}
void display(){System.out.println(id+" "+name);}
public static void main(String args[])
{
Student s1 = new Student(111,"Karan");
Student s2 = new Student(222,"Aryan");
s1.display();
s2.display();
} }
Garbage Collection
Objects are dynamically allocated by using new
operator.
Such objects are destroyed and their memory must
be released for later reallocation.
In java, the destruction of object takes place
automatically.
Garbage collection is a process in which the memory
allocated to objects, which are no longer in use can
be reclaimed for further use or free.
Garbage Collection
The garbage collection is a technique, which
handles the destroying of the objects in java.
Disadvantage of Array
•Size Limit:
• We can store only fixed size of elements in the
array. It doesn't grow its size at runtime.
Types of Array
There are two types of array.
Single Dimensional Array
Multidimensional Array
Single Dimensional Array
A list of items are given in one variable name
using only one subscript.
Syntax to Declare an Array in java
dataType[ ] arrayRefVar; (or)
dataType [ ]arrayRefVar; (or)
dataType arrayRefVar[ ];
//printing array
for(int i=0;i<a.length;i++)
System.out.println(a[i]);
}
}
Passing Java Array in the method
class B{
static void min(int arr[])
{
int min=arr[0];
for(int i=1;i<arr.length;i++)
if(min>arr[i])
min=arr[i];
System.out.println(min);
}
public static void main(String args[])
{
int a[]={33,3,4,5};
min(a); //passing array in the method
}}
Multidimensional array
In such case, data is stored in row and column based
index (also known as matrix form).
Syntax to Declare Multidimensional Array in java
dataType[ ][ ] arrayRefVar; (or)
dataType [ ][ ]arrayRefVar; (or)
dataType arrayRefVar[ ][ ]; (or)
dataType [ ]arrayRefVar[ ];
Example to instantiate Multidimensional Array in java