Abstract Class:
A class which is declared abstract then that class is abstract class.Abstract class can have both abstract method and non abstract method.
Step1: Created an abstract class
public abstract class vehicle
{
abstract void steering();// abstract method
//This is non abstract method i,e normal method
public void brake()
{
System.out.println("Brake method");
}
}
Step2: Class Audi extends abstract class .All the abstract method has to implement in normal class.
If we not implement abstract method then this class will become abstract class.In below program we have implemented only steering method which is abstract.
public class Audi extends vehicle
{
@Override
void steering()
{
System.out.println("Audi steering");
}
}
Step3: Created another class Benz which extends the same vehicle class
package abstractexp;
public class Benz extends vehicle
{
@Override
void steering()
{
System.out.println("Benz steering");
}
}
Step4: In this class Driver our main method is there.
package abstractexp;
public class Driver
{
public static void main(String[] args)
{
vehicle v=new Audi();
v.steering();//only steering method of Audi class is executed.
v.brake();
// If we want steering method of benz class then
//vehicle v1=new Benz();
// v1.steering();
}
}
A class which is declared abstract then that class is abstract class.Abstract class can have both abstract method and non abstract method.
Step1: Created an abstract class
public abstract class vehicle
{
abstract void steering();// abstract method
//This is non abstract method i,e normal method
public void brake()
{
System.out.println("Brake method");
}
}
Step2: Class Audi extends abstract class .All the abstract method has to implement in normal class.
If we not implement abstract method then this class will become abstract class.In below program we have implemented only steering method which is abstract.
public class Audi extends vehicle
{
@Override
void steering()
{
System.out.println("Audi steering");
}
}
Step3: Created another class Benz which extends the same vehicle class
package abstractexp;
public class Benz extends vehicle
{
@Override
void steering()
{
System.out.println("Benz steering");
}
}
Step4: In this class Driver our main method is there.
package abstractexp;
public class Driver
{
public static void main(String[] args)
{
vehicle v=new Audi();
v.steering();//only steering method of Audi class is executed.
v.brake();
// If we want steering method of benz class then
//vehicle v1=new Benz();
// v1.steering();
}
}
Output:
Audi steering
Brake method
No comments:
Post a Comment