OOP Features
OOP Features
OOP Features
Data hiding
Abstraction
Encapsulation
Method signature
IS-A relationship
HAS-A relationship
Overloading
Overriding
Polymorphism
Constructors
Interface
Data hiding
Data means variables. Data hiding means hiding data from other classes.
We can achieve this using private modifier for variables.
Ex:
package pack1;
class B
{
Private int x=1111;
}
Public class A extends B
{
Public static voidmain(String[] args)
{
B b= new B();
System.out.println(b.x);//CE
}
}
Abstraction:
Hiding implementation details while exposing
service names.
We can achieve this using interfaces.
The advantage is that we will get security.
Ex:
interface Bank
{
Abstract int withdraw(intaccno);
}
Encapsulation:
it is the process of binding variables and
methods together into a single unit.
We can achieve this by using class concept.
Ex:
Class A
{
int x=10;
void m1()
{
}
}
Method signature:
Method signature means method name followed by
parameter types.
Method signature doesn’t include AMs, NAMs,
return type, throws clause.
Ex1:
m1(int i) ----> m1(int)
{
}
Ex2:
m2(float f)----> m2(float)
{
}
Ex3:
m1(int i, float f)---->m1(int, float)
{
}
Ex4:
m1()---> m1()
{
}
Ex5:
m1(String s, float f)---->m1(String,float)
{
}
Note:
A class cannot contain 2 same methods with same
signatures, otherwise it’s a CE.
Ex:
package pack1;
publicclass A
{
Publicvoidm1(inti)
{
System.out.println("helo");
}
Privateintm1(intj)
{
System.out.println("hi");
}
publicstaticvoidmain(String[] args)
{
A a=new A();
a.m1(10);//CE
}
}
Overloading:
Two methods of a class are said to be overloaded
if they have same name but different parameter
types.
Ex1:
publicclass A
{
Publicvoid m1(inti)
{
System.out.println("helo");
}
Privateint m1(floati)
{
System.out.println("hi");
return 10;
}
}
Ex2:
publicclass A
{
publicvoid m1(inti,floatf)
{
System.out.println("helo");
}
privateint m1(floatf,inti)
{
System.out.println("hi");
return 10;
}
}
In method overloading, method resolution is
taken care by the compiler based on the
reference type that is used to invoke the method
and the reference type or primitive type that is
passed as argument.
Method resolution: which method has to be
executed for what method call.(yeh method call
ki, yeh method execute kavali ani cheppadanne
method resolution antamu).
Ex:
package pack1;
publicclass Test
{
publicvoid m1(inti)
{
System.out.println("int arg method");
}
int m1(floatf)
{
System.out.println("float arg method");
return 10;
}
publicvoid m1(String s)
{
System.out.println("string arg method");
}
publicstaticvoidmain(String[] args)
{
Test t=new Test();
t.m1(10);
t.m1(10.5f);
t.m1('a');//int-arg method
}
}
Case1:automatic promotion in method overloading
In method overloading if exact method match is
not found then compiler doesn’t rise any CE.
Compiler tries to promote the method argument to
the next level and checks if any method match is
available or not, if available then it will
executed otherwise compiler promotes the method
argument again to the next level. In this way
compiler checks all the possible promotions,
finally if no method is matched then compiler
rises error.
char
2
ex1:
package pack1;
publicclass Test
{
int m1(floatf)
{
System.out.println("float arg method");
return 10;
}
publicvoid m1(String s)
{
System.out.println("string arg method");
}
publicstaticvoidmain(String[] args)
{
Test t=new Test();
byteb=10;
t.m1(b);
t.m1(new String(“hyd”));
}
}
Ex2:
package pack1;
publicclass Test
{
int m1(Object o)
{
System.out.println("object arg method");
return 10;
}
publicvoid m1(Number n)
{
System.out.println("Number arg method");
}
publicvoid m1(Integer I)
{
System.out.println("Integer arg method");
}
publicstaticvoidmain(String[] args)
{
Test t=new Test();
Integer I=newInteger(10);
t.m1(I);//Integer-arg method
/*if m1(Integer) method is not available then
m1(Number) method will be executed.
If m1(Number) method is not available then
m1(Object) method will be executed */
t.m1(new Integer(10));//Integer-arg method
}
}
Ex3:
package pack1;
class Animal
{
}
class Tiger extends Animal
{
publicstaticvoidmain(String[] args)
{
Test t=new Test();
Animal a=newAnimal();
t.m1(a);//method call - Animal version
method
Tiger tr=newTiger();
t.m1(tr);//method call - Tiger version
method
publicclass Test3 {
publicstaticvoidmain(String[] args) {
// TODO Auto-generated method stub
Parent1 p1=new Parent1();
p1.workhard();//college method
//if both reference type and object
type
//are same then same class method
//will be executed.
Child1 c1=new Child1();
c1.workhard();//bar method
//if both reference type and object
type
//are same then same class method
//will be executed.
Parent1 p2=new Child1();
//parent class reference variable
can refer child class object
p2.workhard();//bar method
//p2.love();//CE
p2.care();//valid
}
}
Level of access modifiers:
public>protected>default>private
Overriding rules:
i>about access modifiers
while overriding, the scope of child class
method access modifier should be same or higher
than parent class access modifier.
public>protected>default>private.
Ex:
Parent method child method
Public public
Protected protected,public(but it
cannot be default or private)
Default default,protected,public
- strictfp non-strictfp
- Native non-native
- Synchronized non-synchronized
iii>
a)if parent class methods return type is a
primitive type then the child class overriding
method’s return type should be same.
b)if parent class methods return type is a class
type then child class overriding method’s return
type can be same class or sub-class, but it
cannot be super class.
Ex1:valid
class Parent1 {
Number workhard() {
System.out.println("wakeup
early,gotocollege");
return 10;
}
}
class Child1 extends Parent1 {
Numberworkhard() {
System.out.println("wakeup
anytime,goto bar");
return 20;
}}
Ex2:valid
class Parent1 {
Number workhard() {
System.out.println("wakeup
early,gotocollege");
return 10;
}
}
class Child1 extends Parent1 {
Integerworkhard(){
System.out.println("wakeup
anytime,goto bar");
return 20;
} }
Integer class is child class of Number class.
Number class is child class of Object class.
Ex3:invalid
class Parent1 {
Number workhard() {
System.out.println("wakeup
early,gotocollege");
return 10;
}
}
class Child1 extends Parent1 {
Object workhard() {
System.out.println("wakeup
anytime,goto bar");
return 20;
} }
iv> about exceptions
if parent class method throws a checked
exception, then the child class overriding
method can
a>throw same checked exception OR
b> child checked exception OR
c>nothing(child class overriding method need not
throw any checked exception).
d>child class overriding method cannot throw
parent checked exception.
Ex1:
class Parent1 {
Number workhard() throwsIOException
{
System.out.println("wakeup
early,gotocollege");
return 10;
}}
class Child1 extends Parent1{
Number workhard() throwsIOException
{
System.out.println("wakeup
anytime,goto bar");
return 20;
}}
Ex2:
class Parent1 {
Number workhard() throwsIOException
{
System.out.println("wakeup
early,gotocollege");
return 10;
} }
class Child1 extends Parent1 {
Number workhard()
throwsFileNotFoundException
{
System.out.println("wakeup
anytime,goto bar");
return 20;
} }
Ex3:nothing
class Parent1 {
Number workhard() throwsIOException {
System.out.println("wakeup
early,gotocollege");
return 10;
} }
class Child1 extends Parent1 {
Number workhard() {
System.out.println("wakeup
anytime,goto bar");
return 20;
} }
Polymorphism:
Polymorphism is the ability of an object or a
method to take multiple forms.
a>about an object
i>parent class reference variable can refer/hold
child class objects.
ii>interface reference variable can refer
implementation class object.
Ex:
Parent p1=new Child1();
;
;
;
p1=new Child2();
;
;
;
P1=new Child3();
b) about method
overloading is static polymorphism and
overriding is dynamic polymorphism.
}
Output:
O
Null
O
Null
Ex2:not recommended to initialize instance variables at
the time of declaration because every object will have
same values.
package pack1;
publicclass Student {
introllno=101;
String sname="uday";
Publicstaticvoidmain(String[] args) {
Student s1=newStudent();
Student s2=newStudent();
System.out.println(s1.rollno);
System.out.println(s1.sname);
System.out.println(s2.rollno);
System.out.println(s2.sname);
}
}
Output
101
Uday
101
Uday
Ex3:not recommended to initialize instance variables
inside instance block because every object will have
same values.
For every object creation same instance block gets
executed.
package pack1;
publicclass Student {
introllno;
String sname;
{//instance block
rollno=101;
sname="uday";
}
Publicstaticvoidmain(String[] args) {
Student s1=newStudent();
Student s2=newStudent();
System.out.println(s1.rollno);
System.out.println(s1.sname);
System.out.println(s2.rollno);
System.out.println(s2.sname);
}
}
Output:
101
Uday
101
Uday
Publicstaticvoidmain(String[] args) {
Student s1=newStudent();
s1.rollno=101;
s1.sname="uday";
Student s2=newStudent();
s2.rollno=102;
s2.sname="kumar";
System.out.println(s1.rollno);
System.out.println(s1.sname);
System.out.println(s2.rollno);
System.out.println(s2.sname);
}
Output:
101
Uday
102
Kumar
Publicstaticvoidmain(String[] args) {
Student s1=new Student(101,"uday");
System.out.println(s1.rollno);
System.out.println(s1.sname);
System.out.println(s2.rollno);
System.out.println(s2.sname);
}
Output:
101
Uday
102
Kumar
Ex6:usage of this keyword.
package pack1;
publicclass Student {
introllno;
String sname;
Student(introllno,Stringsname)
{
this.rollno=rollno;
this.sname=sname;
}
Publicstaticvoidmain(String[] args) {
Student s1=new Student(101,"uday");//line
A
Student s2=new
Student(102,"kumar");//line B
System.out.println(s1.rollno);
System.out.println(s1.sname);
System.out.println(s2.rollno);
System.out.println(s2.sname);
}
}
“This” refers current object.
When the control is at line A, this refers s1
object.
When the control is at line B, this refers s2
object.
Ex7:
For every object creation corresponding
constructor must be there.
package pack1;
publicclass Student {
introllno;
String sname;
Student(introllno,Stringsname)
{
this.rollno=rollno;
this.sname=sname;
}
Student(introllno)
{
this.rollno=rollno;
System.out.println(s1.rollno);
}
Publicstaticvoidmain(String[] args) {
Student s1=new Student(101,"uday");
System.out.println(s1.rollno);
System.out.println(s1.sname);
System.out.println(s2.rollno);
System.out.println(s2.sname);
System.out.println(s3.rollno);
}
}
Rules for writing constructors:
1.constructor name should be same as class name.
2.consturctors will not have return types.
3.a constructor can take the 2 AMs
(public,default).
4.none of the NAMs are applicable for a
constructor.
5.default constructor:
a>When we don’t write any constructor then
compiler gives us a no-arg constructor called as
default constructor.
If we write any constructor then compiler will
not give default constructor.
b>the AM of default constructor is same as class
AM, this rule is applicable only for public,
default classes.
Ex:
Programmers code compiler generated
code
class Test
{ class Test
} {
Test()
{
super();
}
}
{
Test()
{
}
}
Note:
The first line of every constructor should be
super() or this().
When we don’t write anything then compiler
places super().
super() is a call to super class no-arg
constructor.
this() is a call to current class no-arg
constructor.
Diff. between
publicclass Test {
1.staticint a=10; 7
2.static
{//static block
System.out.println("FSB"); .8
m1(); .9
}
3.staticvoid m1()
{//static method
System.out.println(a); 10
System.out.println(b); 11
}
4.publicstaticvoidmain(String[] args)
{
m1();
System.out.println("main");
}
5.staticintb=20;
6.static
{
System.out.println("SSB");
m1();
}
}