| OOPS In Java
Questions
Q) What is data hiding?
Ans) The way of providing restriction to the access of property of
a class is called data hiding. In java this is achieved by using access
modifiers: - public, private, protected and default.
Q) What is encapsulation?
Ans) The combination of abstraction and data hiding for a class is
called encapsulation.
Q) What is polymorphism?
Ans) Polymorphism gives us the ultimate flexibility in extensibility.
The abiltiy to define more than pne 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).
When you override methods, JVM determines the proper methods to call
at the program’s run time, not at the compile time. Overriding occurs when
a class method has the same name and signature as a method in parent class.
Overloading occurs when several methods have same names with
Overloading is determined at the compile time.
Different method signature and different number or type of parameters.
Same method signature but different number of parameters.
Same method signature and same number of parameters but of different
type
int add(int a,int b)
float add(float a,int b)
float add(int a ,float b)
void add(float a)
int add(int a)
void add(int a)
//error conflict with the method int add(int a)
Example: Overloading
Class BookDetails{
String title;
String publisher;
float price;
setBook(String title){
}
setBook(String title, String publisher){
}
setBook(String title, String publisher,float price){
}
}
Example: Overriding
class BookDetails{
String title;
setBook(String title){ }
}
class ScienceBook{
setBook(String
title){}
//overriding
setBook(String title, String publisher,float price){ } //overloading
}
Q) 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 int parentage;
public String familyName;
}
public class Child extends Parent{
public String childName;
public int childAge;
public void printMyName(){
System.out.println(“ My name is “+ chidName+” “ +familyName)
}
}
In above example the child has inherit its family name from the parent
class just by inheriting the class.
Q) What is multiple inheritance and does java support?
Ans) If a child class inherits the property from multiple classes is
known as multiple inheritance.
Java does not allow to extend multiple classes but to overcome this
problem it allows to implement multiple Interfaces.
Q) 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;
public String model;
}
Have Java Problem
Ask It in The Java
Forum
Java Books
Java Certification, Programming,
JavaBean and Object Oriented Reference Books
Return to : Java Programming Hints
and Tips
All the site contents are Copyright © www.sap-img.com
and the content authors. All rights reserved.
All product names are trademarks of their respective
companies.
The site www.sap-img.com is not affiliated with or endorsed
by any company listed at this site.
Every effort is made to ensure the content integrity.
Information used on this site is at your own risk.
The content on this site may not be reproduced
or redistributed without the express written permission of
www.sap-img.com or the content authors.
|