Created
April 2, 2018 18:22
-
-
Save vakho10/77d4c04287fad8d0b27c3bbc8e3ff736 to your computer and use it in GitHub Desktop.
Java OOP principles with examples.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Abstraction - separating something out of the class and reusing it anywhere else. | |
public class Person { | |
String firstName; | |
String lastName; | |
short age; | |
Address address; // Taking address specific fields out into another class | |
} | |
// This now can be used anywhere else | |
class Address { | |
String streetName; | |
short streetNumber; | |
short flatNumber; | |
} | |
class Employee { | |
// some fields ... | |
Address address; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 1) Encapsulation - keep fields private and provide access using public methods. | |
public class Person { | |
private String firstName; | |
private String lastName; | |
private short age; | |
private float weight; | |
public Person() { | |
super(); | |
} | |
public String getFirstName() { | |
return firstName; | |
} | |
public void setFirstName(String firstName) { | |
this.firstName = firstName; | |
} | |
public String getLastName() { | |
return lastName; | |
} | |
public void setLastName(String lastName) { | |
this.lastName = lastName; | |
} | |
public short getAge() { | |
return age; | |
} | |
public void setAge(short age) { | |
this.age = age; | |
} | |
public float getWeight() { | |
return weight; | |
} | |
public void setWeight(float weight) { | |
this.weight = weight; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Inheritance - lets you create new classes that shares some of the attributes of existing class | |
public class Vehicle { | |
float speed; | |
double weight; | |
// etc. | |
} | |
class Car extends Vehicle { | |
short wheels; | |
// etc. | |
} | |
class Plane extends Vehicle { | |
short wings; | |
float wingLength; | |
// etc. | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Polymorphism - lets programmers use the same word to mean different things in different contexts. | |
// | |
// One form of polymorphism in Java is method overloading. That’s when different meanings are implied | |
// by the code itself. The other form is method overriding. That’s when the different meanings are | |
// implied by the values of the supplied variables. | |
public class Person { | |
void walk() { | |
System.out.println("Running..."); | |
} | |
} | |
class Employee extends Person { | |
void walk() { | |
System.out.println("Running fast..."); | |
} | |
public static void main(String arg[]) { | |
Person p = new Employee(); | |
p.walk(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment