Java/C3/Abstract-Classes/English-timed

From Script | Spoken-Tutorial
Revision as of 17:33, 24 April 2019 by PoojaMoolya (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search
Time
Narration
00:00 Welcome to the Spoken Tutorial on Abstract Classes.
00:05 In this tutorial, we will learn about: Abstract Methods and Concrete Methods
00:12 Abstract Classes and Concrete Classes and
00:16 How to use Abstract Classes.
00:18 For this tutorial, I am using:

Ubuntu 12.04

JDK 1.7 and

Eclipse 4.3.1

00:28 To follow this tutorial, you should have knowledge of basics of Java and Eclipse IDE.
00:36 You should also have the knowledge of subclassing in Java.
00:40 If not, for relevant Java tutorials, please visit the link shown.
00:46 First we will see about Abstract Method.
00:50 An Abstract method is a method that is declared without implementation.
00:55 It is declared using abstract keyword.
00:59 There should not be opening and closing parenthesis for this method.
01:04 Let us understand the usage of Abstract class with a sample program.
01:09 Now we will switch to Eclipse and create a new project called AbstractDemo.
01:16 Inside this project, we will create the necessary classes to demonstrate the usage of Abstract class.
01:24 Now, right-click on src folder and click New > Class.
01:30 Type the name of the class as Person and press Enter.
01:35 Now we will add the fields to represent the name and the age of the Person. Type: String name semicolon.
01:44 Also type: int age semicolon.
01:48 Now click on Source and select Generate constructor using fields.
01:55 Delete the super keyword from the generated code.
01:59 The constructor can initialise the values of name and age fields.
02:05 Next we will see about concrete method.
02:08 A Concrete method is completely implemented within the curly brackets.
02:14 We will add a concrete method to this class to print the name and age.
02:21 Type the following code as displayed on the screen.
02:25 This method showBasicDetails() illustrated here, is an example of concrete method.
02:32 Observe that this method is implemented completely.
02:36 Now we will add an abstract method to this class.
02:41 So, type: public void showDetails( ) semicolon.
02:46 An error comes up, since we have not yet added the abstract keyword.
02:51 So, now add the keyword abstract.
02:55 Now we can see another error comes up.
02:58 This is because, abstract methods can be added only to abstract classes.
03:03 So, now add the keyword abstract to the Person class to make it an abstract class.
03:10 The class Person illustrated here is an abstract class.
03:15 It contains an abstract method called showDetails().
03:20 The figure here represents an inheritance relation.
03:24 Here, the Person class is an abstract class.
03:29 The Employee class and the Student class are subclasses of the Person class.
03:35 These subclasses can provide their own different implementations.
03:40 These are done by showDetails( ) method present in the Person class.
03:45 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 Register Number and Grade.
04:01 Then right-click on the default package and create another class called Employee.
04:07 Now to make this a subclass of Person class, type: extends Person.
04:14 Now, we can see an error comes up in the Eclipse IDE.
04:19 It indicates that we should provide an implementation to the abstract method showDetails( ).
04:26 We will do it a little later.
04:28 Now create two fields to represent the employee id and employee salary.
04:34 So, type: String empid semicolon and int salary semicolon.
04:42 Now click on Source and then select Generate constructor using fields.
04:49 This constructor can initialize the values of name, age, empid and salary.
04:56 Let us now define the showDetails method. So, type: public void showDetails( ).
05:04 Inside this method, we need to print the employee details.
05:09 Type the following code as displayed on the screen.
05:13 Note that the error disappears, once the showDetails() method is implemented.
05:19 Next we will see a Student class of the project.
05:23 I have already created a subclass called Student.
05:28 There are two fields in the Student class – register number and grade which represent student register number and grade.
05:37 A constructor is also created inside this class.
05:42 This constructor can be used to initialize the values for name, age, register number and grade.
05:50 The showDetails method is also implemented in this class.
05:56 It prints the values of Student Register Number and grade.
06:00 Now note that the Employee class has its own implementation of showDetails().
06:08 And the Student class has its own implementation of showDetails().
06:14 Now right-click on the default package.
06:17 Click on New > Class and then type name as Demo.
06:23 Inside this class, we will have the main method.
06:27 So, type main and then press ctrl+space to generate the main method.
06:33 Now let us try to instantiate the Person class by typing Person p equals new Person.
06:42 Within brackets and double quotes type John and put a semicolon.
06:48 Now we can see an error. This is because the Person class is abstract and it cannot be instantiated.
06:58 Let's remove this line.
07:00 Type the following code as displayed on the screen.
07:04 Now, let us instantiate the Person class using the Employee class as Person p1 equals new Employee.
07:14 In the first line, we are passing the values of different arguments.
07:19 John is passed as Employee name.
07:22 40 as the value of age.
07:25 E267 as the value of Employee ID and 10000 as the value of Employee salary.
07:33 Now we can invoke the concrete method in the Person class as p1.showBasicDetails().
07:41 We can also call the showDetails() method using the object p1 as p1.showDetails().
07:50 Similarly instantiate the Person class using the Student class.
07:55 This is represented as Person p2 equals new Student.
08:01 Now we are passing the values of different arguments.
08:06 We can invoke the showBasicDetails() method and showDetails() method using the object as shown.
08:15 Now let us run this Demo program.
08:18 So, right-click on the class Demo and then select Run as > Java Application.
08:25 We can see the output with the basic employee details like name and age.
08:31 These are printed by showBasicDetails() method.
08:35 Other employee details like employee ID and salary are printed by the showDetails() method.
08:43 Similarly the basic student details like name and age are printed by showBasicDetails() method.
08:52 Other details of the student like Student register number and grade are printed by the showDetails() method.
09:01 This brings us to the end of this tutorial. Let us summarize.
09:07 In this tutorial, we have learnt about: Abstract Methods and Concrete Methods
09:14 Abstract Classes and Concrete Classes and How to create and use Abstract Classes.
09:21 As an assignment, create an abstract class Vehicle which contains an abstract method run().
09:29 Create asubclass Car which extends the Vehicle class and implements the run method that prints “Car is running on 4 wheels”.
09:39 Also create asubclass Bike which again extends the Vehicle class and implements the run method that prints “Bike is running on 2 wheels”.
09:50 Also create a Demo class containing the main method to verify the results.
09:56 The video at the following link summarizes the Spoken Tutorial Project. Please download and watch it.
10:03 The Spoken Tutorial Project Team: Conducts workshops using spoken tutorials and
10:09 Gives certificates on passing the online tests.
10:13 For more details, please write to us.
10:16 Spoken Tutorial Project is funded by NMEICT, MHRD, Government of India.
10:23 More information on this mission is available at the link shown.
10:28 This script has been contributed by Dept. of Information Technology, Amal Jyothi College of Engineering.
10:35 This is Priya from IIT Bombay, signing off. Thank you for joining.

Contributors and Content Editors

PoojaMoolya, Pratik kamble, Sandhya.np14