Professional Documents
Culture Documents
class,object
class:
class Example:
Class java
properties(variables);
Actions(methods);
}
object:
Any language present objects and this should not be confounded with
the most powerful concept of object-orientation.
int i=10;
void mone()
System.out.println("java");
{
Sam s=new Sam(); //Here is the Object
Thank you for visiting Java Examples.For regular updates request you to subscribe to my
RSS Feed or register your Email Subscription for updates directly into your mail box.
Abstraction
Thank you for visiting Java Examples.For regular updates request you to subscribe to my
RSS Feed or register your Email Subscription for updates directly into your mail box.
Encapsulation
Encapsulation Advantages:
Security
Easy to enhance
Maintainability
Modularity
Example:
class Sample
{
public int i=10;
public int getId()
{
return i;
}
public void setId(int x)
{
this.i=x;
}
}
The major limitations od encapsulation is,it increases the code
(because we have to getter and setter methods for the data variables )
and hence slows down the execution.
Java Inheritance
IS-A relation:
• Also known as Inheritance
• By using extends keyword we can implement IS-A relationship.
• Re usability is the benefit of IS-A relationship
Inheritance is accepted up to certain level but after reaching problems.
Because for every child class object, internally all the parent objects
will be created in the inheritance tree.
In the real time it is recommended to have inheritance up to 8 to 10
levels only. Beyond that it is not suggestible.
is a relation Example:
class Superclass
{
void display()
{
System.out.println("Hi");
}
}
class InheritanceExample extends Superclass
{
public static void main(String arg[])
{
InheritanceExample ie=new InheritanceExample();
ie.display();
}
}
HAS-A relation:
• Also known as composition or Aggregation.
• By using new operator we can implement HAS-A relationship.
• Reusability(CBM->Component Based Model)is the benefit of HAS–A
relationship.
• The limitation of HAS-A relation ship is , we are increasing the
dependency between the classes. As a result, the maintenance of the
code becomes complex or costly.
has a relation Example:
class Car
{
Engine e= new Engine();
. …….
.......
}
class Engine
{
m1(){}
m2(){}
}
Car class has Engine reference. Hence Car allowed using all the
features of engine. But without Engine object we can’t create Car
object.
Thank you for visiting Java Examples.For regular updates request you to subscribe to my
RSS Feed or register your Email Subscription for updates directly into your mail box.
Method Signature
Method Signature :
Example:
public void m1(int a, int b)
{
m1(int,int); -----> signature of method m1
}
Example:
public void m1(int a, float b)
{
m1(int , float); //valid
m1(float , int);//invalid
}
Example:
class Test
{
public int m1(int i){}//invalid, CTE: m1(int) is already defined in test
public void m1(int i){}
}
Thank you for visiting Java Examples.For regular updates request you to subscribe to my
RSS Feed or register your Email Subscription for updates directly into your mail box.
Method Overloading
Method Overloading:
Two methods having the same name are not allowed in the case of C-
language, for every data type even though the functionality in the
same; we should maintain different method names. It increases the
complexity of the programming. But in java, two methods having the
same name is allowed irrespective of parameters. We can maintain the
same method name for similar type of functionality. It simplifies the
programming. Such types of methods are called Overloaded methods.
• Two methods are said to be overloaded if and only of they have the
same method name, but different parameter list(at least order).
• The signatures of the two overloaded methods must be different.
Example:
public void m1(){}
private int m1(int i){}
• Here m1() & m1(int i) are overloaded. We never consider return
type, access modifier and throws class in overloading.
Example:
class Sample
{
public void m1()
{
System.out.println(“no arg”);
}
public void m1(int i)
{
System.out.println(“ int arg”);
}
public void m1(double d)
{
System.out.println(“ double arg”);
}
public static void main(String a[])
{
Sample s=new Sample();
s.m1();
s.m1(10);
s.m1(10.5);
}
}
s.m1(10L);//double arg
s.m1(‘a’);//int arg
}
}
In the case of overloading the method resolution performed by the
compiler is based on the reference type.
Thank you for visiting Java Examples.For regular updates request you to subscribe to my
RSS Feed or register your Email Subscription for updates directly into your mail box.
Labels: OOPS comments (0)
Method Overriding
If you don’t want parent class implementation for any method we can
override in the child class based on our child class requirement. This
concept is called Overriding.
While overriding we have to fallow rules:
1. In the overriding, the method names and arg’s must be same.
Ie. In the case of the overriding the signatures of the methods must
be same
Until 1.4 version, the return types must be same. But from 1.5 version
onwards covariant return types are also allowed.
Example:
class p { public Number getNumber(){} }
class c extends P{ public Number getNumber(){} }
(or)
class C extends P
{
public Byte/Short/Integer getNumber(){}
}
Example:
import java.io.*;
class P
{
public object m1(){ return new object; }
}
class C extends P
{
public Object m1(){ return new Object; }
}
class C extends P
{
public String m1(){ return “durga”; }
}//in 1.4 CTE saying m1() in C cannot override found:string,
req:object: in 1.5 ver no CTE
class P{
public void m1()
{ } }//non abstract
abstract class C extends P{
public abstract m1(); }//valid abstract
Method Hiding:-
This is exactly same as overriding except both parent & child class
methods must be declared as static. In the method hiding the method
resolution take care by compiler only based on the reference type.
Ex:
class P
{
static int x=10;
int y=20;
}
class C extends P
{
static int x=100;
int y=200;
}
class Sample
{
public static void main(String[] a)
{
P p=new C();
System.out.println(p.x+”,”+p.y); //10,20
C c=new C();
System.out.println(c.x+”,”+c.y); //100,200
P p1=new P();
System.out.println(p1.x+”,”+p1.y); //10,20
}
}
• We can’t override in the child class. But if define exactly same
variable in child class.
• Variable resolutions take care by compiler only based on the
reference type.
Thank you for visiting Java Examples.For regular updates request you to subscribe to my
RSS Feed or register your Email Subscription for updates directly into your mail box.
Constructors
If we will give return type for the constructor that thing as a method
instead of constructor that thing as a method instead of constructor
(so,there is no CTE). Ie., it is legal (but stupid) to have a method
whose name same as classname.
Default Constructor:-
If the programmer is not writing any constructor, then only compiler
will generate a default constructor.
Ie., either programmer written constructor or compiler generated must
present in your class but not both at a time.
Prototype of default constructor shown below:
a).Programmer written code:- class Test{}
Compiler generated code:-
class Test
{
Test()
{
super();
}
}
Overloaded Constructors:
We are allowed to keep more than one constructor inside a class ,
which are considered as overloaded constructors. We can’t override
the constructors, because they belong to the same Ex: class Test {
Test(int i){}
Test(){} ---->Overloaded Constructors
}
Example:
class P {
P(int i)
{
System.out.println(i);
}
}
class C extends P
{
C()
{
super(10); // valid (without super invalid)
}
}
Example: class P
{
P() throws Exception ----> checked exception
{}
}
class C extends P
{
C() // compile time error unhandled exception type exception
{}
}
Thank you for visiting Java Examples.For regular updates request you to subscribe to my
RSS Feed or register your Email Subscription for updates directly into your mail box.
We can apply abstract keyword for the classes and methods .abstract
keyword is not possible for variables.If method declared as abstract we
don’t know implementation child class is responsible to provide the
implementation for the parent class abstract methods.
Abstract method has declaration only and should not have any
implementation.
This is the way of decorating abstract method .we should not keep
curly braces at end.
Example 2:
abstract class Xy
{
abstract void m1(); // valid
}
Example 3:
abstract class X
{
void m1() // valid
{
System.out.println(“valid”);
}
}
Example 4:
abstract class X
{
abstract void m2();
}
Thank you for visiting Java Examples.For regular updates request you to subscribe to my
RSS Feed or register your Email Subscription for updates directly into your mail box.
Declaring an interface:
interface Interf
{
public void m1();
public void m2();
}
class Bea implements Interf
{
public void m1()
{
System.out.println(“implementation m1”);
}
public void m2()
{
System.out.println(“implementation m2”);
}
public static void main(String arg[])
{
Interf i=new Interf();
i.m1();
}
}
interface interf
{
int x=10;
public int x=10;
public static final int x=10;
final int x=10;
static int x=0;
}
Hence an interface we never declared as private ,protected and
transient.
interface Interf
{
int x;
}
Inside implement classes we are not allowed to change the value of
interface variable violation leads to compile time error.
Class Sample implement Interf
{
public static void main(String arg[])
{
x=20; //compile time error
}
}
A class can extend only one class at a time .But can interface can
extend any number interfaces.
A class can implement any number of interfaces at a time.But interface
never implement another interface.
Case 2:
The interface having same method name but different arguments.
Interface Inter
{
public void m1();
public void m1(int x);
}
case 3:
Same method name,same arguments but different written types.
Interface Left
{
public void m1();
}
Interface Right
{
public int m1();
}
Exceptional case :
We can’t provide implementation at a time for two or more interfaces
having methods with same signature but different return type .