top of page

Java Abstraction Inheritance Polymorphism Real World Example

  • javastrokes
  • Dec 27, 2016
  • 3 min read

An Object Oriented (OO) programming, a program consists of interacting objects. An object encapsulates data and algorithms. An object's data represents state of that object.

For example, here there are two objects of Employee class, have their state separately.


Employee jhon = ("Jhon", "Male");

Employee donna = ("Donna", "Female");


So, the values such as 'Jhon' & 'Male' assigned to the properties (empName, gender) of Employee class make up object's state.


Algorithms, are nothing but methods or functions, used to define the behavior of an object. In Java the ability of an object to respond to the messages is implemented through methods. So, in an object oriented programming, objects are interacting with each other, by sending the messages.

Instrument.java

StringedInstrument.java

package abstractclass.music.instrument; public abstract class StringedInstrument extends Instrument { private int numberOfStrings; private String stringMade; public StringedInstrument (String instrumentName, String instrumentBrand, int numberOfStrings, String stringMade) { super (instrumentName, instrumentBrand); this.numberOfStrings= numberOfStrings; this.stringMade = stringMade; } public int getNumberOfStrings() { return numberOfStrings; } public void setNumberOfStrings(int numberOfStrings) { this.numberOfStrings = numberOfStrings; } public String getStringMade() { return stringMade; } public void setStringMade(String stringMade) { this.stringMade = stringMade; } @Override public String instrumentBasicDetails() { return "An Instrument Name: " + getInstrumentName() + " Brand: "+ getInstrumentBrand() + " " + "No of Strings: " + getNumberOfStrings()+ " stringMade: "+ getStringMade() +"\n"; } }

AcousticGuitars.java

package abstractclass.music.instrument; public class AcousticGuitars extends StringedInstrument { private String bodyType; private String bodyMaterial; private String bodyColor; private double price; public String getBodyType() { return bodyType; } public void setBodyType(String bodyType) { this.bodyType = bodyType; } public String getBodyMaterial() { return bodyMaterial; } public void setBodyMaterial(String bodyMaterial) { this.bodyMaterial = bodyMaterial; } public String getBodyColor() { return bodyColor; } public void setBodyColor(String bodyColor) { this.bodyColor = bodyColor; } public double getPrice() { return price; } public void setPrice(double price) { this.price = price; } public AcousticGuitars(String instrumentName, String instrumentBrand, int numberOfStrings, String stringMade, String bodyType, String bodyMaterial,String bodyColor, double price ) { super(instrumentName, instrumentBrand, numberOfStrings, stringMade); this.bodyType = bodyType; this.bodyMaterial =bodyMaterial; this.bodyColor = bodyColor; this.price = price; } @Override public String instrumentBasicDetails() { String instrumentBasicDetails = super.instrumentBasicDetails(); return instrumentBasicDetails + " " + "An Instrument Body Type: " + getBodyType() + " Body Material: "+ getBodyMaterial() +" "+ "Body Color: " + getBodyColor()+ " Price: "+ getPrice() +"\n" ; } }

BassGuitars.java

package abstractclass.music.instrument; public class BassGuitars extends StringedInstrument { private String bodyType; private String bodyMaterial; private String bodyColor; private double price; public String getBodyType() { return bodyType; } public void setBodyType(String bodyType) { this.bodyType = bodyType; } public String getBodyColor() { return bodyColor; } public void setBodyColor(String bodyColor) { this.bodyColor = bodyColor; } public double getPrice() { return price; } public void setPrice(double price) { this.price = price; } public String getBodyMaterial() { return bodyMaterial; } public void setBodyMaterial(String bodyMaterial) { this.bodyMaterial = bodyMaterial; } public BassGuitars(String instrumentName, String instrumentBrand, int numberOfStrings, String stringMade, String bodyType, String bodyMaterial,String bodyColor, double price ) { super(instrumentName, instrumentBrand, numberOfStrings, stringMade); this.bodyType = bodyType; this.bodyMaterial =bodyMaterial; this.bodyColor = bodyColor; this.price = price; } @Override public String instrumentBasicDetails() { String instrumentBasicDetails = super.instrumentBasicDetails(); return instrumentBasicDetails + " " + "An Instrument Body Type: " + getBodyType() + " Body Material: "+ getBodyMaterial() +" "+ "Body Color: " + getBodyColor()+ " Price: "+ getPrice() +"\n" ; } }

ElectricGuitars.java

package abstractclass.music.instrument; public class ElectricGuitars extends StringedInstrument { private String bodyType; private String bodyMaterial; private String bodyColor; private double price; public String getBodyType() { return bodyType; } public void setBodyType(String bodyType) { this.bodyType = bodyType; } public String getBodyColor() { return bodyColor; } public void setBodyColor(String bodyColor) { this.bodyColor = bodyColor; } public double getPrice() { return price; } public void setPrice(double price) { this.price = price; } public String getBodyMaterial() { return bodyMaterial; } public void setBodyMaterial(String bodyMaterial) { this.bodyMaterial = bodyMaterial; } public ElectricGuitars(String instrumentName, String instrumentBrand, int numberOfStrings, String stringMade, String bodyType, String bodyMaterial,String bodyColor, double price ) { super(instrumentName, instrumentBrand, numberOfStrings, stringMade); this.bodyType = bodyType; this.bodyMaterial =bodyMaterial; this.bodyColor = bodyColor; this.price = price; } @Override public String instrumentBasicDetails() { String instrumentBasicDetails = super.instrumentBasicDetails(); return instrumentBasicDetails + "An Instrument Body Type: " + getBodyType() + " Body Material: " + getBodyMaterial() +" "+ "Body Color: " + getBodyColor()+ " Price: "+ getPrice() +"\n" ; } }

Execution.java

package abstractclasses.execution; import abstractclass.music.instrument.AcousticGuitars; import abstractclass.music.instrument.BassGuitars; import abstractclass.music.instrument.ElectricGuitars; import abstractclass.music.instrument.Instrument; public class Execution { public static void main(String[] args) { Instrument guitar; String guitarDescription; guitar = new ElectricGuitars("Electric Guitar", "Fender",6, "Nickel Plated Electric String", "Hollow", "Solid Wood", "Blue", 985.0 ); guitarDescription = guitar.instrumentBasicDetails(); System.out.println(guitarDescription); guitar = new BassGuitars("Bass Guitar", "Warwick", 5, "Pure Nickel Electric String", "Solid", "Acrylic", "Brown", 565.0 ); guitarDescription = guitar.instrumentBasicDetails(); System.out.println(guitarDescription); guitar = new AcousticGuitars("Acoustic Guitar", "Jackson",6, "Stainless Steel Electric String", "Baritone", "Laminate", "white", 345.0 ); guitarDescription = guitar.instrumentBasicDetails(); System.out.println(guitarDescription); } }


Comentarios


Featured Posts
Check back soon
Once posts are published, you’ll see them here.
Follow Us
  • Facebook Basic Square
  • Twitter Basic Square
  • Google+ Basic Square
Recent Posts
Archive
Search By Tags
  • Grey Facebook Icon
  • Grey Twitter Icon
  • Grey Instagram Icon

© 2016 by JavaStrokes. Proudly created with Wix.com

Success! Message received.

© Copyright 2016 by JavaStrokes
bottom of page