Java Week One
Polymorphism
- This is one of the main pillars of OOPs
- Poly (many) + morph (shape)
- in many shapes/forms
There are two type of polymorphism
- Static / compile time
- Method overloading - static
- Multiple methods, same name but different parameters
- within a class or between parent and child classes
- It is all about adding behavior to a method
Conditions
- Different number of parameters
- Different type of parameters
- Different order of parameters
- Same return types
- Method overloading - static
- Dynamic / runtime
- Method overriding - dynamic
- Used to provide a specific implementation of a method that is already implemented by the parent class
- It is used for runtime polymorphism
- It changes the behavior
- multiple methods with same names and same parameters
- It only takes place btw child and parent class but not within a class.
- Method overriding - dynamic
Key differences btw method overloading and overriding
| Overloading | Overriding |
|---------------------------|---------------------------|
| Adding behavior | Changing behavior |
| Same name different parameters| Same name Same parameters|
| Can be done within the same class | Can be done btw parent and child class only|
Final
- Key word used to restrict the user actions in :
- variables
- methods
- classes
- The Final Key word in Variable makes it a constant and it cannot be changed
- When used in a parent class method, No child class can override the method.
- When usd with a class, no other class can inherit from the class
Abstraction
Abstraction is a process of hiding the implementation details and showing only functionality to the user in another word, it show only essential things to the user and hide the internal details
Abstract class and method
- a class which declared with abstract keyword is know as abstract class
- abstract class can contain abstract as well non abstract methods
- abstract class consider as incomplete hence we cannot able to create an instance of the abstract class
- abstract class always be a super class
- the class derived from the abstract class is responsible for providing the implementation of all abstract method
- a non abstract class derived from the abstract class must provide the implementation of all abstract method
Abstract method
- abstract method are declared using abstract keyword
- it cannot have a body or implementation and if we try to do so it will be a compile time error
- only abstract class can contain abstract method
Rectangle, Circle, Cone, Triangle
width, height, radius, pie
Exercise:
- Create an abstract class ‘Parent’ with a method ‘message’. It has two subclasses each having a method with the same name ‘message’ that prints “This is first subclass” and “This is second subclass” respectively. Call the methods ‘message’ by creating an object for each subclass.
2.Create an abstract class ‘Bank’ with an abstract method ‘getBalance’. $100, $150 and $200 are deposited in banks A, B and C respectively. ‘BankA’, ‘BankB’ and ‘BankC’ are subclasses of class ‘Bank’, each having a method named ‘getBalance’. Call this method by creating an object of each of the three classes.
3.We have to calculate the percentage of marks obtained in three subjects (each out of 100) by student A and in four subjects (each out of 100) by student B. Create an abstract class ‘Marks’ with an abstract method ‘getPercentage’. It is inherited by two other classes ‘A’ and ‘B’ each having a method with the same name which returns the percentage of the students. The constructor of student A takes the marks in three subjects as its parameters and the marks in four subjects as its parameters for student B. Create an object for eac of the two classes and print the percentage of marks for both the students.
Interface
- it is a kind of contract of contract which needs to be fullfil by a class
- an interface in java is a blue print of a class
- it can have static constants and abstract method
- it is a compile time error to provide an implementation for any interface members
- if a class inherit from an interface, it must provide an implementation for all interface members
- interface can also inherit from another interface. A class that inherit from this interface must provide the implementation for all interface member in the entire interface chain
- we can not create an instance of the interface
- we can hold the reference of a class that has implemented interface in interface instance
Interview Question
Interface vs Abstract class
- interface is used to achieve 100% abstraction, where as abstract classes are used for partial abstraction
- interface can be use for multiple inheritance, where abstract class is a class which means cannot be inherited from more then one class
Functional Interface
- a interface with a default and single abstract method are called function interface
Java 8, we can have default and static methods in an interface Java 9, we have private method in an interface
note: by default, interface field are public, static and final and the method is public and abstract