|Language:||English, Spanish, French|
|Distribution:||Free* [*Sign up for free]|
Java Language. 2. Object-Oriented Programming. 3. Inheritance and Polymorphism. 4. Static Members. 5. Interfaces and Abstract Classes. 6. Exceptions and. This PDF edition of the book is available under a Creative Commons . Object Orientation (OO) is a fundamental problem solving and design. Inheritance. • Polymorphism and dynamic binding. • Java an example of an object-oriented programming language. ▫ Program example. ▫ History of Java.
Inheritance Inheritance is the object oriented programming concept where an object is based on another object. Inheritance is the mechanism of code reuse. The object that is getting inherited is called superclass and the object that inherits the superclass is called subclass. We use extends keyword in java to implement inheritance.
Below is a simple example of inheritance in java. Association defines the multiplicity between objects. For example Teacher and Student objects.
There is one to many relationship between a teacher and students. Similarly a student can have one to many relationship with teacher objects. However both student and teacher objects are independent of each other.
Aggregation Aggregation is a special type of association.
In aggregation, objects have their own life cycle but there is an ownership. Composition Composition is a special case of aggregation. The composition represents the relationship where one object contains other objects as a part of its state. There is a strong relationship between the containing object and the dependent object.
It is the state where containing objects do not have an independent existence.
If you delete the parent object, all the child objects will be deleted automatically. Advantage of OOPs over Procedure-oriented programming language 1 OOPs makes development and maintenance easier, whereas, in a procedure-oriented programming language, it is not easy to manage if code grows as project size increases.
What are the six ways to use this keyword?
Why is multiple inheritance not supported in Java? Why use aggregation? Can we override the static method? What is the covariant return type? What are the three usages of Java super keyword? Why use instance initializer block? What is the usage of a blank final variable? What is a marker or tagged interface? What is runtime polymorphism or dynamic method dispatch? What is the difference between static and dynamic binding? How downcasting is possible in Java? What is the purpose of a private constructor?
But suppose, someone else is driving a super car, where it increment by 30kms or 50kms. Again the logic varies. Similarly for applybrakes, where one person may have powerful brakes, other may not. After that, I will create a child class which implements this interface, where the definition to all these method varies. SpeedUp ; A6.
Implementing an interface allows a class to become more formal about the behavior it promises to provide.
So I hope you guys are clear with the interface and how you can achieve abstraction using it. Finally, the last Object oriented programming concept is Polymorphism.
It is the ability of a variable, function or object to take on multiple forms. In other words, polymorphism allows you define one interface or method and have multiple implementations. Fast bowlers, Medium pace bowlers and spinners.
As you can see in the above figure, there is a parent class- BowlerClass and it has three child classes: FastPacer, MediumPacer and Spinner.
Bowler class has bowlingMethod where all the child classes are inheriting this method. As we all know that a fast bowler will going to bowl differently as compared to medium pacer and spinner in terms of bowling speed, long run up and way of bowling, etc.
And same happens with spinner class. The point of above discussion is simply that a same name tends to multiple forms. All the three classes above inherited the bowlingMethod but their implementation is totally different from one another.
In this, a reference variable is used to call an overridden method of a superclass at run time.
Method overriding is an example of run time polymorphism. Method overloading is an example of compile time polymorphism. Method Overloading is a feature that allows a class to have two or more methods having the same name but the arguments passed to the methods are different.