We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 13
Abstract class in Java
Abstract Class
• A class that is declared with abstract keyword, is known as abstract
class in java. It can have abstract and non-abstract methods (method with body). • Before learning java abstract class, let's understand the abstraction in java first. Abstraction in Java
• Abstraction is a process of hiding the implementation details and
showing only functionality to the user. • Another way, it shows only important things to the user and hides the internal details for example sending sms, you just type the text and send the message. You don't know the internal processing about the message delivery. • Abstraction lets you focus on what the object does instead of how it does it. Ways to achieve Abstraction
• There are two ways to achieve abstraction in java
• Abstract class (0 to 100%) • Interface (100%) Abstract class in Java
• A class that is declared as abstract is known as abstract class. It needs
to be extended and its method implemented. It cannot be instantiated. • Example of abstract class o abstract class A{} Abstract Method
• A method that is declared as abstract and does not have
implementation is known as abstract method. • Example of abstract method o abstract void printStatus();//no body and abstract Example of abstract class that has abstract method
abstract class Bike{ Bike obj = new Honda4();
abstract void run(); obj.run(); } } class Honda4 extends Bike{ } void run() {System.out.println("running Output safely..");} running safely.. public static void main(Stri ng args[]){ Example 2:TestBank.java class TestBank{ abstract class Bank{ public static void main(String arg abstract int getRateOfInterest(); s[]){ Bank b; } b=new SBI(); class SBI extends Bank{ System.out.println("Rate of Interes int getRateOfInterest() t is: "+b.getRateOfInterest() {return 7;} +" %"); b=new PNB(); } System.out.println("Rate of Interes class PNB extends Bank{ t is: "+b.getRateOfInterest() int getRateOfInterest() +" %"); Output {return 8;} Rate of Interest is: }} 7% } Rate of Interest is: 8 % Abstract class
Abstract class have constructor, data member, methods etc.
An abstract class can have data member, abstract method, method body, constructor and even main() method. TestAbstraction2.java class Honda extends Bike{ //example of abstract class that void run() have method body {System.out.println("running saf abstract class Bike ely..");} { } Bike() class TestAbstraction2{ {System.out.println("bike is creat public static void main(String ed");} args[]) { abstract void run(); Bike obj = new Honda(); void changeGear() obj.run(); { obj.changeGear(); } System.out.println("gear changed output} "); } bike is created } running safely.. gear changed RULES
• Rule: If there is any abstract method in a class, that class must be
abstract. class Bike12 { abstract void run(); } • Rule: If you are extending any abstract class that have abstract method, you must either provide the implementation of the method or make this class abstract. Questions
1. Can abstract class have constructors in Java?
2. Can abstract class implements interface in Java? 3. does they require to implement all methods? Can abstract class be final in Java? 4. What will happen if we define a concrete method in an interface? 5. What will happen if we not initialize variables in an interface 6. Can we create object for an interface?