Sie sind auf Seite 1von 3

Software objects are conceptually similar to real world objects: they too consist of state and

related behavior(state->fields and behavior->method)

known as instance variables because their values are unique to each instance of a class. the
currentSpeed of one bicycle is independent from the currentSpeed of another.
Static keyword tells the compiler that there is exactly one copy of that variable and
regardless of hwo many instances of that particular class is created the variable gonna
remain same. the number of gears for a particular kind of bicycle could be marked as static
since conceptually the same number of gears will apply to all instances. The code static int
numGears = 6; static vs final
Java byte-code runs on the Java Virtual Machine, the size (number of bytes) for each
primitive type is fixed. Difference bw c and java no sizeof
Making main static indicates that JVM doesnt have to instantiate the class for running the
main method.
You can not access an instance variable inside a static method nor you can call a non-static
method inside the static method.
compiler does not consider return type when differentiating methods, so you cannot
declare two methods with the same signature even if they have a different return type.
A class having no instance variable(only static variables present) , there is no need for
instantiating as it is a stateless class, one can simply make its methods static and call it by
class name.
Memory leak is handled through garbage collector which searches for orphaned memory in
heap from time to time(java vs c/c++)
StringBuffer sb = new StringBuffer("Hello, world"); is mutable
Java compiler automatically inserts a call to parent class default constructor.
To manually call the construtctor super() keyword is to be used with specific paramters
toString and equals are properties of Object class. System.out.print(object) automaticallya
calls the toString method.
Upcasting done automatically and downcasting done explicitly
o Car c= new Car();
o c. recharge();//error
o ElectricCar ec=(ElectrcCar)c;//downcast
o ec.recharge()// still error
o ec instanceof ElecircCar returns false
An abstract class cannot be insantiated.somebehaviour is partially left to be completed by
subclass.
Interface
o All methods and attributes public
o Attributes static and final
o Mthods have no definition
o Cannot be intantiated
An interface can extend multiple interfaces. A class can extend only one class but can
implement multiple interfaces.
An overrridng method must either maintain or extend the visibility of inherited method.
o Any protected method cannot be overridden as private method.
Dynamic method dispatch , runtime polymorphism and method overriding
Final variable cant be altered even by subclass and final methods cant be overridden by
subclass. Final class cannot be inherited.
Even if a single method is abstract the whole class has to be made abstract
Immutability means no changes made to the string,whatever change made is done on new
location , the freed location is garbage handled.
.append(xyz) vs + string buffer
S.No Process Thread

1 No Sharing of Memory Sharing of Memory and other


data structures

2 Can not Corrupt Data Can Corrupt Data Structures


structures

3 Context switching is Context Switching is Chaeper


Expensive

Synchronization is done on the shared object. Only one thread can own a monitor at a
given time.
Advantage of inner class Both of their methods have access to each other's private
methods and instance variables
o If both the inner and outer class have a method with the same

name, and the intent is to inv oke the method in the outer class,

then the following invocation must be used:

OuterClassName.this.methodName()

o BankAccount account = new BankAccount();

BankAccount.Money amount =account.new Money("41.99");

o A aObject = new A();

A.B bObject = aObject.new B();

A.B.C cObject = bObject.new C();

o Anonymous class eg :-abc obj=new abc(){

private String name;

public setName(String p){p=name;}

public getName(){return p;}

obj.setName(sarabh);

obj.getName();}

Das könnte Ihnen auch gefallen