Single Inheritance
package J1;
//Parent class (Vehicle)
class Vehicle {
// Variable in Vehicle class
int speed = 60;
// Method to display speed
void showSpeed() {
System.out.println("Vehicle speed is: " + speed + " km/h");
}
}
//Child class (Car) inherits from Vehicle
class Car extends Vehicle {
// Additional variable in the Car class
String model = "Sedan";
// Method to display car details
void displayCarInfo() {
System.out.println("Car model: " + model);
}
}
public class Inherit {
public static void main(String[] args) {
// Create an object of the Car class
Car car = new Car();
// Access the method and variable from the Vehicle (Parent) class
car.showSpeed(); // Inherited from Vehicle class
// Access the method and variable from the Car (Child) class
car.displayCarInfo(); // Defined in Car class
}
}
Demonstration of super keyword:
//Parent class (Animal)
class Animal {
// Variable in Animal class
String name;
// Constructor to initialize the variable
public Animal(String name) {
this.name = name; // Assigning the name through constructor
}
// Method in Animal class to display the name
void displayAnimalName() {
System.out.println("Animal Name: " + name);
}
}
//Child class (Dog) that inherits from Animal
class Dog extends Animal {
// Constructor of Dog class, calling the parent class constructor
public Dog(String name) {
super(name); // Using the super keyword to call Animal's constructor
}
// Method in Dog class to use the inherited variable
void bark() {
System.out.println(name + " is barking!"); // Accessing inherited variable
}
}
public class inherit2 {
public static void main(String[] args) {
// Create a Dog object, initializing the 'name' via the constructor
Dog dog = new Dog("Buddy");
// Call the methods using the variable from parent class
dog.displayAnimalName(); // Inherited method
dog.bark(); // Child class method
}
}
Hierarchial Inheritance
//Parent class (Vehicle)
class Vehicle1 {
// Variable in Vehicle class
int speed;
// Constructor to initialize the speed
public Vehicle1(int speed) {
this.speed = speed;
}
// Method to display speed
void showSpeed() {
System.out.println("Vehicle speed is: " + speed + " km/h");
}
}
//Child class (Car) inheriting from Vehicle
class Car1 extends Vehicle1 {
// Constructor for Car class
public Car1(int speed) {
super(speed); // Call parent class constructor
}
// Car-specific method
void displayModel() {
System.out.println("This is a car.");
}
}
//Another child class (Bike) inheriting from Vehicle
class Bike extends Vehicle1{
// Constructor for Bike class
public Bike(int speed) {
super(speed); // Call parent class constructor
}
// Bike-specific method
void displayType() {
System.out.println("This is a bike.");
}
}
public class inherit3 {
public static void main(String[] args) {
// Create objects of Car and Bike classes
Car1 car = new Car1(80);
Bike bike = new Bike(40);
// Access methods from parent and child classes
car.showSpeed(); // Inherited from Vehicle class
car.displayModel(); // Defined in Car class
bike.showSpeed(); // Inherited from Vehicle class
bike.displayType(); // Defined in Bike class
}
}
Upcasting:
package J1;
//Parent class (Animal)
class Animal1 {
// Method to be overridden
void makeSound() {
System.out.println("The animal makes a sound.");
}
}
//Child class (Dog) that overrides the makeSound() method
class Dog1 extends Animal1 {
// Overriding the makeSound() method
//@Override
void makeSound() {
System.out.println("The dog barks.");
}
}
public class Override {
public static void main(String[] args) {
// Create an object of the Animal class
Animal1 myAnimal = new Animal1();
myAnimal.makeSound(); // Calls the makeSound() method in Animal class
// Create an object of the Dog class
Dog1 myDog = new Dog1();
myDog.makeSound(); // Calls the overridden makeSound() method in Dog class
// Polymorphism: Parent class reference holding a child class object
Animal1 anotherDog = new Dog1();
anotherDog.makeSound(); // Calls the overridden method in Dog class
}
}
1. Calling Superclass Methods
The super keyword is commonly used to call a method in the superclass that has been
overridden in the subclass.
class Parent {
void display() {
System.out.println("Parent class method");
}
}
class Child extends Parent {
void display() {
super.display(); // Calls the Parent class method
System.out.println("Child class method");
}
}
public class Test {
public static void main(String[] args) {
Child obj = new Child();
obj.display();
}
}
Output:
Parent class method
Child class method
2. Calling Superclass Constructors
The super keyword can also be used to invoke a constructor of the superclass, allowing for the
reuse of initialization code from the parent class.
class Parent {
Parent() {
System.out.println("Parent class constructor");
}
}
class Child extends Parent {
Child() {
super(); // Calls the Parent class constructor
System.out.println("Child class constructor");
}
}
public class Test {
public static void main(String[] args) {
Child obj = new Child();
}
}
Output:
Parent class constructor
Child class constructor
3). Accessing Superclass Variables
If a subclass has a field with the same name as one in the superclass, super can be used to
differentiate between them.
class Parent {
int value = 10;
}
class Child extends Parent {
int value = 20;
void display() {
System.out.println("Child class value: " + value);
System.out.println("Parent class value: " + super.value); // Accesses Parent class variable
}
}
public class Test {
public static void main(String[] args) {
Child obj = new Child();
obj.display();
}
}
Output:
Child class value: 20
Parent class value: 10
4). Using super(Parameter) in Java
In Java, you can pass parameters to the parent class’s constructor using the super() keyword.
This is useful when the parent class has a constructor that requires arguments.
class Parent {
int age;
// Constructor with a parameter
Parent(int age) {
this.age = age;
System.out.println("Parent class age: " + age);
}
}
class Child extends Parent {
// Constructor of Child, calling Parent constructor
Child(int age) {
super(age); // Passing 'age' to the Parent constructor
System.out.println("Child class constructor");
}
}
public class Test {
public static void main(String[] args) {
Child obj = new Child(50); // This will pass '50' to the Parent class constructor
}
}
Output:
Parent class age: 50
Child class constructor
Protected Access Specifier
In Java, the protected access specifier (or access modifier) is one of the four visibility
modifiers, along with private, public, and default (no modifier). It determines the scope
within which a class member (like a variable, method, or constructor) can be accessed.
Key Characteristics of the protected Access Specifier
Same Package Access:
Members marked with protected can be accessed by any class within the same package as the
class in which they are declared. This is similar to the default (package-private) access modifier.
Access by Subclasses (Even in Different Packages):
The main distinction between protected and default access is that protected members can also
be accessed by subclasses, even if they are in a different package.
This allows subclasses to inherit and use these members, promoting reusability and extending
class functionality while still keeping those members somewhat restricted.
Cannot Be Accessed by Non-Subclass Classes in Different Packages:
A class in another package cannot access the protected member unless it is through inheritance
(i.e., the class is a subclass of the class with the protected member).
1. Within the Same Package
package mypackage;
class Parent {
protected String name = "ParentName";
protected void display() {
System.out.println("Protected method in Parent class.");
}
}
public class Test {
public static void main(String[] args) {
Parent obj = new Parent();
System.out.println(obj.name); // Accessing protected variable
obj.display(); // Accessing protected method
}
}
Output:
ParentName
Protected method in Parent class.
2. In a Subclass (Different Package)
// File: Parent.java (in package mypackage)
package mypackage;
public class Parent {
protected String name = "ParentName";
protected void display() {
System.out.println("Protected method in Parent class.");
}
}
// File: Child.java (in package otherpackage)
package otherpackage;
import mypackage.Parent;
public class Child extends Parent {
public void accessParent() {
System.out.println(name); // Accessing protected variable from Parent
display(); // Accessing protected method from Parent
}
}
// File: Test.java (in package otherpackage)
package otherpackage;
public class Test {
public static void main(String[] args) {
Child obj = new Child();
obj.accessParent(); // Accessing Parent class members via subclass
}
}
Output:
ParentName
Protected method in Parent class.
3. Trying to Access protected from a Non-Subclass in a Different Package
// File: Test.java (in package otherpackage)
package otherpackage;
import mypackage.Parent;
public class Test {
public static void main(String[] args) {
Parent obj = new Parent();
// The following lines will cause compilation errors:
// System.out.println(obj.name); // ERROR: 'name' has protected access
// obj.display(); // ERROR: 'display' has protected access
}
}
Output:
name has protected access in mypackage.
Parent display() has protected access in mypackage.Parent