Unit-3 Exception handling And Inheritance
Interface and abstract class
Abstraction in Java: -
o 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)
o Abstraction is a process of hiding the implementation details and showing
only functionality to the user.
o it shows only essential things to the user and hides the internal details, for
example, sending SMS where you type the text and send the message. You
don't know the internal processing about the message delivery.
Ways to achieve abstraction: -
1. Abstract class
2. Interface
1. Abstract class: -
o 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.
o An abstract class must be declared with an abstract keyword.
o It can have abstract and non-abstract methods.
o It cannot be instantiated.
o It can have constructors and static methods also.
o It can have final methods which will force the subclass not to change the body
of the method.
o Syntax: - abstract class A{ }
o Example: -
abstract class Shape {
abstract void draw ();
}
//In real scenario, implementation is provided by others i.e. unknown by end user
class Rectangle extends Shape{
void draw(){System.out.println("drawing rectangle");}
}
class Circle1 extends Shape{
void draw(){System.out.println("drawing circle");}
, }
//In real scenario, method is called by programmer or user
class TestAbstraction1{
public static void main(String args[]){
Shape s=new Circle1();//In a real scenario, object is provided through method, e.g.,
getShape() method
s.draw();
}
}
o An abstract class can have a data member, abstract method, method body
(non-abstract method), constructor, and even main() method.
o Example: -
abstract class Bike{
Bike(){System.out.println("bike is created");}
abstract void run();
void changeGear(){System.out.println("gear changed");}
}
//Creating a Child class which inherits Abstract class
class Honda extends Bike{
void run(){System.out.println("running safely..");}
}
//Creating a Test class which calls abstract and non-abstract methods
class TestAbstraction2{
public static void main(String args[]){
Bike obj = new Honda();
obj.run();
obj.changeGear();
}
}
Interface: -
o An interface in Java is a blueprint of a class. It has static constants and
abstract methods.
o 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.
Interface and abstract class
Abstraction in Java: -
o 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)
o Abstraction is a process of hiding the implementation details and showing
only functionality to the user.
o it shows only essential things to the user and hides the internal details, for
example, sending SMS where you type the text and send the message. You
don't know the internal processing about the message delivery.
Ways to achieve abstraction: -
1. Abstract class
2. Interface
1. Abstract class: -
o 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.
o An abstract class must be declared with an abstract keyword.
o It can have abstract and non-abstract methods.
o It cannot be instantiated.
o It can have constructors and static methods also.
o It can have final methods which will force the subclass not to change the body
of the method.
o Syntax: - abstract class A{ }
o Example: -
abstract class Shape {
abstract void draw ();
}
//In real scenario, implementation is provided by others i.e. unknown by end user
class Rectangle extends Shape{
void draw(){System.out.println("drawing rectangle");}
}
class Circle1 extends Shape{
void draw(){System.out.println("drawing circle");}
, }
//In real scenario, method is called by programmer or user
class TestAbstraction1{
public static void main(String args[]){
Shape s=new Circle1();//In a real scenario, object is provided through method, e.g.,
getShape() method
s.draw();
}
}
o An abstract class can have a data member, abstract method, method body
(non-abstract method), constructor, and even main() method.
o Example: -
abstract class Bike{
Bike(){System.out.println("bike is created");}
abstract void run();
void changeGear(){System.out.println("gear changed");}
}
//Creating a Child class which inherits Abstract class
class Honda extends Bike{
void run(){System.out.println("running safely..");}
}
//Creating a Test class which calls abstract and non-abstract methods
class TestAbstraction2{
public static void main(String args[]){
Bike obj = new Honda();
obj.run();
obj.changeGear();
}
}
Interface: -
o An interface in Java is a blueprint of a class. It has static constants and
abstract methods.
o 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.