Java/C3/Abstract-Classes/English
Title of script: Abstract Classes
Author: Joms Antony
Keywords: Abstract Methods, Concrete Methods, Abstract Classes, Concrete Classes, Inheritance, Method Implementation, Video tutorial, java tutorial
|
|
Slide 1: | Welcome to the Spoken Tutorial on Abstract Classes. |
Slide 2:
Learning Objectives
|
In this tutorial we will learn about:
|
Slide 3:
Software Requirements
|
For this tutorial, I am using
|
Slide 4:
Prerequisites
|
To follow this tutorial, you should have knowledge of basics of Java and Eclipse IDE.
You should also have the knowledge of subclassing in Java. If not, for relevant Java tutorials, please visit the link shown. |
First we will see about Abstract Method. | |
Slide 5:
What is an Abstract Method? |
An Abstract method is a method that is declared without implementation.
|
Slide 6:
Abstract Method Example: Highlight the method and abstract keyword |
The method showDetails() illustrated here, is an example of abstract method.
|
Next we will see about concrete method. | |
Slide 7:
Concrete Method: Example:
|
A Concrete method is completely implemented within the curly brackets.
|
Now we will learn about abstract class with an example. | |
Slide 8:
Abstract Class: |
An abstract class usually contains at least one abstract method.
|
Slide 9:
Abstract Class - Example
|
The class Person illustrated here is an abstract class.
|
Slide 10:
Abstract Class - Example |
The figure here represents an inheritance relation.
Here, the Person class is an abstract class. The Employee class and the Student class are subclasses of the Person class. These subclasses can provide their own different implementations. |
Go back to Slide 9 and point the showDetails() method
|
These are done by showDetails( ) method present in the Person class.
For example: ShowDetails() Method in the Employee class prints the Employee ID and the Salary, where as ShowDetails() Method in the Student class prints the Student Reg No and the Grade. |
Slide 11:
Concrete Class: ExampleSlide 10 |
A class is said to be a concrete class, if all the methods in that class are concrete methods.
|
Let us understand the usage of Abstract class with a sample program. | |
In Ecilpse IDE a project called AbstractDemo is created,
|
Now we will switch to Eclipse and create a new project called AbstractDemo.
|
Create the class Person in the src folder.
|
Now, right click on src folder and click new-> class.
|
Type String name; int age; |
Now we will add the fields to represent the name and age of the Person.
|
click on source -> and select generate constructor using fields | Now click on source -> and select generate constructor using fields.
|
Constructor code | This constructor can initialise the values of name and age fields. |
Type
public void showBasicDetails( ) { System.out.println("Name:"+name); System.out.println("Age:"+age); |
We will add a concrete method to this class to print the name and age.
So type public void showBasicDetails( ). Within brackets type, System.out.println within round brackets and within quotes type Name colon plus name semicolon
System.out.println within round brackets and within quotes Age colon plus age semicolon |
Type
public void showDetails( ); |
Now we will add an abstract method to this class.
|
Point to the error |
An error comes up, since we have not yet added the abstract keyword. |
Type the keyword abstract | So now, add the keyword abstract. |
Point to the error
|
Now we can see, another error comes up.
|
Type the keyword abstract | So now, add the keyword abstract to the Person class to make it an abstract class. |
Right click on the default package and create another class called Employee. | Then right click on the default package and create another class called Employee. |
Type extends Person | Now to make this a subclass of Person class, type extends Person. |
Point to the error message | Now we can see an error comes up in the Eclipse IDE.
|
Type
String empid semicolon; int salary;
|
Now create two fields to represent the employee id and employee salary.
|
click on source-> and select generate constructor using fields | Now click on source-> and then select generate constructor using fields.
|
Type
{ System.out.println("Emp id"+empid); System.out.println("Salary:"+salary); }
|
Let us now define the showDetails method.
So type public void showDetails( )
So type System.out.println within quotes Emp id colon plus empid semicolon
|
Next we will see a Student class of the project. | |
Show and highlight the code for Student Class. | I have already created a subclass called Student. |
Highlight the regno and grade fields | There are two fields in the Student class, regno and grade which represent student reg. no and grade. |
Highlight constructor | A constructor is also created inside this class.
|
Highlight the code
{ System.out.println("Student regno:"+regno); System.out.println("Student grade:"+grade); } |
The showDetails method is also implemented in this class.
|
Highlight showDetails() method of Employee class
Highlight showDetails() method of student class |
Now note that the Employee class has its own implementation of showDetails().
|
Right click on the default package, click new-> class and then type name as Demo. | Now right click on the default package.
Click on new-> class and then type name as Demo.
|
Type
Person p =new Person(“John”); |
Now let us try to instantiate the Person class by typing
Person p equals new Person.
|
Point to the error
|
Now we can see an error.
|
Highlight the code line by line
new Employee("John",40,"E267",10000); p1.showBasicDetails(); p1.showDetails();
new Student("Hari",20,"12005",'A'); p2.showBasicDetails(); p2.showDetails(); |
Type the following code as displayed on the screen.
This is represented as Person p2 equals new Student.
|
Right click on the class Demo and then select Run as -> Java Application | Now let us run this Demo program.
|
Highlight code and the output | We can see the output with the basic employee details like name and age.
Student reg no and grade are printed by the showDetails() method. |
This brings us to the end of this tutorial. Let us summarize. | |
Slide 12:
Summary |
In this tutorial we have learnt about
|
Slide 13:
Assignment |
As an assignment-
|
About Project | The video at the following link summarizes the Spoken Tutorial Project.
Please download and watch it. |
About Workshops
|
The Spoken Tutorial Project Team
For more details, please write to us. |
About NMEICT
|
Spoken Tutorial Project is funded by NMEICT, MHRD, Government of India.
More information on this Mission is available at the link shown. |
Contributor slide
|
This script has been contributed by:
Dept. of Information Technology, Amal Jyothi College of Engineering.
|