OOPs & JAVA By Sandesh Sharma
Contents  Objects & Classes  Encapsulation  Composition, Inheritance and Delegation  Polymorphism  Language Basics
Objects & Classes  Variable − Access Modifiers  Private  Public  Default  Classes − Constructors − Interface − Nested Clasess − Anonym Classes  Objects
Access Modifier  public modifier—the field is accessible from all classes.  private modifier—the field is accessible only within its own class. public class Bicycle { private int cadence; private int gear; private int speed; public Bicycle(int startCadence, int startSpeed, int startGear) { gear = startGear; cadence = startCadence; speed = startSpeed; } }
Classs public class Bicycle { // the Bicycle class has // three fields public int cadence; public int gear; public int speed; // the Bicycle class has // one constructor public Bicycle(int startCadence, int startSpeed, int startGear) { gear = startGear; cadence = startCadence; speed = startSpeed; } // the Bicycle class has // four methods public void setCadence(int newValue) { cadence = newValue; } public void setGear(int newValue) { gear = newValue; } public void applyBrake(int decrement) { speed -= decrement; } public void speedUp(int increment) { speed += increment; } }
Interface interface Bicycle { // wheel revolutions per minute void changeCadence(int newValue); void changeGear(int newValue); void speedUp(int increment); void applyBrakes(int decrement); } class ACMEBicycle implements Bicycle { int cadence = 0; int speed = 0; int gear = 1; // The compiler will now require that methods // changeCadence, changeGear, speedUp, and applyBrakes // all be implemented. Compilation will fail if those // methods are missing from this class. void changeCadence(int newValue) { cadence = newValue; }
Method Overloading public class DataArtist { ... public void draw(String s) { ... } public void draw(int i) { ... } public void draw(double f) { ... } public void draw(int i, double f) { ... } }
Constructor public Bicycle(int startCadence, int startSpeed, int startGear) { gear = startGear; cadence = startCadence; speed = startSpeed; } Rectangle rectTwo = new Rectangle(50, 100);
Nested Classes class OuterClass { ... class NestedClass { ... } } class OuterClass { ... static class StaticNestedClass { ... } class InnerClass { ... } }
Anonymous Classes HelloWorld frenchGreeting = new HelloWorld() { String name = "tout le monde"; public void greet() { greetSomeone("tout le monde"); } public void greetSomeone(String someone) { name = someone; System.out.println("Salut " + name); } };
Polymorphism public class RoadBike extends Bicycle{ // In millimeters (mm) private int tireWidth; public RoadBike(int startCadence, int startSpeed, int startGear, int newTireWidth){ super(startCadence, startSpeed, startGear); this.setTireWidth(newTireWidth); } public int getTireWidth(){ return this.tireWidth; } public void setTireWidth(int newTireWidth){ this.tireWidth = newTireWidth; } public void printDescription(){ super.printDescription(); System.out.println("The RoadBike" + " has " + getTireWidth() + " MM tires."); } }
Polymorphism public class TestBikes { public static void main(String[] args){ Bicycle bike01, bike02, bike03; bike01 = new Bicycle(20, 10, 1); bike02 = new MountainBike(20, 10, 5, "Dual"); bike03 = new RoadBike(40, 20, 8, 23); bike01.printDescription(); bike02.printDescription(); bike03.printDescription(); } }
Language Basics  clone()  equals()  finalize()  getClass()  hashCode()  ToString()  final  Abstract  String / stringbuilder / stringbuffer  Try / catch
thAnks  Email at ersandeshsharma[at]gmail.com

OOP and java by a introduction sandesh sharma

  • 1.
    OOPs & JAVA BySandesh Sharma
  • 2.
    Contents  Objects & Classes  Encapsulation  Composition,Inheritance and Delegation  Polymorphism  Language Basics
  • 3.
    Objects & Classes  Variable −Access Modifiers  Private  Public  Default  Classes − Constructors − Interface − Nested Clasess − Anonym Classes  Objects
  • 4.
    Access Modifier  public modifier—thefield is accessible from all classes.  private modifier—the field is accessible only within its own class. public class Bicycle { private int cadence; private int gear; private int speed; public Bicycle(int startCadence, int startSpeed, int startGear) { gear = startGear; cadence = startCadence; speed = startSpeed; } }
  • 5.
    Classs public class Bicycle{ // the Bicycle class has // three fields public int cadence; public int gear; public int speed; // the Bicycle class has // one constructor public Bicycle(int startCadence, int startSpeed, int startGear) { gear = startGear; cadence = startCadence; speed = startSpeed; } // the Bicycle class has // four methods public void setCadence(int newValue) { cadence = newValue; } public void setGear(int newValue) { gear = newValue; } public void applyBrake(int decrement) { speed -= decrement; } public void speedUp(int increment) { speed += increment; } }
  • 6.
    Interface interface Bicycle { //wheel revolutions per minute void changeCadence(int newValue); void changeGear(int newValue); void speedUp(int increment); void applyBrakes(int decrement); } class ACMEBicycle implements Bicycle { int cadence = 0; int speed = 0; int gear = 1; // The compiler will now require that methods // changeCadence, changeGear, speedUp, and applyBrakes // all be implemented. Compilation will fail if those // methods are missing from this class. void changeCadence(int newValue) { cadence = newValue; }
  • 7.
    Method Overloading public classDataArtist { ... public void draw(String s) { ... } public void draw(int i) { ... } public void draw(double f) { ... } public void draw(int i, double f) { ... } }
  • 8.
    Constructor public Bicycle(int startCadence,int startSpeed, int startGear) { gear = startGear; cadence = startCadence; speed = startSpeed; } Rectangle rectTwo = new Rectangle(50, 100);
  • 9.
    Nested Classes class OuterClass{ ... class NestedClass { ... } } class OuterClass { ... static class StaticNestedClass { ... } class InnerClass { ... } }
  • 10.
    Anonymous Classes HelloWorld frenchGreeting= new HelloWorld() { String name = "tout le monde"; public void greet() { greetSomeone("tout le monde"); } public void greetSomeone(String someone) { name = someone; System.out.println("Salut " + name); } };
  • 11.
    Polymorphism public class RoadBikeextends Bicycle{ // In millimeters (mm) private int tireWidth; public RoadBike(int startCadence, int startSpeed, int startGear, int newTireWidth){ super(startCadence, startSpeed, startGear); this.setTireWidth(newTireWidth); } public int getTireWidth(){ return this.tireWidth; } public void setTireWidth(int newTireWidth){ this.tireWidth = newTireWidth; } public void printDescription(){ super.printDescription(); System.out.println("The RoadBike" + " has " + getTireWidth() + " MM tires."); } }
  • 12.
    Polymorphism public class TestBikes{ public static void main(String[] args){ Bicycle bike01, bike02, bike03; bike01 = new Bicycle(20, 10, 1); bike02 = new MountainBike(20, 10, 5, "Dual"); bike03 = new RoadBike(40, 20, 8, 23); bike01.printDescription(); bike02.printDescription(); bike03.printDescription(); } }
  • 13.
  • 14.