Day 04:- Object Oriented Programming Continue

DAY 04- Object Oriented Programming Continue

========================================================================
TOPICS

  • Dynamic Binding
  • Abstract Classes
  • Interfaces In Java till Java-7
  • Final Keyword
  • Interface in Java with Java-8
  • Packages
  • Exception handling
========================================================================

DYNAMIC BINDING

The reference of child class object can be hold in the reference variable of parent class this is known as a Dynamic binding.

When  type of a object is determined at compile level then it is known as compile time binding or static binding.

However when type of the object is determined at run-time, it is known as dynamic binding.

Lets understand with the help of example having a child class and a parent class.


When compiler is not able to resolve the call/binding at compile time, such binding is known as Dynamic or late Binding. Overriding is a perfect example of dynamic binding as in overriding both parent and child classes have same method. Thus while calling the overridden method, the compiler gets confused between parent and child class method.

Now lets understand how it works, look show() method is actually called by parent class but since this method is overridden thus JVM run the overridden method  i.e. child class method.

Because if reference of child class object is hold by parent class then only methods which are present in parent class can only be run, with the help of parent we can't call method written in child class except overridden methods.



ABSTRACT CLASS

A class that is declared with abstract keyword, is known as abstract class in java. It can have abstract and non-abstract methods (method with body).

Before learning java abstract class, let's understand the abstraction in java first.

Abstraction is a process of hiding the implementation details and showing only functionality to the user.

Examples :-

A.  Bank ATM Screens (Hiding the internal implementation and highlighting set of services like withdraw, money transfer, mobile registration).


B. Mobile phones (The mobile persons are hiding the internal circuit implementation and highlighting touch screen).

C. Syllabus copy (the institutions persons just highlighting the set of contents that persons provided the persons are not highlighting the whole content).


In Java Abstraction can be achieved via 2 ways.

1. Abstract class
2. Interface 

Lets first discussed about the abstract class then will cover the interface part.

Note:- 
  • Abstract classes are declared with abstract keyword.
  • Abstract class may or may not be contain abstract method.
  • Abstract methods do not contain body.
  • If we declare any method as Abstract, then the class must be declared as abstract.
  • child class is responsible to provide the implementation for parent class abstract method.



Now make the class as abstract to remove the error.



let see we can have non abstract methods as well in abstract class


  • Abstract classes can’t be instantiated, because it contains unimplemented methods.


  • Abstract class can have a constructor.

  • To use an abstract class, we must inherit this class from another normal class.
  • The subclass of abstract class in java must implement all the abstract methods unless the subclass is also an abstract class.


Now let implement the abstract method and see that error will remove.


  • We can’t create a static method as abstract.
  • Abstract method can’t be private.
  • Data members can't be abstract.

Note:- Since abstract classes may or may not contain abstract methods that is the reason abstract class is used to achieve partial abstraction.
INTERFACE IN JAVA

Lets understand why we need interfaces instead of abstract class ?

Since java does not support multiple inheritance we need to go for interface.

Interface in java is like a blue print, it means in interface we can only declare methods we can't define them.

Note:- All methods present in interface are by default abstract and we know that abstract can't have definition, thus we need a class to provide the definition of interface methods.

Note:- To inherit an interface java provided us a keyword known as implement, just like we are implementing the blueprints.

lets take an example.



now create a class to implement the methods of Interface01.



As you can see when we try to implement interface it is a restriction for the class to provide implementation for all the methods present in the class.




As we can see we can call show() method with the help of child class as well as interface reference variable.

This is how a class implements an interface. It has to provide the body of all the methods that are declared in interface.

Note: class implements interface but an interface extends another interface and we can achieve multiple inheritance using interface.

lets take an example




Now let see why java allows us to use multiple inheritance.

Because parent interface does't have any definition, so there is no chance for ambiguity or diamond problem.


So basically there are two benefits of interfaces.

1. It allows us to use multiple inheritance.
2. To achieve abstraction.

Note: 
  • Always remember a interface can't implement another interface.
  • Interface methods are always public.
  • Interface methods are by default abstract. 
  • Interface does not allowed constructor. 
  • When a class implements an interface user should define all the methods of class.
  • When a class implements an interface all the data members are available for class. 
  • The java compiler adds public and abstract keywords before the interface method. More, it adds public, static and final keywords before data members.
  • Interface does not contain main method because main is a static method and static method can't be abstract. 

To understand the data members in Interface lets first take a look for final keyword.


FINAL KEYWORD

Final keyword can be used with Classes, Methods, Variables.

Final keyword is basically use to provide restrictions.



 lets take an example of final variable.



DATA MEMBERS IN INTERFACES

All the data members in interfaces are by default public static and final.

It means the value of data member set in interface can't be change.



Also since variables are final we can't change the value of these variables.

INTERFACE IN JAVA 8

Till java 7 we are not allowed to provide definitions for method which we create in interface, since all the methods are abstract however in java 8 we can provide the definitions for the methods via two ways.

In Java 8 interface changes include static methods and default methods  that means we can create and provide definitions for the methods if they are either static or default.

lets have a look to below mention example for static and default method.


Note:- 

If a class implements two interfaces and both interfaces are having default method then we compiler won't allow us to do the same.

However if a class implements two interfaces and both interfaces are having static method then there would be no compilation error because in this case method can only be accessed with the help of class name.

========================================================================

Links for the files (.Java File)

https://drive.google.com/drive/folders/0B5M4BcbfVN07OEN3UVlUYkRHOTg

Links for recorded Video

https://www.youtube.com/watch?v=wvydHAyFoyc&feature=youtu.be


========================================================================



  1. Unknown said...

    Appreciate you sharing, great article.Much thanks again. Really Cool.
    . net online training
    dot net online training hydarabad

Post a Comment