Successfully reported this slideshow.

Oop lecture5

573 views

Published on

Published in: Education
  • Be the first to comment

  • Be the first to like this

Oop lecture5

  1. 1. Lecture 5Some concepts Object Oriented Programming Eastern University, Dhaka Md. Raihan Kibria
  2. 2. Primitive types Almost like the primitives in C:public class PrimitiveDemo { int i;//integer long l;//is longer than integer float fl;//float double dbl;//double; has more precision than float char[] charArray;//char array int[] intArray;//integer array boolean b;//true or false public static void main(String[] args) { PrimitiveDemo demo = new PrimitiveDemo(); demo.charArray = new char[]{E, A, S, T, E, R,N}; for (int i=0; i<demo.charArray.length; i++) System.out.print(demo.charArray[i]); }}Prints: EASTERN
  3. 3. Boxed types A primitive like integer has a Boxed equivalent: Primitive Boxed integer Integer long Long double Double float Float boolean BooleanInteger I = null;//is possible because it is an objectButint i = null;//is illegal because i is not an object
  4. 4. Type conversionpublic class TypeConversion {public static void main(String[] args) { long longval = 234234234L; int intval = (int)longval; }}
  5. 5. StringsStrings are objects in java. An object of String is created in the following: public class StringDemo { public static void main(String[] args) { String name1 = "john"; System.out.println(name1); } } Strings can be concatenated: public class StringDemo { public static void main(String[] args) { String name1 = "john"; name1 = name1 + " kerry"; System.out.println(name1); } } Prints: john kerry
  6. 6. Methodspublic class MethodDemo { static void doSomething(){//static method System.out.println("hello"); } void doALittle(){//instance method System.out.println("a text"); } public static void main(String[] args) { //static method can be called without creating instance MethodDemo.doSomething(); //instance method must be called only on the instance MethodDemo demo = new MethodDemo(); demo.doALittle(); }}
  7. 7. ConstructorConstructor: as soon as an object is instantiated, constructor is called. Constructor is a method with the same name as the class. But there are no type modifiers (void, int, etc.) to the left of a constructor. public class ConstructorDemo { int i; public ConstructorDemo(){ i = 9; } public void printValue(){ System.out.println("Value is: " + i); } public static void main(String[] args) { new ConstructorDemo().printValue(); } } Prints: value is: 9
  8. 8. public, protected, private modifierspublic definitions are accessible outside of packageprotected definitions are accessible inside the packageprivate definitions are accessible only within the class
  9. 9. Example of public/privatepackage com; package net;public class A { import com.A; public void doSomething(){ doSomethingPrivately(); public class B { } public static void main(String[] private void args) {doSomethingPrivately(){ A a = new A(); a.doSomething(); } } } public static voidmain(String[] args) { }} doSomethigPrivately() of A is not accessible from B

×