class ParentClass {
// Parent class code
}
class ChildClass extends ParentClass {
// Child class code
}
// Parent class
class BankAccount
{
void accountType()
{
System.out.println("This is a general bank account.");
}
}
// Child class
class SavingsAccount extends BankAccount
{
void interestRate()
{
System.out.println("Savings account offers 4% interest.");
}
}
// Main class
public class MainApp
{
public static void main(String[] args)
{
SavingsAccount sa = new SavingsAccount();
sa.accountType(); // Inherited method
sa.interestRate(); // Specific method
}
}
This is a general bank account. Savings account offers 4% interest.
class Grandparent
{
// code
}
class Parent extends Grandparent
{
// code
}
class Child extends Parent
{
// code
}
// Grandparent class
class Animal
{
void eat()
{
System.out.println("Animal is eating.");
}
}
// Parent class
class Dog extends Animal
{
void bark()
{
System.out.println("Dog is barking.");
}
}
// Child class
class Puppy extends Dog
{
void weep()
{
System.out.println("Puppy is weeping.");
}
}
public class MainApp
{
public static void main(String[] args)
{
Puppy p = new Puppy();
p.eat(); // From Animal
p.bark(); // From Dog
p.weep(); // Own method
}
}
Animal is eating. Dog is barking. Puppy is weeping.
class Parent
{
// Parent class code
}
class Child1 extends Parent
{
// Child1 specific code
}
class Child2 extends Parent
{
// Child2 specific code
}
// Parent class
class Vehicle
{
void fuelType()
{
System.out.println("Uses fuel.");
}
}
// First child class
class Car extends Vehicle
{
void wheels()
{
System.out.println("Car has 4 wheels.");
}
}
// Second child class
class Bike extends Vehicle
{
void handleType()
{
System.out.println("Bike has a handlebar.");
}
}
public class MainApp
{
public static void main(String[] args)
{
Car c = new Car();
c.fuelType(); // Inherited from Vehicle
c.wheels(); // Car specific
Bike b = new Bike();
b.fuelType(); // Inherited from Vehicle
b.handleType(); // Bike specific
}
}
Uses fuel. Car has 4 wheels. Uses fuel. Bike has a handlebar.
interface A
{
void methodA();
}
interface B
{
void methodB();
}
class C implements A, B
{
public void methodA()
{
System.out.println("Method from interface A");
}
public void methodB()
{
System.out.println("Method from interface B");
}
}
// Interface A
interface A
{
void displayA();
}
// Interface B
interface B
{
void displayB();
}
// Class C implementing both interfaces
class C implements A, B
{
public void displayA()
{
System.out.println("Display from interface A");
}
public void displayB()
{
System.out.println("Display from interface B");
}
}
public class MainApp
{
public static void main(String[] args)
{
C obj = new C();
obj.displayA();
obj.displayB();
}
}
Display from interface A Display from interface B
interface A
{
void methodA();
}
interface B
{
void methodB();
}
class C
{
void methodC()
{
System.out.println("Method from class C");
}
}
// Class D inherits class C and implements A and B
class D extends C implements A, B
{
public void methodA()
{
System.out.println("Method from interface A");
}
public void methodB()
{
System.out.println("Method from interface B");
}
}
// Interface A
interface A
{
void showA();
}
// Interface B
interface B
{
void showB();
}
// Class C
class C
{
void showC()
{
System.out.println("Show from class C");
}
}
// Class D: extends C and implements A and B
class D extends C implements A, B
{
public void showA()
{
System.out.println("Show from interface A");
}
public void showB()
{
System.out.println("Show from interface B");
}
}
// Main class
public class MainApp
{
public static void main(String[] args)
{
D obj = new D();
obj.showA();
obj.showB();
obj.showC();
}
}
Show from interface A Show from interface B Show from class C
Your feedback helps us grow! If there's anything we can fix or improve, please let us know.
We’re here to make our tutorials better based on your thoughts and suggestions.