21CS203 / Object Oriented Programming 2025
UNIT-I
INTRODUCTION TO OBJECT ORIENTED PROGRAMMING
ABSTRACTION
Data abstraction is the process of hiding certain details and showing only essential
information to the user.
An abstract is a java modifier applicable for classes and methods in java but not for
Variables.
There are two ways to achieve abstraction in java
1. Interface (100%)
2. Abstract class (0 to 100%)
1. INTERFACE (Full Abstraction)
An interface in Java is a blueprint of a class.
The interface in Java is a mechanism to achieve abstraction
There can be only abstract methods in the Java interface, not method body.
Why use Java interface?
Dr. A.M.Rajeswari, CSE, VCET Page 1 of 10
21CS203 / Object Oriented Programming 2025
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:
1. interface <interface_name>
2. {
3. // declare constant fields
4. // declare methods that abstract by default.
5. }
Interface
o Fields are public, static and final by default.
o Methods are public and abstract.
The relationship between classes and interfaces
Dr. A.M.Rajeswari, CSE, VCET Page 2 of 10
21CS203 / Object Oriented Programming 2025
S. No. Class Interface
1. In class, you can instantiate variables In an interface, you can’t instantiate
and create an object. variables and create an object.
2. Class can contain concrete (with The interface cannot contain concrete
implementation) methods (with implementation) methods
3. The access specifiers used with classes In Interface only one specifier is used -
are private, protected, and public. Public.
Example: 1
1. interface Bank
2. {
3. float rateOfInterest();
4. }
5. class SBI implements Bank
6. {
7. public float rateOfInterest()
8. {return 9.15f;}
9. }
10. class PNB implements Bank
11. {
12. public float rateOfInterest()
13. {return 9.7f;}
14. }
15. class TestInterface2
16. {
17. public static void main(String[] args)
18. {
19. Bank b=new SBI();
20. System.out.println("ROI: "+b.rateOfInterest());
21. }
22. }
Dr. A.M.Rajeswari, CSE, VCET Page 3 of 10
21CS203 / Object Oriented Programming 2025
Example: 2
1. Interface Polygon
2. {
3. void getArea();
4. default void getSides() // default method – Allowed since JDK 8
5. {
6. System.out.println ( "I can get sides of a polygon." );
7. }
8. }
9. // implements the interface
10. Class Rectangle implements Polygon
11. {
12. Public void getArea()
13. {
14. int length = 6;
15. int breadth = 5;
16. int area = length * breadth;
17. System.out.println ( "The area of the rectangle is " + area);
18. }
19. // overrides the getSides()
20. Public void getSides()
21. {
22. System.out.println ("I have 4 sides.");
23. }
24. }
25. // implements the interface
26. Class Square implements Polygon
27. {
28. Public void getArea()
29. {
30. int length = 5;
31. int area = length * length;
32. System.out.println( "The area of the square is " + area);
33. }
34. }
Dr. A.M.Rajeswari, CSE, VCET Page 4 of 10
21CS203 / Object Oriented Programming 2025
35. Class Main
36. {
37. public static void main(String[] args)
38. {
39. // create an object of Rectangle
40. Rectangle r1 = new Rectangle();
41. r1.getArea();
42. r1.getSides();
43. // create an object of Square
44. Square s1 = new Square();
45. s1.getArea();
46. s1.getSides();
47. }
48. }
Output:
The area of the rectangle is 30
I have 4 sides.
The area of the square is 25
I can get sides of a polygon.
Example: 3
1. public interface GPS
2. {
3. public void getCoordinates ();
4. }
5. public interface Radio
6. {
7. public void startRadio();
8. public void stopRadio();
9. }
Dr. A.M.Rajeswari, CSE, VCET Page 5 of 10
21CS203 / Object Oriented Programming 2025
10. public class Smartphone implements GPS, Radio // Multiple inheritance
11. {
12. public void getCoordinates()
13. {
a. // return some coordinates
14. }
15. public void startRadio()
16. {
17. // start Radio
18. }
19. public void stopRadio()
20. {
a. // stop Radio
21. }
22. }
Example: 4
Dr. A.M.Rajeswari, CSE, VCET Page 6 of 10
21CS203 / Object Oriented Programming 2025
2. ABSTRACT CLASS (Partial Abstraction)
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.
Example: 1
// Java Program to Illustrate Abstract Class
// Class 1- Abstract class
1. abstract class Base
2. {
3. // Constructor of base class
4. Base()
5. {
6. System.out.println ("Base Constructor Called");
7. }
Dr. A.M.Rajeswari, CSE, VCET Page 7 of 10
21CS203 / Object Oriented Programming 2025
8. // Abstract method inside class1
9. abstract void fun();
10. }
11. class Derived extends Base
12. {
13. Derived()
14. {
15. System.out.println("Derived Constructor Called");
16. }
17. void fun()
18. {
19. System.out.println("Derived fun() called");
20. }
21. }
22. // Main class
23. Class GFG
24. {
25. public static void main(String args[])
26. {
27. Derived d = new Derived();
28. d.fun();
29. }
30. }
Output
Base Constructor Called
Derived Constructor Called
Derived fun() called
Dr. A.M.Rajeswari, CSE, VCET Page 8 of 10
21CS203 / Object Oriented Programming 2025
Abstract class and interface both are used to achieve abstraction where we can declare the
abstract methods. Abstract class and interface both can't be instantiated.
Abstract class Interface
Abstract class can have abstract and non- Interface can have only abstract methods.
abstract methods. Since Java 8, it can have default and static
methods also.
Abstract class doesn't support multiple Interface supports multiple inheritances.
inheritance.
Abstract class can have final, non-final, static Interface has only static and final variables.
and non-static variables.
Abstract class can provide the Interface can't provide the implementation of
implementation of interface. abstract class.
The abstract keyword is used to declare The interface keyword is used to declare
abstract class. interface.
An abstract class can extend another Java An interface can extend another Java interface
class and implement multiple Java interfaces. only.
An abstract class can be extended using An interface can be implemented using
keyword "extends". keyword "implements".
A Java abstract class can have class members Members of a Java interface are public by
like private, protected, etc. default.
Example: Example:
public abstract class Shape public interface Drawable
{ {
public abstract void draw(); void draw();
} }
Example: Interface and Abstract class
1. //Creating interface that has 4 methods
2. interface A
3. {
4. void a(); //bydefault, public and abstract
5. void b(); void c(); void d();
6. }
7. //Creating abstract class that provides the implementation of one method of A interface
Dr. A.M.Rajeswari, CSE, VCET Page 9 of 10
21CS203 / Object Oriented Programming 2025
8. abstract class B implements A
9. {
10. public void c(){System.out.println("I am C");}
11. }
12. //Creating subclass of abstract class,
13. class M extends B
14. {
15. public void a(){System.out.println("I am a");}
16. public void b(){System.out.println("I am b");}
17. public void d(){System.out.println("I am d");}
18. }
19. //Creating a test class that calls the methods of A interface
20. class Test5
21. {
22. public static void main(String args[])
23. {
24. M a=new M();
25. a.a(); a.b(); a.c(); a.d();
26. }
27. }
OUTPUT
I am a
I am b
I am c
I am d
TUTORIAL
https://www.w3resource.com/java-exercises/java-interface-exercise-4.php
REFERENCES
https://www.c-sharpcorner.com/article/java-interfaces-and-their-implementation-in-real-world/
https://www.geeksforgeeks.org/interfaces-in-java/
https://www.freecodecamp.org/news/java-interfaces-explained-with-examples/
******************
Dr. A.M.Rajeswari, CSE, VCET Page 10 of 10