0% found this document useful (0 votes)
26 views

2

Uploaded by

Manoj Kumar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views

2

Uploaded by

Manoj Kumar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

class Method_Overloading {

double figure(double l, int b) //two parameters with double type


{
return (l*b);
}
float figure(int s) //one parameter with float return type
{
return (s*s);
}

public static void main(String[] args) {


Method_Overloading obj = new Method_Overloading();
System.out.println("Area of Rectangle: " +obj.figure(5.55, 6));
System.out.println("Area of Square: " +obj.figure(3));
}
}

//To access the inner class, create an object of the outer class, and then
//create an object of the inner class:
class OuterClass {
int x = 10;

class InnerClass {
int y = 5;
}
}

public class Main {


public static void main(String[] args) {
OuterClass myOuter = new OuterClass();
OuterClass.InnerClass myInner = myOuter.new InnerClass();
System.out.println(myInner.y + myOuter.x);
}
}
//output = 15

Private Inner Class

Unlike a "regular" class, an inner class can be private or protected. If you don't
want outside objects to access the inner class, declare the class as private:

class OuterClass {
int x = 10;

private class InnerClass {


int y = 5;
}
}

public class Main {


public static void main(String[] args) {
OuterClass myOuter = new OuterClass();
OuterClass.InnerClass myInner = myOuter.new InnerClass();
System.out.println(myInner.y + myOuter.x);
}
}

Static Inner Class

An inner class can also be static, which means that you can access it without
creating an object of the outer class:
class OuterClass {
int x = 10;

static class InnerClass {


int y = 5;
}
}

public class Main {


public static void main(String[] args) {
OuterClass.InnerClass myInner = new OuterClass.InnerClass();
System.out.println(myInner.y);
}
}

// Outputs 5

Access Outer Class From Inner Class

One advantage of inner classes, is that they can access attributes and methods of
the outer class:

class OuterClass {
int x = 10;

class InnerClass {
public int myInnerMethod() {
return x;
}
}
}

public class Main {


public static void main(String[] args) {
OuterClass myOuter = new OuterClass();
OuterClass.InnerClass myInner = myOuter.new InnerClass();
System.out.println(myInner.myInnerMethod());
}
}
// Outputs 10

Java Inheritance (Subclass and Superclass)

In Java, it is possible to inherit attributes and methods from one class to another.
We group the "inheritance concept" into two categories:

subclass (child) - the class that inherits from another class


superclass (parent) - the class being inherited from

To inherit from a class, use the extends keyword.

In the example below, the Car class (subclass) inherits the attributes and methods
from the Vehicle class (superclass):

class Vehicle {
protected String brand = "Ford"; // Vehicle attribute
public void honk() { // Vehicle method
System.out.println("Tuut, tuut!");
}
}

class Car extends Vehicle {


private String modelName = "Mustang"; // Car attribute
public static void main(String[] args) {

// Create a myCar object


Car myCar = new Car();

// Call the honk() method (from the Vehicle class) on the myCar object
myCar.honk();

// Display the value of the brand attribute (from the Vehicle class) and the
value of the modelName from the Car class
System.out.println(myCar.brand + " " + myCar.modelName);
}
}

You might also like