ABSTRACT CLASSES AND METHODS
Abstract class in Java
• A class which is declared with the abstract keyword is known as
an abstract class in Java. It can have abstract and non-abstract
methods (method with the body).
Abstraction in Java
• Abstraction is a process of hiding the implementation details and
showing only functionality to the user.
• Another way, it shows only essential things to the user and hides
the internal details. Abstraction lets you focus on what
the object does instead of how it does it.
• There are two ways to achieve abstraction in java
✔ Abstract class (0 to 100%)
✔ Interface (100%)
Abstract class in Java
• A class which is declared as abstract is known as an abstract class.
It can have abstract and non-abstract methods. It needs to be
extended and its method implemented. It cannot be instantiated.
• Points to Remember
• An abstract class must be declared with an abstract keyword.
• It can have abstract and non-abstract methods.
• It cannot be instantiated.
• It can have constructors and static methods also.
• It can have final methods which will force the subclass not to
change the body of the method.
Example of abstract class
abstract class A{}
Abstract Method in Java
• A method which is declared as abstract and does not have
implementation is known as an abstract method.
Example of abstract method
abstract void printStatus();//no method body and abst
ract
Example of Abstract class that has an abstract method
abstract class Bike
{
abstract void run();
}
class Honda extends Bike
{
void run()
{
System.out.println("running safely
");
}
public static void main(String ar
gs[])
{
Bike obj = new Honda();
obj.run();
}
}
Understanding the real scenario of Abstract class
abstract class Shape
{
abstract void draw();
}
class Rectangle extends Shape
{
void draw()
{
System.out.println("drawing rectang
le");
}
}
class Circle1 extends Shape
{
void draw()
{
System.out.println("drawing circle")
;
}
}
class TestAbstraction1
{
public static void
main(String args[])
{
Shape s=new Circl
e1();
s.draw();
}
}
Interface in Java
• An interface in Java is a blueprint of a class. It has
static constants and abstract methods.
• The interface in Java is a mechanism to
achieve abstraction. There can be only abstract
methods in the Java interface, not method body. It
is used to achieve abstraction and
multiple inheritance in Java.
• Java Interface also represents the IS-A relationship.
• It cannot be instantiated just like the abstract class.
How to declare an interface?
• An interface is declared by using the interface keyword.
• It provides total abstraction; means all the methods in an
interface are declared with the empty body, and all the fields
are public, static and final by default.
• A class that implements an interface must implement all the
methods declared in the interface.
Syntax:
interface <interface_name>{
// declare constant fields
// declare methods that abstract
// by default.
}
Internal addition by the compiler
Interface fields are public, static and final by default, and the methods
are public and abstract.
The relationship between classes and interfaces
• As shown in the figure given below, a class extends
another class, an interface extends another interface,
but a class implements an interface.
Example
interface printable{
void print();
}
class A6 implements printable{
public void print(){System.out.println("Hello");}
public static void main(String args[]){
A6 obj = new A6();
obj.print();
}
}
//Interface declaration: by first user
interface Drawable
{
void draw();
}
//Implementation: by second user
class Rectangle implements Drawab
le
{
public void draw()
{
System.out.println("drawing r
ectangle");
}
}
class Circle implements Drawable
{
public void draw()
{
System.out.println("drawing c
ircle");
}
}
//Using interface: by third user
class TestInterface1
{
public static void main(String a
rgs[])
{
Drawable d=new Circle()
;
d.draw();
}
}
Multiple inheritance in Java by interface
• If a class implements multiple interfaces, or an
interface extends multiple interfaces, it is known as
multiple inheritance.
Example
interface Printable
{
void print();
}
interface Showable
{
void show();
}
class A7 implements Printable,Showable
{
public void print()
{
System.out.println("Hello");
}
public void show()
{
System.out.println("Welcome");
}
}
public static void main(String args[])
{
A7 obj = new A7();
obj.print();
obj.show();
}

ABSTRACT CLASSES AND INTERFACES.ppt

  • 1.
  • 2.
    Abstract class inJava • A class which is declared with the abstract keyword is known as an abstract class in Java. It can have abstract and non-abstract methods (method with the body). Abstraction in Java • Abstraction is a process of hiding the implementation details and showing only functionality to the user. • Another way, it shows only essential things to the user and hides the internal details. Abstraction lets you focus on what the object does instead of how it does it. • There are two ways to achieve abstraction in java ✔ Abstract class (0 to 100%) ✔ Interface (100%)
  • 3.
    Abstract class inJava • A class which is declared as abstract is known as an abstract class. It can have abstract and non-abstract methods. It needs to be extended and its method implemented. It cannot be instantiated. • Points to Remember • An abstract class must be declared with an abstract keyword. • It can have abstract and non-abstract methods. • It cannot be instantiated. • It can have constructors and static methods also. • It can have final methods which will force the subclass not to change the body of the method. Example of abstract class abstract class A{}
  • 4.
    Abstract Method inJava • A method which is declared as abstract and does not have implementation is known as an abstract method. Example of abstract method abstract void printStatus();//no method body and abst ract
  • 5.
    Example of Abstractclass that has an abstract method abstract class Bike { abstract void run(); } class Honda extends Bike { void run() { System.out.println("running safely "); } public static void main(String ar gs[]) { Bike obj = new Honda(); obj.run(); } }
  • 6.
    Understanding the realscenario of Abstract class abstract class Shape { abstract void draw(); } class Rectangle extends Shape { void draw() { System.out.println("drawing rectang le"); } } class Circle1 extends Shape { void draw() { System.out.println("drawing circle") ; } } class TestAbstraction1 { public static void main(String args[]) { Shape s=new Circl e1(); s.draw(); } }
  • 7.
    Interface in Java •An interface in Java is a blueprint of a class. It has static constants and abstract methods. • The interface in Java is a mechanism to achieve abstraction. There can be only abstract methods in the Java interface, not method body. It is used to achieve abstraction and multiple inheritance in Java. • Java Interface also represents the IS-A relationship. • It cannot be instantiated just like the abstract class.
  • 8.
    How to declarean interface? • An interface is declared by using the interface keyword. • It provides total abstraction; means all the methods in an interface are declared with the empty body, and all the fields are public, static and final by default. • A class that implements an interface must implement all the methods declared in the interface. Syntax: interface <interface_name>{ // declare constant fields // declare methods that abstract // by default. }
  • 9.
    Internal addition bythe compiler Interface fields are public, static and final by default, and the methods are public and abstract.
  • 10.
    The relationship betweenclasses and interfaces • As shown in the figure given below, a class extends another class, an interface extends another interface, but a class implements an interface.
  • 11.
    Example interface printable{ void print(); } classA6 implements printable{ public void print(){System.out.println("Hello");} public static void main(String args[]){ A6 obj = new A6(); obj.print(); } }
  • 12.
    //Interface declaration: byfirst user interface Drawable { void draw(); } //Implementation: by second user class Rectangle implements Drawab le { public void draw() { System.out.println("drawing r ectangle"); } } class Circle implements Drawable { public void draw() { System.out.println("drawing c ircle"); } } //Using interface: by third user class TestInterface1 { public static void main(String a rgs[]) { Drawable d=new Circle() ; d.draw(); } }
  • 13.
    Multiple inheritance inJava by interface • If a class implements multiple interfaces, or an interface extends multiple interfaces, it is known as multiple inheritance.
  • 14.
    Example interface Printable { void print(); } interfaceShowable { void show(); } class A7 implements Printable,Showable { public void print() { System.out.println("Hello"); } public void show() { System.out.println("Welcome"); } } public static void main(String args[]) { A7 obj = new A7(); obj.print(); obj.show(); }