Professional Documents
Culture Documents
1. Data hiding
2. Abstraction
3. Encapsulation
4. Tightly encapsulated class
5. IS-A relationship
6. Has-A relationship
7. Method signature
8. Overloading
9. Overriding
Data Hiding:
1. Outside person can’t access our internal data directly or our internal
data should not go out directly. This is OOP feature is nothing but
Data hiding.
2. After validation or identification outside person can access our
internal data.
Example:
1. After providing proper username and password, we can able to access our
G-mail inbox information.
2. Even though we are valid customer of the bank, we can able to access
our account information and we can’t access other’s account
information.
3. By declaring data member (variable only) as private we can achieve data
hiding.
package pack6;
Abstraction:
1. Hiding internal implementation and just highlight the set of services
what are offering is the concept of abstraction.
Example: Through Bank ATM GUI screen, bank people are highlighting the set of
service what they are offering without highlighting internal implementation.
Withdraw Deposit
Encapsulation:
1. The process of binding data and corresponding methods into a single
unit is nothing but encapsulation.
Example:
Class students {
Capsule
package pack6;
Note:
The main advantage of encapsulation is, we can achieve security but the main
disadvantage of the encapsulation is, it increases length of the code and
slows down execution.
package pack6;
}
Which of the following classes are tightly encapsulated?
package pack1;
public class A{
private int x = 10;
private String name = "Lalit";
}
package pack1;
int y = 20;
}
package pack1;
package pack1;
Example:
Case1:
package pack1;
public class A{
package pack1;
package pack1;
public class C {
public static void main(String[] args) {
A a = new A();
a.m1();
a.m2();----------------------------------Error
}
}
Compile time error: The method m2() is undefined for the type A at
pack1.C.main/ cannot find symbol: method m2(), location: class A
Case1:
package pack1;
public class A{
package pack1;
package pack1;
public class C {
public static void main(String[] args) {
B b = new B();
b.m1();--------------------------------------Parent
b.m2();--------------------------------------Child
}
}
Case1:
package pack1;
public class A{
package pack1;
public class C {
public static void main(String[] args) {
A a = new B();
a.m1();
a.m2();--------------------------------Error
}
}
Compile time error: The method m2() is undefined for the type A at
pack1.C.main/ cannot find symbol: method m2(), location: class A
Conclusion:
1. Whatever methods parent has by default available to the child and
hence on the child reference we can call both parent and child
class methods.
2. Whatever methods child has by default not available to the parent
class and hence on the parent reference we can’t call child
specific methods.
3. Parent reference can be used to hold child object but using that
reference we can’t call child specific methods. But we can call
methods present in parent class.
4. Parent reference can be used to hold child object but child
reference can’t be used to hold parent object.
Note:
1. The most common methods which are applicable for any type child,
we have to define in parent class.
2. The specific which are applicable for a particular child, we have
to define in child class.
The most common methods which are applicable for any java
object are defined in Object Class and hence every class in
java either child class of the object class either directly or
indirectly, so that object class methods by default available
to every class java without re-writing. Due to this object
class access is root for all java classes.
Note:
If our class doesn’t extend any other class then only our
class is direct child class of object class.
Class A {
Class A extends B {
P1 class------m1() method.
P2 class------m1() method.
package pack1;
interface A{
package pack1;
interface B {
package pack1;
@Override
public void m1() {
// TODO Auto-generated method stub
Note:
Cyclic inheritance:
Cyclic inheritance is not allowed in java, off course it is not required.
Example1:
Class A extends A {
}
Example2:
Class A extends B {
Class B extends A {
Example:
Class Engine {
Class Car {
Composition Aggregation
Example: Example:
University Department
Contained objects
Container object Contained object Container object
Note:
Example:
Example:
package pack1;
4. Within a class two methods with the same signature are not allowed,
even return types are different.
package pack1;
package pack1;
3. But in java we can declare multiple methods with the same but different
argument types. Such types of methods are called overloaded methods.
package pack1;
First it will promote argument to the next level and checked whether matched
method is available or not.
If the matched method is not available then compiler promotes arguments once
again to the next level. This process will be continued until all possible
promotions.
Still if the matched method is not available then we will get compile time
error. The following are all possible promotions in overloading.
Byteshortintlongfloatdouble
Charintlongfloatdouble
package pack1;
package pack1;
}
Case2:
Case3:
Object String
StringBuffer
package pack1;
}
Compile time error: reference to m1( ) is ambiguous
package pack1;
}
}
Case3:
package pack1;
}
}
package pack1;
}
}
Case5:
package pack1;
}
}
Case4:
package pack1;
}
package pack1;
package pack1;
}
}
Overriding:
1. Whatever methods parent has by default available to the child
through inheritance. If child class not satisfied with parent
class implementation then child is allowed to redefine that
method based on its requirement. This process is called
overriding.
2. The parent class method which is overridden is called overridden
method and child class method which is overriding is called
overriding method.
package pack6;
package pack6;
}
}
package pack6;
package pack6;
package pack6;
Object o1 = c.m1();
System.out.println(o1);--------------------- Lalit
But String to Object is not allowed and int to double is not allowed.
Parent class private method not available to the child and hence
overriding concept not applicable for private methods.
package pack6;
package pack6;
}
}
package pack6;
}
}
package pack6;
package pack6;
package pack6;
public class Child extends Parent {
@Override
public void m1() {
package pack6;
}
}
package pack6;
@Override
public abstract void m1();
package pack6;
@Override
public void m1() {
// TODO Auto-generated method stub
The main advantage of this approach is, we can stop the availability
of parent method implementation to the next level child classes.
In overriding the following modifiers won’t keep any restriction:
Synchronize non-synchronize
Native non-native
Strictfp non-strictfp
Non-final final
Abstract non-abstract
package pack6;
}
}
package pack6;
@Override
abstract void m1();
void m1() {
}
}
package pack6;
@Override
public void m1() {
Public public
Protected protected, public
Default default, protected, public
Private Overriding concept not allowed
Example1:
package pack6;
import java.io.IOException;
package pack6;
import java.io.*;
@Override
public void m1() throws EOFException,InterruptedException {--Error
package pack6;
import java.io.IOException;
}
package pack6;
import java.io.*;
@Override
public void m1() throws EOFException, InterruptedException {
Exapmple2:
package pack6;
package pack6;
@Override
public void m1() {
}
public static void main(String[] args) {
Example3:
package pack6;
package pack6;
@Override
public void m1() throws Exception {-----Error
}
public static void main(String[] args) {
package pack6;
package pack6;
@Override
public void m1() throws Exception {
}
public static void main(String[] args) {
Example4:
package pack6;
package pack6;
import java.io.IOException;
@Override
public void m1() throws IOException {
}
public static void main(String[] args) {
Example5:
package pack6;
import java.io.IOException;
package pack6;
@Override
public void m1() throws Exception {
}
public static void main(String[] args) {
package pack6;
import java.io.IOException;
package pack6;
@Override
public void m1() throws Exception {
}
public static void main(String[] args) {
Example6:
package pack6;
import java.io.IOException;
package pack6;
import java.io.EOFException;
import java.io.FileNotFoundException;
@Override
public void m1() throws FileNotFoundException,EOFException {
}
public static void main(String[] args) {
Exaple7:
package pack6;
import java.io.IOException;
package pack6;
@Override
public void m1() throws
ArithmeticException,NullPointerException,ClassCastException {
}
public static void main(String[] args) {
package pack6;
package pack6;
@Override
public void m1() {--------Error
}
public static void main(String[] args) {
package pack6;
}
package pack6;
@Override
public static void m1() {-----Error
}
public static void main(String[] args) {
package pack6;
}
}
package pack6;
}
public static void main(String[] args) {
}
Method hiding:
All rules of method hiding are exactly same as overriding
except the following differences.
package pack6;
package pack6;
package pack6;
package pack6;
package pack6;
public class Child extends Parent {
@Override
public void m1() {
System.out.println("Child");
}
package pack6;
package pack6;
package pack6;
package pack6;
@Override
public void m1(int... x) {
System.out.println("Child");
}
package pack6;
package pack6;
package pack6;
Note:
Modifier
Polymorphism:
1. One name but multiple forms are the concept of polymorphism.
Example1:
Method name is the same but we can apply for different types of arguments.
This concept is called overloading.
Example2:
Method signature is same but in parent class one type of implementation and
in the child class another type of implementation. This concept is called
overriding.
package pack6;
package pack6;
@Override
public void m1() {
System.out.println("Bizlain");
}
public static void main(String[] args) {
Example3:
ArrayList
LinkedList
Parent class can be used to hold child class object but by using
that reference we can call only the methods available in parent
class and we can’t child specific methods.
But by using child reference we can call both parent and child class methods.
The first element present in the ArrayList can be any type. It may be student
object or customer object or string object or stringBuffer object. Hence the
return type of get method is object which can hold any object.
Object o = l.get(0);
Child c = new Child(); Parent p = new child();
Coupling:
1. The degree of dependency between the components is called
coupling.
2. If dependency is more then it is considered as tightly coupling
and if dependency is less then it is considered as loosely
coupling.
Example:
package pack4;
public class D {
public static int m1() {
return 10;
}
}
package pack4;
public class C {
static int k = D.m1();
}
package pack4;
public class B {
static int j = C.k;
}
package pack4;
public class A {
static int i = B.j;
}
Cohesion:
1. For every component a clear well defined functionality is define then
that component is said to be follow high-cohesion.
Example:
Low-cohesion:
Total servlet
Validation
Index page
Reply page
Compose page
Error page
High-cohesion:
Error.jsp compose.jsp
A a = (C) d;
The type of ‘d’ and ‘C’ must have some relation either child to parent or
parent to child or same type otherwise we will get compile time error saying
inconvertible, found: d, required: C.
Example:
Example:
Run time object type of ‘d’ must be either same or derived type of ‘C’,
otherwise we will get run time exception saying class cast exception:
java.lang.Strong cannot be cast to java.lang.StringBuffer.
Example:
Strictly speaking through type casting we are not creating any new
object.
For the exiting object we are providing another type of reference
variable i.e we are perfoming type casting (conversion) but not object
casting.
Example:
Integer i
Number n 10
Object o
package pack2;
public class C {
public static void main(String[] args) {
Integer i = new Integer(10);
Number n = (Number) i;
Object o = (Object) n;
System.out.println(i==n);--------true
System.out.println(n==o);--------true
System.out.println(i==o);--------true
}
}
Note:
C B A
C c = new C();
package pack6;
public class Parent {
package pack6;
}
}
Parent reference can be used to hold child object but by using that reference
we can’t call child specific methods and we can call only the methods
available in parent class.
Example1:
package pack6;
package pack6;
public class Child extends Parent {
@Override
public void m1() {
System.out.println("bizlain");
}
package pack6;
((Child)c).m1();----------lalli
((Parent)((Child)c)).m1();---------lalli
}
}
Example2:
package pack6;
}
package pack6;
package pack6;
((Child)c).m1();-------------bizlain
((Parent)((Child)c)).m1();--------------lalit
}
}
In this example we are using static keyword for every same signature
method. Here it is method hiding not method overriding. In method
hiding, method resolution is always based on reference type.
Example3:
package pack6;
package pack6;
public class Child extends Parent {
int x = 888;
}
package pack6;
int x = 777;
System.out.println(((Child)c).x);---------888
System.out.println(((Parent)((Child)c)).x);--------999
}
}
Example:
package pack5;
2 static {
m1(); 8
m1(); 13
System.out.println("Main method"); 15
}
System.out.println(j); 9 14
}
5 static {
}
I = 0; [read indirect and write only at step 1st]
J = 0; [read indirect and write only at step 6th]
I = 10; [read and write at step 7th]
J = 20; [read and write at step 12th]
Output:
0
First static block
Second static block
20
Main method
Example:
package pack5;
static {
m1();
System.out.println(i);------direct read
System.out.println("main");
}
Output:
0
10
20
Main
package pack5;
static {
System.out.println(i);
}
}
package pack5;
public class Base {
static {
System.out.println(i);
}
package pack5;
static {
m1();
}
Example1:
package pack5;
static {
}
}
Example2:
package pack5;
static {
}
}
Note:
Within a class we can declare any number of static blocks but all
these static will be executed from top to bottom.
Q; Without writing main method is it possible to print some statements
to the consol?
package pack5;
static {
}
} java 1.6
package pack5;
}
}
To be continued
Constructors:
1. Once we creates an object, compulsory we should perform initialization
then only the object is in a position to respond properly.
2. Whenever we are creating an object some piece of the code will be
executed automatically to perform initialization. This piece is nothing
but constructor. Hence the main purpose of constructor is to perform
initialization of an object.
Example: if we want to create 100 Student object with their name and roll
number.
package pack5;
String name;
int rollNo;
this.rollNo = rollNo;
System.out.println(this.rollNo);
}
2. Both constructor and instance block have their own different purposes
and replacing one concept with another concept may not work always.
3. Both constructor and instance block will be executed for every object
creation but instance block first followed by constructor.
package pack5;
Test(){
Test(int x){
Test(double d){
count);------13
}
package pack5;
Test(){
System.out.println("constructor");
}
void Test() {
System.out.println("method but not constructor");
}
static Test(){--------------------------Error
System.out.println("constructor");
}
Default constructor:
1. Compiler is responsible to generate default constructor (but not JVM).
2. If we are not writing any constructor then only compiler will generate
default constructor i.e if we are writing at least one constructor then
compiler won’t generate default constructor. Hence every class in java
can contain constructor, it may be default constructor generated by
compiler or customized constructor explicitly provided by programmer
but not both simultaneously.
Test(){
} super();
}
}
}
}
Test(){ Test(){
super();
} }
} }
} }
Test(){ Test(){
this(10); this(10);
} }
Test(){ Test(){
super();
} }
} }
Case1:
package pack5;
class Test {
Test(){
System.out.println("constructor");
super();---------------------------------Error
}
Case2:
Within the constructor we can either super() or this() but not both
simultaneously.
package pack5;
class Test {
Test(){
super();
this();---------------------------------Error
}
}
Compile time error: call to this() must be 1st statement in
constructor.
Case3:
We can use super() or this() only inside in constructor but not inside
method. If we are trying to use outside of constructor we will get compile
time error.
class Test {
Overloaded constructor:
1. Within a class we can declare multiple constructors and all these,
constructor having same name but different type of argument. Hence all
these, constructor are considered as overloaded constructors. Hence
overloading concept applicable for constructors.
package pack5;
class Test {
Test() {
this(10);
System.out.println("no-arg");
}
Test(int i){
this(10.5);
System.out.println(i);
}
Test(double d){
System.out.println(d);
}
System.out.println();
System.out.println();
System.out.println();
package pack6;
Output:
parent constructor
child constructor
package pack5;
class Test {
Test() {
package pack5;
Test() {
}
}
package pack5;
interface Test {
Test() {-------------------Error
Case1:
Recursive method call is run time exception saying stack overflow error.
In recursive method call case when we don’t call any method in main method:
package pack5;
}
In recursive method call case when we call any method in main method:
package pack5;
System.out.println("Hello");
}
Infinite time
M2() method
M1() method
M2() method
M1() method
M2() method
M1() method
Main method
package pack5;
Test(){
this(10);-----------compile time error
}
Test(int i){
this();-----------compile time error
}
package pack5;
Test(){
this(10);
}
Test(int i){
this();
}
Case2:
Example1:
package pack6;
package pack6;
Child(){
super();
}
}
Example2:
package pack6;
Parent(){
super();
package pack6;
Child(){
super();
}
}
Example3:
package pack6;
Parent(int i){
super();
}
package pack6;
Child(){
super();
}
}
Note:
Parent(){
super();
}
Parent(int i){
super();
}
package pack6;
Child(){
super();
}
}
Case3:
Example1:
package pack6;
import java.io.IOException;
public class Parent {
package pack6;
Example2:
package pack6;
import java.io.IOException;
package pack6;
Child(){
try {
super();---------------Error
} catch (Exception e) {
// TODO: handle exception
}
}
}
package pack6;
import java.io.IOException;
package pack6;
import java.io.IOException;
OR
package pack6;
OR
package pack6;
Note:
Answer:
The main purpose of constructor is to perform initialization of an
object.
b) The main purpose of constructor is to perform initialization of an
object. (true)
c) The name of the constructor need not be same as class name. (false)
Answer:
The name of the constructor need be same as class name
d) Return type concept applicable for constructors but only void. (false)
Answer:
Return type concept not applicable for constructors, even void
also.
Answer:
We can apply only public, private, protected modifier.
Answer:
Default constructor generated by compiler.
Answer:
Compiler will not always generate default constructor
(When programmers are create default constructor).
Answer:
If we are writing no-arg constructor then compiler will not
generate default constructor
Answer:
Every no argument constructor is always default constructor.
(When programmer create no argument constructor then it is not a
default constructor).
k) Default constructor is always no-arg constructor. (true)
Answer:
The 1st line inside every constructor should be either super()
or this(). If we are not writing anything then compiler will
generate super().
Answer:
For constructor both overloading and overriding concepts are not
applicable.
Answer:
For constructor inheritance concept is not applicable.
Answer:
Concrete classes and abstract classes both can contain
constructors.
Answer:
Interface cannot contain constructors.
Answer:
Recursive constructor invocation is a compile time exception.