Method overriding, in object oriented programming, is a language feature that allows a subclass or child class to provide a specific implementation of a method that is already provided by one of its superclasses or parent classes.
OOPS Concept |
Q1) What is polymorphism? Ans) Polymorphism gives us the ultimate
flexibility in extensibility. The abiltiy to define more than one function
with the same name is called Polymorphism. In java,c++ there are two type
of polymorphism: compile time polymorphism (overloading) and runtime
polymorphism (overriding). Overloading occurs when several methods have same names with
Example of Overloading Example: Overloading Class
BookDetails{
Example: Overriding class
BookDetails{
class ScienceBook extends BookDetails{ setBook(String title){} //overriding
} |
Q2) What is inheritance? Ans) Inheritance is the property which allows a Child class to inherit some properties from its parent class. In Java this is achieved by using extends keyword. Only properties with access modifier public and protected can be accessed in child class. public class Parent{ public String parentName; public class Child extends Parent{ public String childName; public void printMyName(){ } In above example the child has inherit its family name from the parent class just by inheriting the class. |
Q3) What is multiple inheritance and does java support? Ans) If a child class inherits the property from multiple classes is
known as multiple inheritance. |
Q4) What is abstraction? Ans) Abstraction is way of converting real world objects in terms of class. For example creating a class Vehicle and injecting properties into it. E.g public class Vehicle { public String colour; |
Q5) What is encapsulation? Ans) The encapsulation is achieved by combining the methods and attribute into a class. The class acts like a container encapsulating the properties. The users are exposed mainly public methods.The idea behind is to hide how thinigs work and just exposing the requests a user can do. |
Q6) What is Association? Ans) Association is a relationship between two classes. In this relationship the object of one instance perform an action on behalf of the other class. The typical behaviour can be invoking the method of other class and using the member of the other class. public class MyMainClass{
public void init(){ new OtherClass.init(); }}
|
Q7) What is Aggregation? Ans) Aggregation has a relationship between two classes. In this relationship the object of one class is a member of the other class. Aggregation always insists for a direction. public class MyMainClass{
OtherClass otherClassObj = new OtherClass();}
|
Q8) What is Composition? Ans) Composition is a special type of aggregation relationship with a difference that its the compulsion for the OtherClassobject (in previous example) to exist for the existence of MyMainClass. |
原文:http://www.cnblogs.com/leetcode/p/3553558.html