filmov
tv
Java Tutorial - 4 | Edureka
Показать описание
Take instructor-led Live class on Java Tutorial at :
The following topics were covered in this Java Tutorial:
Abstract Class and Abstract methods, Creating Interface, Implementing Interface, Enums and their Usage, Example Questions.
Abstract Class:
Abstraction is nothing but hiding. That means you do not provide the detail. You don't get to know what is inside that but just have an overview on that and that's called abstraction (data hiding).
Real world example of abstraction is: you drive a car; you know that car moves on four wheels and many things. You don't know the details about how it works inside. You don't know the internal details of how the car will move left when you move your steering left.
In java you can create classes that provide overview but the definition is not present. Suppose you have class shape, you don't know have the details whether the shape is Triangle or Circle or Rectangle and so on. So making the class shape as abstract which gives overview that it is some kind of shape.
What is an Abstract Class?
An abstract class is a class preceded by "abstract" keywords. It may or may not use abstract methods.
What is Abstract method?
A method that is preceded by "abstract" keyword and without any implementation for it. In java we create an abstract class and put the details into that just the names i.e abstract methods. Finally what we do is create a class that extends abstract class and give the implementation for the details of abstract class methods.
What is the difference between inheritance and Abstract class?
Inheritance is one of the OOP's concepts that extend a class. Abstract means you are actually having an abstract class and an abstract method of that class. Inheritance do not tell you whether the class is abstract or not. Inheritance is an OOP's concept and Abstraction is another OOP's concept. In abstraction we use the concept of inheritance. Abstract methods will be present in interfaces and abstract class.
Interface:
Interfaces are declared using the "interface" keyword. You consider interface as a medium through which you can use certain methods which do not have an implementation.
Interface can contain method signature i.e abstract methods with no implementation and constant declarations i.e variable declarations that are declared have to be both static and final. We use "interface" keyword to create an interface. If a class want to use an interface then the class should use "implement" keyword to get the interface and implement all the abstract methods present in an interface compulsorily.
What is the difference between interface and abstract class?
An interface contains only abstract methods where as an abstract class contains both abstract methods and well-defined methods.
What is the advantage of an interface?
We know that java does not support multiple inheritance but it can be achieved with the help of interface. A class can extend only one class where as a class can implement any number of interfaces.
As we have only abstract methods in an interface we no need to declare those methods as abstract. The methods in an interface should not be an declared as final because it should be overridden in the class that implements this interface. But the variable defined in an interface should be final.
Does a class that implements an interface can have its own methods other than implementing abstract methods of the interface? - Yes.
Enums:
Enums is actually abbreviation for enumeration in java. Like there are constants like Monday to Sunday. In a week you have 7 days which will remain same from Monday till Sunday. So whenever you want to use these kinds of things in your program, you can insert them like manually Sunday, Monday, Tuesday ......Saturday. When we use these constants in this way there is a chance of getting an error and it is also not optimised. To avoid such type of issues we use enums in java. Other examples could be roll number of the students in the class.
Enumeration:
Enumerations helps to relate the variables with related constants so that it will be flexible to work. Whenever you are defining enumeration you use "enum" keyword. An application of enums is that it can be used in drop down boxes.
The following topics were covered in this Java Tutorial:
Abstract Class and Abstract methods, Creating Interface, Implementing Interface, Enums and their Usage, Example Questions.
Abstract Class:
Abstraction is nothing but hiding. That means you do not provide the detail. You don't get to know what is inside that but just have an overview on that and that's called abstraction (data hiding).
Real world example of abstraction is: you drive a car; you know that car moves on four wheels and many things. You don't know the details about how it works inside. You don't know the internal details of how the car will move left when you move your steering left.
In java you can create classes that provide overview but the definition is not present. Suppose you have class shape, you don't know have the details whether the shape is Triangle or Circle or Rectangle and so on. So making the class shape as abstract which gives overview that it is some kind of shape.
What is an Abstract Class?
An abstract class is a class preceded by "abstract" keywords. It may or may not use abstract methods.
What is Abstract method?
A method that is preceded by "abstract" keyword and without any implementation for it. In java we create an abstract class and put the details into that just the names i.e abstract methods. Finally what we do is create a class that extends abstract class and give the implementation for the details of abstract class methods.
What is the difference between inheritance and Abstract class?
Inheritance is one of the OOP's concepts that extend a class. Abstract means you are actually having an abstract class and an abstract method of that class. Inheritance do not tell you whether the class is abstract or not. Inheritance is an OOP's concept and Abstraction is another OOP's concept. In abstraction we use the concept of inheritance. Abstract methods will be present in interfaces and abstract class.
Interface:
Interfaces are declared using the "interface" keyword. You consider interface as a medium through which you can use certain methods which do not have an implementation.
Interface can contain method signature i.e abstract methods with no implementation and constant declarations i.e variable declarations that are declared have to be both static and final. We use "interface" keyword to create an interface. If a class want to use an interface then the class should use "implement" keyword to get the interface and implement all the abstract methods present in an interface compulsorily.
What is the difference between interface and abstract class?
An interface contains only abstract methods where as an abstract class contains both abstract methods and well-defined methods.
What is the advantage of an interface?
We know that java does not support multiple inheritance but it can be achieved with the help of interface. A class can extend only one class where as a class can implement any number of interfaces.
As we have only abstract methods in an interface we no need to declare those methods as abstract. The methods in an interface should not be an declared as final because it should be overridden in the class that implements this interface. But the variable defined in an interface should be final.
Does a class that implements an interface can have its own methods other than implementing abstract methods of the interface? - Yes.
Enums:
Enums is actually abbreviation for enumeration in java. Like there are constants like Monday to Sunday. In a week you have 7 days which will remain same from Monday till Sunday. So whenever you want to use these kinds of things in your program, you can insert them like manually Sunday, Monday, Tuesday ......Saturday. When we use these constants in this way there is a chance of getting an error and it is also not optimised. To avoid such type of issues we use enums in java. Other examples could be roll number of the students in the class.
Enumeration:
Enumerations helps to relate the variables with related constants so that it will be flexible to work. Whenever you are defining enumeration you use "enum" keyword. An application of enums is that it can be used in drop down boxes.