0% found this document useful (0 votes)
22 views12 pages

Inheritence Example

The document discusses inheritance in Java through multiple examples: 1. It shows a Vehicle class being extended by a Car class, inheriting attributes like color and speed, and adding its own attributes like CC and gears. 2. It demonstrates making attributes private in the parent Vehicle class and accessing them through getter/setter methods in the child Car class. 3. It provides an example of constructors and inheritance, with a Shape class extended by Rectangle and ColoredRectangle classes, showing how to call parent constructors. 4. It shows method overriding, with a Shape class's showAttributes() method overridden in Rectangle to also display the type attribute. So in summary, the document covers key

Uploaded by

shaheena.atas
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views12 pages

Inheritence Example

The document discusses inheritance in Java through multiple examples: 1. It shows a Vehicle class being extended by a Car class, inheriting attributes like color and speed, and adding its own attributes like CC and gears. 2. It demonstrates making attributes private in the parent Vehicle class and accessing them through getter/setter methods in the child Car class. 3. It provides an example of constructors and inheritance, with a Shape class extended by Rectangle and ColoredRectangle classes, showing how to call parent constructors. 4. It shows method overriding, with a Shape class's showAttributes() method overridden in Rectangle to also display the type attribute. So in summary, the document covers key

Uploaded by

shaheena.atas
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 12

Inheritence Example:

1.

public class ChildClass extends BaseClass {

// derived class methods extend and possibly override

Here is the complete example:

// A class to display the attributes of the vehicle

class Vehicle {

String color;

int speed;

int size;

void attributes() {

System.out.println("Color : " + color);

System.out.println("Speed : " + speed);

System.out.println("Size : " + size);

// A subclass which extends for vehicle

class Car extends Vehicle {

int CC;

int gears;

void attributescar() {

// The subclass refers to the members of the superclass

System.out.println("Color of Car : " + color);

System.out.println("Speed of Car : " + speed);


System.out.println("Size of Car : " + size);

System.out.println("CC of Car : " + CC);

System.out.println("No of gears of Car : " + gears);

public class Test {

public static void main(String args[]) {

Car b1 = new Car();

b1.color = "Blue";

b1.speed = 200 ;

b1.size = 22;

b1.CC = 1000;

b1.gears = 5;

b1.attributescar();

The output is

Color of Car : Blue

Speed of Car : 200

Size of Car : 22

CC of Car : 1000

No of gears of Car : 5
--------------------------------------------------------------

// A class to display the attributes of the vehicle

class Vehicle {

String color;
private int speed;

private int size;

public int getSize() {

return size;

public int getSpeed() {

return speed;

public void setSize(int i) {

size = i;

public void setSpeed(int i) {

speed = i;

// A subclass which extends for vehicle

class Car extends Vehicle {

int CC;

int gears;

int color;

void attributescar() {

// Error due to access violation

// System.out.println("Speed of Car : " + speed);

// Error due to access violation

//System.out.println("Size of Car : " + size);


}

public class Test {

public static void main(String args[]) {

Car b1 = new Car();

// the subclass can inherit 'color' member of the superclass

b1.color = 500;

b1.setSpeed(200) ;

b1.setSize(22);

b1.CC = 1000;

b1.gears = 5;

// The subclass refers to the members of the superclass

System.out.println("Color of Car : " + b1.color);

System.out.println("Speed of Car : " + b1.getSpeed());

System.out.println("Size of Car : " + b1.getSize());

System.out.println("CC of Car : " + b1.CC);

System.out.println("No of gears of Car : " + b1.gears);

The output is:

Color of Car : 500

Speed of Car : 200

Size of Car : 22

CC of Car : 1000

No of gears of Car : 5
Constructors and Inheritance

class Shape {

private int length;

private int breadth;

public int getBreadth() {

return breadth;

public int getLength() {

return length;

public void setBreadth(int i) {

breadth = i;

public void setLength(int i) {

length = i;

// default Constructor

Shape() {

length = 0;

breadth = 0;

System.out.println("Inside default constructor of Shape ");

// Parameterized Constructor

Shape(int len, int bdth) {

length = len;
breadth = bdth;

System.out.println("Inside constructor of Shape ");

System.out.println("length : " + length);

System.out.println("breadth : " + breadth);

// A subclass which extends for shape

class Rectangle extends Shape {

private String type;

// default Constructor

Rectangle() {

super();

type = null;

System.out.println("Inside default constructor of rectangle ");

// Parameterized Constructor

Rectangle(String ty, int len, int bdth) {

super (len, bdth);

System.out.println("Inside constructor of rectangle ");

System.out.println("length : " + len);

System.out.println("breadth : " + bdth);

System.out.println("type : " + type);

public String getType() {

return type;
}

public void setType(String string) {

type = string;

// A subclass which extends for rectangle

class ColoredRectangle extends Rectangle {

private String color;

/* default Constructor*/

ColoredRectangle() {

super();

color = null;

System.out.println("Inside default constructor of coloredRectangle");

// Parameterized Constructor

ColoredRectangle(String c, String ty, int len, int bdth) {

super (ty, len, bdth);

System.out.println("Inside constructor of coloredRectangle ");

System.out.println("length : " + len);

System.out.println("breadth : " + bdth);

System.out.println("type : " + ty);

public String getColor() {

return color;

}
public void setColor(String string) {

color = string;

public class Test {

public static void main(String args[]) {

ColoredRectangle CR = new ColoredRectangle();

ColoredRectangle CR2 = new ColoredRectangle("Red","Big", 5, 2 );

The output is:

Inside default constructor of Shape

Inside default constructor of rectangle

Inside default constructor of coloredRectangle

Inside constructor of Shape

length : 5

breadth : 2

Inside constructor of rectangle

length : 5

breadth : 2
type : null

Inside constructor of coloredRectangle

length : 5

breadth : 2

type : Big

Inheritance and Method Overriding


class Shape {

private int length;

private int breadth;

// default Constructor

Shape() {

length = 0;

breadth = 0;

// Parameterized Constructor

Shape(int len, int bdth) {

length = len;

breadth = bdth;

void showattributes() {

System.out.println("length : " + length);

System.out.println("breadth : " + breadth);

// A subclass which extends for shape

class Rectangle extends Shape {

private String type;

/* default Constructor

*/

Rectangle() {

type = null;
}

// Parameterized Constructor

Rectangle(String ty, int len, int bdth) {

super(len,bdth);

type = ty;

void showattributes() {

// showattributes() of class Shape is called

super.showattributes();

System.out.println("type : " + type);

public class Test {

public static void main(String args[]) {

Rectangle rect = new Rectangle("Blue",5,7);

// showattributes() in rectangle is called

rect.showattributes();

The output is :

length : 5

breadth : 7

type : Blue
package com.company;

class Circle{

public int radius;

Circle(){

System.out.println("I am non param of circle");

Circle(int r){

System.out.println("I am circle parameterized constructor");

this.radius = r;

public double area(){

return Math.PI*this.radius*this.radius;

class Cylinder1 extends Circle{

public int height;

Cylinder1(int r, int h){

super(r);

System.out.println("I am cylinder1 parameterized constructor");

this.height = h;

public double volume(){


return Math.PI*this.radius*this.radius*this.height;

public class cwh_52_ch10ps {

public static void main(String[] args) {

// Problem 1

// Circle objC = new Circle(12);

Cylinder1 obj = new Cylinder1(12, 4);

What will be the output of this program?

class A
{
int i = 10;
}

class B extends A
{
int i = 20;
}

public class MainClass


{
public static void main(String[] args)
{
A a = new B();

System.out.println(a.i);
}
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy