Download presentation
Presentation is loading. Please wait.
1
Sampath Kumar S Assistant Professor, SECE
Abstract class Sampath Kumar S Assistant Professor, SECE
2
Abstraction 11/29/2018 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. Sampath Kumar S, AP
3
Ways to achieve Abstraction
11/29/2018 Ways to achieve Abstraction There are two ways to achieve abstraction in java Abstract class Interface Sampath Kumar S, AP
4
Abstract class 11/29/2018 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. Syntax to declare the abstract class abstract class <class_name> { } Sampath Kumar S, AP
5
Abstract Method 11/29/2018 A method that is declared as abstract and does not have implementation is known as abstract method. Syntax to declare the abstract method abstract return_type <method_name>(); //no braces{} Sampath Kumar S, AP
6
Example: abstract method
11/29/2018 abstract class Bike{ abstract void run(); } class Honda extends Bike{ void run(){ System.out.println("running safely"); public static void main(String args[]){ Bike obj = new Honda(); obj.run(); Output: running safely Sampath Kumar S, AP
7
11/29/2018 Example: Scenario abstract class Shape{ abstract void draw(); } class Rectangle extends Shape{ void draw(){ System.out.println("drawing rectangle"); } } class Circle extends Shape{ System.out.println("drawing circle"); class Test{ public static void main(String args[]){ Shape s=new Circle(); s.draw(); } } Note: Shape is the abstract class, its implementation is provided by the Rectangle and Circle classes. Output: drawing circle Sampath Kumar S, AP
8
Example: Method Output: This is third IT All Students are good!!!
11/29/2018 Example: Method abstract class finalIt { abstract void student(); void goodStudents(){ System.out.println(“All Students are good!!!"); } } class thirdIt extends finalIT { void student(){ System.out.println(“This is third IT"); } public static void main(String args[]){ finalIt obj = new thirdIt(); obj.student(); obj.goodStudents(); } Output: This is third IT All Students are good!!! Sampath Kumar S, AP
9
Example: Constructor, field and method
11/29/2018 Example: Constructor, field and method abstract class finalIt { int total=52; finalIt(){ System.out.println("constructor is invoked"); } void getDetails(){ System.out.println(“All Students are good!!!"); } void student(){ } } class thirdIt extends finalIT { void student(){ System.out.println(“This is third IT"); } public static void main(String args[]){ finalIt obj = new thirdIt(); obj.student(); obj. getDetails(); System.out.println(obj.total); } Output: constructor is invoked This is third IT All Students are good!!! 52 Sampath Kumar S, AP
10
11/29/2018 Some Rules: If there is any abstract method in a class, that class must be abstract. Example class Bike{ abstract void run(); } While extending any abstract class that have abstract method, provide either the implementation of the method or make the class abstract. Output: compile time error Sampath Kumar S, AP
11
11/29/2018 Sampath Kumar S, AP
12
11/29/2018 Thank You Sampath Kumar S, AP
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.