SlidePub
Home
Categories
Login
Register
Home
General
Abstract classes.pptx Abstract classesAbstract classesAbstract classes
Abstract classes.pptx Abstract classesAbstract classesAbstract classes
yatakonakiran2
12 views
17 slides
Sep 24, 2024
Slide
1
of 17
Previous
Next
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
About This Presentation
Abstract classesAbstract classes
Size:
127.63 KB
Language:
en
Added:
Sep 24, 2024
Slides:
17 pages
Slide Content
Slide 1
Abstract classes © 2017 Wipro wipro.com confidential 1
Slide 2
Agenda © 2017 Wipro wipro.com confidential 2 Abstract Classes
Slide 3
Abstract classes © 2017 Wipro wipro.com confidential 3
Slide 4
Let us see the below example of Figure class extended by Rectangle and Circle. In the above example area() for Figure being more generic we cannot define it. At the level of rectangle or Circle we can give the formula for area. Abstract Classes Figure + int area() Rectangl e + int area() Circle + int area() © 2017 Wipro wipro.com confidential 4
Slide 5
Often, you would want to define a superclass that declares the structure of a given abstraction without providing the implementation of every method The objective is to: Create a superclass that only defines a generalized form that will be shared by all of its subclasses leaving it to each subclass to provide for its own specific implementations Such a class determines the nature of the methods that the subclasses must implement Such a superclass is unable to create a meaningful implementation for a method or methods © 2017 Wipro wipro.com confidential 5 Abstract Classes (Contd.).
Slide 6
The class Figure in the previous example is such a superclass. Figure is a pure geometrical abstraction You have only kinds of figures like Rectangle , Triangle etc. which actually are subclasses of class Figure The class Figure has no implementation for the area( ) method, as there is no way to determine the area of a Figure The Figure class is therefore a partially defined class with no implementation for the area( ) method The definition of area() is simply a placeholder The importance of abstract classes: they define a generalized form (possibly some generalized methods with no implementations) that will be shared by all of its subclasses , so that each subclass can provide specific implementations of such methods. © 2017 Wipro wipro.com confidential 6 Abstract Classes (Contd.).
Slide 7
abstract method – It’s a method declaration with no definition a mechanism which shall ensure that a subclass must compulsorily override such methods . Abstract method in a superclass has to be overridden by all its subclasses. The subclasses cannot make use of the abstract method that they inherit directly(without overriding these methods). These methods are sometimes referred to as subclasses’ responsibility as they have no implementation specified in the superclass © 2017 Wipro wipro.com confidential 7 Abstract Classes (Contd.).
Slide 8
To use an abstract method, use this general form : abstract type name(parameter- list); Abstract methods do not have a body Abstract methods are therefore characterized by the lack of the opening and closing braces that is customary for any other normal method This is a crucial benchmark for identifying an abstract class area method of Figure class made Abstract. public abstract int area (); © 2017 Wipro wipro.com confidential 8 Abstract Classes (Contd.).
Slide 9
Any class that contains one or more abstract methods must also be declared abstract It is perfectly acceptable for an abstract class to implement a concrete method You cannot create objects of an abstract class That is, an abstract class cannot be instantiated with the new keyword Any subclass of an abstract class must either implement all of the abstract methods in the superclass, or be itself declared abstract. © 2017 Wipro wipro.com confidential 9 Abstract Classes (Contd.).
Slide 10
There is no meaningful concept of area( ) for an undefined two- dimensional geometrical abstraction such as a Figure The following version of the program declares area( ) as abstract inside class Figure. This implies that class Figure be declared abstract, and all subclasses derived from class Figure must override area( ). © 2017 Wipro wipro.com confidential 10 Revised Figure Class – using abstract
Slide 11
abstract class Figure{ double dimension1; double dimension2; Figure(double x, double y){ dimension1 = x; dimension2 = y; } abstract double area(); } © 2017 Wipro wipro.com confidential 11 Improved Version of the Figure Class Hierarchy
Slide 12
© 2017 Wipro wipro.com confidential 12 class Rectangle extends Figure{ Rectangle(double x, double y){ super(x,y); } double area(){ System.out.print("Area of rectangle is :"); return dimension1 * dimension2; } } class Triangle extends Figure{ Triangle(double x, double y){ double area(){ super(x,y); } System.out.print("Area for triangle is :"); return dimension1 * dimension2 / 2; } } Improved Version of the Figure Class Hierarchy (Contd.).
Slide 13
class FindArea{ public static void main(String args[]){ Figure fig; Rectangle r = new Rectangle(9,5); Triangle t = new Triangle(10,8); fig = r; System.out.println("Area of rectangle is :" + fig.area()); fig = t; System.out.println("Area of triangle is :" + fig.area()); } } © 2017 Wipro wipro.com confidential 13 Improved Version of the Figure Class Hierarchy (Contd.).
Slide 14
What will be the output for the below code ? class Gbase{ public abstract void testBase(); } public class Sample extends GBase{ public static void main() { Sample ob = new Sample(); ob.testBase(); } } © 2017 Wipro wipro.com confidential 14 Quiz
Slide 15
What will be the output for the below code ? class abstract GBase{ public void testBase(){ System.out.println(“Hello World”); } } public class Sample extends GBase{ public static void main() { GBase ob = new GBase(); ob.testBase(); } } © 2017 Wipro wipro.com confidential 15 Quiz(Contd.).
Slide 16
© 2017 Wipro wipro.com confidential 16
Slide 17
Thank You © 2017 Wipro wipro.com confidential 17
Tags
Categories
General
Download
Download Slideshow
Get the original presentation file
Quick Actions
Embed
Share
Save
Print
Full
Report
Statistics
Views
12
Slides
17
Age
433 days
Related Slideshows
22
Pray For The Peace Of Jerusalem and You Will Prosper
RodolfoMoralesMarcuc
30 views
26
Don_t_Waste_Your_Life_God.....powerpoint
chalobrido8
32 views
31
VILLASUR_FACTORS_TO_CONSIDER_IN_PLATING_SALAD_10-13.pdf
JaiJai148317
30 views
14
Fertility awareness methods for women in the society
Isaiah47
29 views
35
Chapter 5 Arithmetic Functions Computer Organisation and Architecture
RitikSharma297999
26 views
5
syakira bhasa inggris (1) (1).pptx.......
ourcommunity56
28 views
View More in This Category
Embed Slideshow
Dimensions
Width (px)
Height (px)
Start Page
Which slide to start from (1-17)
Options
Auto-play slides
Show controls
Embed Code
Copy Code
Share Slideshow
Share on Social Media
Share on Facebook
Share on Twitter
Share on LinkedIn
Share via Email
Or copy link
Copy
Report Content
Reason for reporting
*
Select a reason...
Inappropriate content
Copyright violation
Spam or misleading
Offensive or hateful
Privacy violation
Other
Slide number
Leave blank if it applies to the entire slideshow
Additional details
*
Help us understand the problem better