Abstract :-
abstract is a keyword to make any method abstract and abstract method doesnot have any body we will get compile time error if we specify any body to a abstract method.
Abstract method can only be given inside an abstract class.
abstract class A
{
abstract void display();
}
While abstract class can have abstract as well as non abstract method.
We can’t create a object for abstract class. This means only way to use abstract class is inheritance.
Final & Abstract keyword can’t be used together because both have opposite meaning.
Eg.
final abstract class A //not possible
{
final abstract void display(); //not possible
}
*It is must to overwrite abstract method into its child class otherwise we will get compile time error
*
class B extends A
{
void display()
{
}
}
Note:-
When we create object Buffered class we have to pass the object of Reader class but Reader class but reader class is abstract class so its object cannot be created. We have a class named InputStreamReader inherited from Reader class so we can pass object of InputStreamReader while creating object of BufferedReader.
Now InputStreamReader needs object of InputStream but InputStream is a abstract class so we have to pass a variable ‘in’ of type InputStream declared in System class as a static member variable.
‘out’ is static member variable of System class and of type PrintStream and print is a method of class PrintStream that can accessed through ‘out’ (as it is of type PrintStream).
Interface:-
Way to use multiple inheritance.
Interface is a keyword.
In a Interface by default:-
1.)All variables are public, static & final
2.)All methods are public & abstract.
interface B
{
void hello();
}
class A implements B
{
public void hello()
{
}
}
Abstract method cannot be static.
We cannot have body for any method in interface as it is abstract and it is must to overwrite all the methods of interface in its inherited class. Interface method cannot be static.
The overrided method of interface must be public as its access can’t be decreased.
interface B
{
int a=10;
void hello();
}
class A implements B
{
public void hello()
{
a+=5;
System.out.print(a);
}
}
Will show error as ‘a’ is final by default
Final variables must be initialised at the time of declaration and its value cannot be changed.
*An interface can inherit an another interface and extends is the keyword to perform inheritance.
*One interface cannot be inherited from more than one interface.
*implements is the keyword to inherit any interface to the class & a class can be inherited from any number of interface.
Eg.
class C implements A,B,C
{
}
1.interface extends interface True
2.interface implements class False
3.interface extends class False
4.interface implements interface False
5.class extends class True
6.class implements interface True
7.class extends interface False
8.class implements class False
class extends C implements D,E,F
{
}
Static:-
We can mark any variable static or method static when a variable is marked static.
Eg.
Static int a;
Member variables allocate memory differently for different objects. It means for each object different memory is allocated to member variables. Static variable allocates only once to a class and this memory is allocated when we firstly refers to class. So changes made by one object in the static variable are reflected in the other objects.
Static variables can be accessed through class name it means there is no need of object to access static member.
Eg.
class A
{
public int b;
public int d;
public static int e;
}
class C extends A
{
public static void main(String[] arg)
{
A a1=new A();
a1.b=5;
a1.e=5;
A a2=new A();
System.out.println(a2.b);
System.out.println(A.e);
}
}
Result:-
0
5
When we mark any method static it can be accessed with class name no need of object.
In a static method non-static members can’t be used directly.
Eg.
class A
{
int e1;
public static void main(String[] arg)
{
new A().e1;
System.out.println(e1);
}
}
To access non-static member into a static method we need object.
Abstract method cannot be static.


No comments:
Post a Comment