You are on page 1of 20

Encapsulation, ADT ,Inheritance and Polymorphism

Encapsulation
Combining of data and related operations is called encapsulation.

Class Syntax
["public"] ["abstract"|"final"]"class" Class_name ["extends" object_name] ["implements" interface_name] "{ " // properties declarations //behavior declarations }"

Sample code
public class Box { // what are the properties or fields private int length, width, height; // what are the actions or methods public void setLength(int p) {length=p;} public int getLength() {return length;} public void setWidth(int p) {width=p;} public void setHeight(int p) {height=p;} public void showVolume() {System.out.println(length*width*height);} }

Wrapper Classes
Java wrappers are classes that wrap up primitive values in classes that offer utility methods to manipulate the values. The wrapper classes also offer utility methods for converting to and from the int values they represent. Thus if you have a string that could also represent a number, you can use a wrapper class to perform that calculation. The methods of the wrapper classes are all static so you can use them without creating an instance of the matching wrapper class. Note that once a wrapper has a value assigned to it that value cannot be changed.

Wrapper Classes
public class String2Int { public static void main(String argv[]) { try { int i= Integer.parseInt(argv[0]); System.out.println("Coverted to int val = " + i); }catch(NumberFormatException nfe){ System.out.println("Could not covert to int"); } } }

Abstract Data Types (ADT)


An ADT is a data type defined only by the type of data stored and the operations that may be performed on concrete instances of the type. Developers access concrete instances of an ADT using only its operations, and they are unaware of how the operations are implemented within the internal structure of the data type.

Inheritance
Inheritance is the capability of a class to use the properties and methods of another class while adding its own functionality. The Object class is the highest superclass (ie. root class) of Java. All other classes are subclasses (children or descendants) inherited from the Object class.

Inheritance
The Object class includes methods such as: clone() equals() copy(Object src) finalize() getClass() hashCode() notify() notifyAll() toString() wait()

Inheritance
Java uses the extends keyword to set the relationship between a parent class and a child class. public class GraphicsBox extends Box

Inheritance
public class Animal { } public class Mammal extends Animal { } public class Reptile extends Animal { } public class Dog extends Mammal { }

Inheritance
Overriding means creating a new set of method statements for the same method signature (name, number of parameters and parame You cannot override final methods, methods in final classes, private methods or static methods).

Abstract Classes and methods


Often, the superclass will be set up as an abstract class which does not allow objects of its prototype to be created. In this case, only objects of the subclass are used. To do this the reserved word abstract is included in the class definition.

Abstract Classes and methods


Abstract methods are methods with no body specification. Subclasses must provide the method statements for their particular meaning. If the method was one provided by the superclass, it would require overriding in each subclass. And if one forgot to override, the applied method statements may be inappropriate.

Sample code
public abstract class Animal // class is abstract { private String name; public Animal(String nm) // constructor method { name=nm; } public String getName() // regular method { return (name); } public abstract void speak(); // abstract method }

Polymorphism
Polymorphism is the capability of an action or method to do different things based on the object that it is acting upon. This is the third basic principle of object oriented programming. Overloading, overriding and dynamic method binding are three types of polymorphism.

Questions
1. 2. 3. 4. 5. 6. 7. 8. How is a java program executed? What is JVM? What is JIT? What is java compiler? What is bytecode? How can we say that Java Program is portable? How we can say that Java Program is platform independent? What is JRE? Which version of JRE is used in the institutes laboratory? 9. What is JDK? Which version of JDK you are using in laboratories? 10. What are the features of object oriented programming language? Explain each of them.

Questions
11. 12. 13. 14. 15. 16. 17. 18. 19. 20. What are the advantages of using object-oriented concepts? Prove that Java is object-oriented language. What is encapsulation? How Java provides encapsulation? What is inheritance? How Java provides it? What is polymorphism? How Java provides it? What is information hiding? How that can be done using Java? What is the advantage of using Interface? What is the advantage of using Abstract class? How Interfaces and Abstract classes differ? What is a final class?

Questions
21. What is a final method? 22. What is the purpose of super()? How it is used during inheritance? 23. What is overriding? Explain with appropriate example. 24. What is exception handling? 25. What is the difference between errors and exception? 26. How can we create and use user-defined exceptions? 27. What is the purpose of throw and throws? 28. What is the purpose of finally block? 29. What are wrapper classes? 30. Differentiate: Concrete class and abstract class

Questions
31. 32. 33. 34. 35. 36. 37. 38. 39. 40. 41. 42. 43. 44. How instance variables differ from static(class ) variables? Can we override final methods? What is accessor method, mutator method and helper method? Write the hierarchy of Exception class? Why do we set PATH and CLASSPATH? Can we create final variable? If yes, how and what is its purpose? Write a Java code that shows usage of ternary operator. How can we use while and do.. while looping statements in Java? Explain the purpose of for..loop with proper syntax and example. Which data types are supported by Java? Write the memory space allotted to each data type and range of data that can be stored in it. What is a object initialization and object instantiation? What is constructor? What is Garbage Collection? What is the purpose of finalize() method?

You might also like