Introduction about Abstract class:-
- Abstract class is incomplete class which may have contained abstract method as well as concrete method
- In a class at least one method is abstract than class must be define as abstract class.
- Abstract method can’t have body or implementation.
- When a class inherits the abstract class than its mandatory for child class to override all the parent abstract method, otherwise child class also become abstract.
- We can’t create an object of abstract class but parent abstract class can refer any of its child objects in the entire inheritance hierarchy.
- In abstract class can have constructor (User define, as well as default)
- Constructor of abstract class required to instance variable of abstract class
- We can’t call constructor of an abstract class by using keyword “new” but it can be called by its child class constructor.
- A class can still be called as an abstract even through it doesn’t have abstract method.
- Variable & constructor or can’t be abstract.
Advantage:-
- Can achieve abstraction.
- Lose coupling.
- Zero dependency.
- Standardization in method definition.
Example :- Abstract class
public abstract class Mobile {
public void call()
{
System.out.println ("call function....");
}
public abstract void middleButton();
}
public class NokiaMobile extends Mobile{
@Override
public void middleButton() {
System.out.println ("start music player...");
} }
public class SonyMobile extends Mobile {
@Override
public void middleButton() {
System.out.println("start camera...");
}
}
public class Run
{
public static void main(String[] args)
{
Mobile m=null;
System.out.println ("first call sony mobile methods");
m=new Sonymobile();//PRCO
m.call();
m.middleButton();
System.out.println ("now call Nokia mobile methods");
m=new NokiaMobile();//PRCO m.call();
m.middleButton ();
}
}
OUT PUT:-
first call sony mobile methods
call function....
start camera...
now call Nokia mobile methods
call function....
start music player...
public void call()
{
System.out.println ("call function....");
}
public abstract void middleButton();
}
public class NokiaMobile extends Mobile{
@Override
public void middleButton() {
System.out.println ("start music player...");
} }
public class SonyMobile extends Mobile {
@Override
public void middleButton() {
System.out.println("start camera...");
}
}
public class Run
{
public static void main(String[] args)
{
Mobile m=null;
System.out.println ("first call sony mobile methods");
m=new Sonymobile();//PRCO
m.call();
m.middleButton();
System.out.println ("now call Nokia mobile methods");
m=new NokiaMobile();//PRCO m.call();
m.middleButton ();
}
}
OUT PUT:-
first call sony mobile methods
call function....
start camera...
now call Nokia mobile methods
call function....
start music player...
Give me ans of following question.
Think:-“what is meaning of System.out.Println(“”) ????????”Think:-“why main() method is static?????????”
No comments:
Post a Comment