Java/C3/Abstract-Classes/English-timed

From Script | Spoken-Tutorial
Revision as of 14:10, 12 August 2016 by PoojaMoolya (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search
Time
Narration
00:01 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:15 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. 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. There should not be opening and closing parenthesis for this method.
01:04 The method showDetails() illustrated here, is an example of abstract method.
01:10 Note that this method is declared using the abstract keyword, with no current implementation.
01:18 Next we will see about concrete method.
01:21 A Concrete method is completely implemented within the curly brackets.
01:27 This method showBasicDetails() illustrated here is an example of concrete method.
01:35 Observe that this method is implemented completely.
01:39 Now we will learn about abstract class with an example.
01:44 An abstract class usually contains at least one abstract method.
01:49 Abstract methods can be added only to abstract classes.
01:54 The class Person illustrated here is an abstract class.
01:59 It contains an abstract method called showDetails().
02:04 It can also contain variables and other concrete methods.
02:09 The figure here represents an inheritance relation. Here, the Person class is an abstract class.
02:18 The Employee class and the Student class are subclasses of the Person class.
02:24 These subclasses can provide their own different implementations.
02:29 These are done by showDetails( ) method present in the Person class.
02:35 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.
02:49 A class is said to be a concrete class, if all the methods in that class are concrete methods.
02:57 The class Employee illustrated here, is a concrete class. All the methods inside this class including showDetails() are concrete.
03:07 Let us understand the usage of Abstract class with a sample program.
03:12 Now we will switch to Eclipse and create a new project called AbstractDemo.
03:19 Inside this project, we will create the necessary classes to demonstrate the usage of Abstract class.
03:27 Now, right click on src folder and click new-> class.
03:33 Type the name of the class as Person and press Enter.
03:38 Now we will add the fields to represent the name and age of the Person.
03:44 Type String name semicolon. Also type int age semicolon.
03:51 Now click on source -> and select generate constructor using fields.
03:58 Delete the super keyword from the generated code.
04:02 The constructor can initialise the values of name and age fields.
04:08 We will add a concrete method to this class to print the name and age.
04:14 So type public void showBasicDetails( ) within brackets type, System.out.println within round brackets and within quotes type Name colon plus name semicolon
04:33 Also type System.out.println within round brackets and within quotes Age colon plus age semicolon
04:46 Now we will add an abstract method to this class. So type public void showDetails( ) semicolon
04:56 An error comes up, since we have not yet added the abstract keyword.
05:01 So now, add the keyword abstract.
05:04 Now we can see, another error comes up. This is because, abstract methods can be added only to abstract classes.
05:13 So now, add the keyword abstract to the Person class to make it an abstract class.
05:20 Then right click on the default package and create another class called Employee.
05:26 Now to make this a subclass of Person class, type extends Person.
05:34 Now we can see an error comes up in the Eclipse IDE.
05:38 It indicates that, we should provide an implementation to the abstract method showDetails( ).

We will do it a little later.

05:48 Now create two fields to represent the employee id and employee salary.
05:54 So type String empid semicolon and int salary semicolon
06:01 Now click on source-> and then select generate constructor using fields.
06:08 This constructor can initialise the values of name, age, empid and salary.
06:15 Let us now define the showDetails method. So type public void showDetails( )
06:23 Inside this method, we need to print the employee details.
06:27 So type System.out.println within quotes Emp id colon plus empid semicolon
06:38 System.out.println within quotes Salary colon plus salary semicolon
06:47 Note that the error disappears, once the showDetails() method is implemented.
06:54 Next we will see a Student class of the project.
06:57 I have already created a subclass called Student.
07:02 There are two fields in the Student class, regno (register number) and grade which represent student reg. no (register number) and grade.
07:11 A constructor is also created inside this class. This constructor can be used to initialize the values for name, age, register number and grade.
07:24 The showDetails method is also implemented in this class. It prints the values of Student Reg. Number and grade
07:34 Now note that the Employee class has its own implementation of showDetails().
07:42 And the Student class has its own implementation of showDetails().
07:48 Now right click on the default package. Click on new-> class and then type name as Demo.
07:57 Inside this class, we will have the main method, So type main and then press Ctrl space to generate the main method.
08:08 Now let us try to instantiate the Person class by typing Person p equals new Person.
08:16 Within brackets and double quotes, type John and put a semicolon.
08:23 Now we can see an error. This is because the Person class is abstract and it cannot be instantiated. Let's remove this line.
08:34 Type the following code as displayed on the screen.
08:38 Now let us instantiate the Person class using the Employee class as Person p1 equals new Employee.
08:48 In the first line, we are passing the values of different arguments.
  • John is passed as Employee name,
08:57 40 as the value of age. E267 as the value of Employee ID and
09:04 10000 as the value of Employee salary.
09:07 Now we can invoke the concrete method in the Person class as p1.showBasicDetails()
09:16 We can also call the showDetails() method using the object p1 as p1.showDetails()
09:24 Similarly, instantiate the Person class using the Student class. This is represented as Person p2 equals new Student.
09:35 Now we are passing the values of different arguments.

Hari is passed as the value of student name,

09:44 20 as the value of age.
09:47 12005 as the value of student Reg No. and A as the value of grade.
09:55 Now we can call the showBasicDetails() method using the object p2 as p2.showBasicDetails()
10:04 We can also invoke the showDetails() method using the object p2 as p2.showDetails()
10:13 Now let us run this Demo program. So right click on the class Demo and then select Run as -> Java Application
10:23 We can see the output with the basic employee details like name and age.
10:29 These are printed by showBasicDetails() method.
10:33 Other Employee details like employee ID and salary are printed by the showDetails() method.
10:41 Similarly, the basic student details like name and age are printed by showBasicDetails() method.
10:50 Other details of the student like Student register no and grade are printed by the showDetails() method.
10:59 This brings us to the end of this tutorial. Let us summarize.
11:05 In this tutorial we have learnt about
  • Abstract Methods and Concrete Methods
  • Abstract Classes and Concrete Classes and
  • How to create and use Abstract Classes
11:19 As an assignment- Create an abstract class Vehicle which contains an abstract method run()
11:27 Create a subclass Car which extends the Vehicle class and implements the run method that prints “Car is running on 4 wheels”
11:37 Also create asubclass Bike which again extends the Vehicle class and implements the run method that prints “Bike is running on 2 wheels”
11:48 Also create a Demo class containing the main method to verify the results.
11:54 The video at the following link summarizes the Spoken Tutorial Project. Please download and watch it.
12:02 The Spoken Tutorial Project Team
  • Conducts workshops using spoken tutorials
  • Gives certificates on passing the online tests
12:11 For more details, please write to us.
12:15 Spoken Tutorial Project is funded by NMEICT, MHRD, Government of India.

More information on this Mission is available at the link shown.

12:26 This script has been contributed by: Dept. of Information Technology, Amal Jyothi College of Engineering.


This is Priya from IIT Bombay signing off. Thank you for joining.

Contributors and Content Editors

PoojaMoolya, Pratik kamble, Sandhya.np14