Class vs abstract vs interface
This presentation is the property of its rightful owner.
Sponsored Links
1 / 13

Class vs Abstract vs Interface PowerPoint PPT Presentation


  • 49 Views
  • Uploaded on
  • Presentation posted in: General

Class vs Abstract vs Interface. Class. 100% concrete (full defined) data type All methods are fully implemented May be directly instantiated (created) but invoking one of its constructors. extends Parent – Child Classes. A class (fully functioning) may have sub-classes.

Download Presentation

Class vs Abstract vs Interface

An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript


Class vs abstract vs interface

Class vs Abstract vs Interface


Class

Class

  • 100% concrete (full defined) data type

  • All methods are fully implemented

  • May be directly instantiated (created) but invoking one of its constructors


Extends parent child classes

extends Parent – Child Classes

  • A class (fully functioning) may have sub-classes.

  • The subclass inherits functionality from the parent


Class vs abstract vs interface

Student is the parent class

HighSchoolStudent is-A student

HighSchoolStudent inherits from Student

Notice that HighSchoolStudent does NOT have a name field or methods related to name. These things are inherited and therefore you do not repeat them in the child class.

Is-A


Class vs abstract vs interface

c.getName() will work because HighSchoolStudent inherited that method from Student


Class vs abstract vs interface

b is a reference to a Student object – when you output b it looks to see what type of Student is it actually referencing and calls the toString of the HighSchoolStudent class.


Abstract

Abstract

  • Incomplete class definition (conceptual)

    • May include fully implemented methods

    • May include unimplemented abstract methods

  • Cannot be directly instantiated

  • Subclasses of an abstract class must:

    • Implement all the abstract methods

      • Or

    • Be declared as abstract also


Class vs abstract vs interface

This class is abstract because there is not a default area or perimeter formula


Class vs abstract vs interface

Both Square and Circle inherit name from the Shape class.

Both Square and Circle implement getArea() and getPerimeter() with the correct formula.


Class vs abstract vs interface

The output is calling the toString for each object. In the toString the getArea() and getPerimeter() methods are called. Java uses dynamic binding to call the correct getArea() method – meaning if it is a Circle Java will call Circle’s getArea() and if it is a Square Java will call Square’s getArea()


Class vs abstract vs interface

You cannot instantiate a Shape because it is abstract

s.getArea() would not work because getArea is abstract in the Shape class.


Interface

Interface

  • 100% abstract (conceptual) data type

  • Declares what operations (functionality) the type will have

  • Doesn’t define any operations (no implemented methods)

  • Only abstract methods

  • Cannot be directly instantiated (NO CONSTRUCTOR)


  • Login