You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
// Basic Class StructurepublicclassCar {
// Instance variables (attributes)privateStringbrand; // Private for encapsulationprivateintyear;
publicbooleanisRunning; // Public attribute// Constructor - creates new Car objectspublicCar(Stringbrand, intyear) {
this.brand = brand; // 'this' refers to current instancethis.year = year;
this.isRunning = false;
}
// Method to start the carpublicvoidstartEngine() {
isRunning = true;
System.out.println("Engine started!");
}
// Getter method - allows safe access to private datapublicStringgetBrand() {
returnbrand;
}
// Setter method - allows controlled modificationpublicvoidsetYear(intyear) {
if (year > 1900) { // Data validationthis.year = year;
}
}
}
// Creating and Using ObjectsCarmyCar = newCar("Toyota", 2020); // Create new CarmyCar.startEngine(); // Call methodStringbrand = myCar.getBrand(); // Use getter
Inheritance
// Parent (Base) ClasspublicclassAnimal {
protectedStringname; // 'protected' allows access in child classes// ConstructorpublicAnimal(Stringname) {
this.name = name;
}
// Method that can be inheritedpublicvoidmakeSound() {
System.out.println("Some sound");
}
}
// Child (Derived) ClasspublicclassDogextendsAnimal { // 'extends' indicates inheritanceprivateStringbreed; // Additional attribute// Child constructorpublicDog(Stringname, Stringbreed) {
super(name); // Call parent constructorthis.breed = breed;
}
// Override parent's method@Override// Good practice to use @OverridepublicvoidmakeSound() {
System.out.println("Woof!");
}
// Additional method specific to Dogpublicvoidfetch() {
System.out.println(name + " is fetching!");
}
}
// Using InheritanceDogmyDog = newDog("Rex", "Labrador");
myDog.makeSound(); // Uses overridden methodmyDog.fetch(); // Uses Dog-specific method
Interfaces
// Interface DefinitionpublicinterfaceFlyable {
// Abstract methods (no implementation)voidfly(); // Implicitly public and abstractvoidland();
// Default method (Java 8+)defaultvoidglide() { // Provides default implementationSystem.out.println("Gliding in air");
}
// Static method in interfacestaticintgetWingCount() {
return2;
}
}
// Class implementing interfacepublicclassBirdimplementsFlyable {
// Must implement all abstract methods@Overridepublicvoidfly() {
System.out.println("Bird is flying");
}
@Overridepublicvoidland() {
System.out.println("Bird has landed");
}
// Can use default method as-is or override it
}
// Multiple Interface ImplementationpublicclassAirplaneimplementsFlyable, Maintainable {
// Must implement all methods from both interfaces
}
Abstract Classes
// Abstract Class DefinitionpublicabstractclassShape {
protectedStringcolor; // Common attribute// Constructor in abstract classpublicShape(Stringcolor) {
this.color = color;
}
// Abstract method (must be implemented by children)publicabstractdoublecalculateArea();
// Concrete method (shared implementation)publicvoiddisplayColor() {
System.out.println("Color is " + color);
}
}
// Concrete Class extending Abstract ClasspublicclassCircleextendsShape {
privatedoubleradius;
publicCircle(Stringcolor, doubleradius) {
super(color); // Call abstract class constructorthis.radius = radius;
}
// Must implement abstract method@OverridepublicdoublecalculateArea() {
returnMath.PI * radius * radius;
}
}
Polymorphism
// Polymorphic Class StructurepublicclassVehicle {
publicvoidmove() {
System.out.println("Vehicle is moving");
}
}
publicclassCarextendsVehicle {
@Overridepublicvoidmove() {
System.out.println("Car is driving");
}
}
publicclassBoatextendsVehicle {
@Overridepublicvoidmove() {
System.out.println("Boat is sailing");
}
}
// Polymorphic Method UsagepublicclassTransportation {
// Polymorphic method - accepts any Vehicle typepublicvoidstartJourney(Vehiclevehicle) {
vehicle.move(); // Calls appropriate version
}
publicstaticvoidmain(String[] args) {
Transportationt = newTransportation();
// Same method, different behaviorst.startJourney(newCar()); // "Car is driving"t.startJourney(newBoat()); // "Boat is sailing"
}
}