extends.class derived-class extends base-class {
//methods and fields
}
class A {
int x, y;
void getdata(int a, int b) {
x = a;
y = b;
}
int add() {
System.out.println(“Superclass Method”);
return (x + y);
}
}
class B extends A {
int mult() {
System.out.println(“Sub - class Method”);
return (x * y);
}
}
class Test {
public static void main(String args[]) {
B obj = new B();
int add, mult;
obj.getdata(5, 3);
add = obj.add();
mult = obj.mult();
System.out.println(“Addition” + add);
System.out.println(“Multiplication” + mult);
}
}
class one {
public void print() {
System.out.println(“hi ");
}
}
class two extends one {
public void print1() {
System.out.println(“hello ");
}
}
class three extends two {
public void print2() {
System.out.println(“hi ");
}
}
// Drived class
public class Main {
public static void main(String[] args) {
three g = new three();
g.print();
g.print1();
g.print2();
}
}
class One {
int x = 10, y = 20;
void disp() {
System.out.println(“Method of class One”);
System.out.println(“Value of X” + x);
System.out.println(“Value of Y” + y);
}
}
class Two extends One {
void add() {
System.out.println(“Method of class Two”);
System.out.println(“Addition: ”+(x + y));
}
}
class Three extends One {
void mul() {
System.out.println(“Method of class Three”);
System.out.println(“Multiplication: ”+(x * y));
}
}
class Test {
public static void main(String args[]) {
Two obj1 = new Two();
Three obj2 = new Three();
obj1.disp();
obj1.add();
obj2.mul();
}
}
class Student {
int m1, m2;
void getmarks(int x, int y) {
m1 = x;
m2 = y;
}
void putmarks() {
System.out.println(“First” + m1);
System.out.println(“Second” + m2);
}
}
interface sport {
int sp = 6;
void spmarks();
}
class Result extends Student implements
Sport {
int total;
public void spmarks() {
System.out.println(“Sport Mark” + sp);
}
void disp() {
putmarks();
spmarks();
total = m1 + m2 + sp;
System.out.println(“Total” + total);
}
}
class Test {
public static void main(String args[]) {
Result obj = new Result();
obj.getmarks(80, 60);
obj.disp();
}
}
class C {
public void display() {
System.out.println("C");
}
}
class A extends C {
public void display() {
System.out.println("A");
}
}
class B extends C {
public void display() {
System.out.println("B");
}
}
class D extends A {
public void display() {
System.out.println("D");
}
}
class Main {
public static void main(String args[]) {
D obj = new D();
obj.display();
}
}// Base class Person
class Person {
void message() {
System.out.println("This is person class");
}
}
class Student extends Person
// Subclass Student
{
void message() {
System.out.println("This is student class");
}
void display() // Note that display() is
only in Student class {
message(); // will invoke or call current
class message() method
super.message(); // will invoke or call
parent class message() method
}
}
class Test /* Driver program to test */ {
public static void main(String args[]) {
Student s = new Student();
s.display(); // calling display() of
Student
}
}class Person {
// Superclass Person
Person() {
System.out.println("Person class Constructor");
}
}
class Student extends Person {
// Subclass Student extending Person
Student() {
super();
System.out.println("Student class Constructor");
}
}
class Test {
// Driver class to test
public static void main(String[] args) {
Student s = new Student();
}
}public class Main {
// Method with 2 int parameters
static int add(int a, int b) {
return a + b;
}
// Method with 3 int parameters
static int add(int a, int b, int c) {
return a + b + c;
}
// Method with 2 double parameters
static double add(double a, double b) {
return a + b;
}
public static void main(String[] args) {
System.out.println(add(10, 20));
System.out.println(add(10, 20, 30));
System.out.println(add(10.5, 20.5));
}
}// OUTPUT
30
60
31.0class Animal {
void sound() {
System.out.println("Animal makes a sound");
}
}
class Dog extends Animal {
@Override
void sound() {
System.out.println("Dog barks");
}
}
public class Main {
public static void main(String[] args) {
Dog d = new Dog();
d.sound();
}
}// OUTPUT
Dog barks| Method Overloading | Method Overriding |
|---|---|
| Same method name with different parameters. | Child class provides a new implementation of a parent class method. |
| Usually occurs within the same class. | Occurs between parent and child classes. |
| Inheritance is not required. | Inheritance is required. |
| Parameters must be different. | Parameters must be same. |
| Return type can be different, but cannot be the only difference. | Return type must be same or covariant. |
| It is compile-time polymorphism. | It is runtime polymorphism. |
| Method binding happens at compile time. | Method binding happens at runtime. |
static, final, and private methods can be overloaded. | final and private methods cannot be overridden. |
Example: add(int, int) and add(int, int, int) | Example: Animal.sound() and Dog.sound() |
class Animal {
void sound() {
System.out.println("Animal makes sound");
}
}
class Dog extends Animal {
void sound() {
System.out.println("Dog barks");
}
}
public class Main {
public static void main(String[] args) {
Animal a = new Dog();
a.sound();
}
}// OUTPUT
Dog barksfinal Keywordfinal keyword is used to restrict changes in Javafinal variable cannot be changed after it is assigned a value.class Main {
public static void main(String[] args) {
final int x = 10;
System.out.println(x);
// x = 20; // Error
}
}final method cannot be overridden by a child class.class Animal {
final void sound() {
System.out.println("Animal makes sound");
}
}
class Dog extends Animal {
// Error: cannot override final method
// void sound() {
// System.out.println("Dog barks");
// }
}final class cannot be inherited.final class Animal {
void sound() {
System.out.println("Animal makes sound");
}
}
// Error: cannot inherit from final class
// class Dog extends Animal {
// }abstract class Animal { //abstract method
public abstract void sound();
}
public class Dog extends Animal //Dog class
extends Animal class {
public void sound() {
System.out.println("Woof");
}
public static void main(String args[]) {
Dog obj = new Dog();
obj.sound();
}
}
Why can’t we create the object of an abstract class?
Animal.staticMethod();Difference between Abstract Class and Interface
| Abstract Class | Interface |
|---|---|
| Abstract class can have abstract and non-abstract methods. | Interface can have only abstract methods. |
| Abstract class doesn't support multiple inheritance. | Interface supports multiple inheritance. |
| Abstract class can have final, non-final, static and non-static variables. | Interface has only static and final variables. |
| Abstract class can provide the implementation of interface. | Interface can't provide the implementation of abstract class. |
| The abstract keyword is used to declare abstract class. | The interface keyword is used to declare interface. |
| An abstract class can be extended using keyword "extends". | An interface can be implemented using keyword "implements". |
| A Java abstract class can have class members like private, protected, etc. | Members of a Java interface are public by default. |
When to use Abstract Class and Abstract Method?
Made By SOU Student for SOU Students