Abstract classes can define by specifying a
class before abstract keyword but that class must have at least one abstract method
(signature) and we can’t define objects for those classes
import
java.lang.*;
import
java.io.*;
abstract
class car
{
private
String regno;
public void read() throws Exception
{ //
this is the common function for all classes
DataInputStream k= new
DataInputStream(System.in);
regno=k.readLine();
}
abstract
public void steering (); /*this
abstract method is allowed to define the implementing classes according to
their wish but they must implement otherwise those classes still become an
abstract so can’t instantiated */
}
class maruthi extends car
{
//according
to our requirements we can write code for the abstract signatures
public void steering ()
{
System.out.println("the steering is ordinary steering");
System.out.println("the steering is ordinary steering");
}
public void breaks()
{
System.out.println("the
breaks are power breaks");
}}
class santro extends car
{
public void steering ()
{
System.out.println("the streing
is power steering");
}
public void breaks()
{
System.out.println("the breaks
are disk breaks");
}}
class
abstractdemo
{
public
static void main(String ramu[]) throws
Exception
{
car c; //here
we are implementing the abstract class using the reference variable of abstract
class to implementing polymorphism so by that abstract class variable we can
refer various extended objects of the abstract class
DataInputStream
k= new DataInputStream(System.in);
System.out.println("enter
the choice for dynamic polymorphisim\n 1for maruti,2 forsantro");
int
x=Integer.parseInt(k.readLine());
if(x==1)
{
maruthi m=new maruthi();
c=m;
c.read(); //polymorphism is implemented
here
c.steering();
c.breaks();
}
else
{
santro s=new santro();
c=s;
c.read();
c.steering();
c.breaks();
}}}
కామెంట్లు లేవు:
కామెంట్ను పోస్ట్ చేయండి