Quick introduction to Object oriented concepts in Java - Part 2 of series
Chapters
Inheritance
This tutorial is a follow up from the part 1, where we introduced Java Language.
Inheritance
Inheritance is the major block in object oriented programming language has it helps in hierarchical classification. Inheritance is a process wherein a new object or class is derived from an existing objects or class. The class derived from another class is called as subclass whereas class from which a subclass derived is called as a superclass. Therefore A subclass is special version of superclass which as methods and variables of it along with added its own unique elements. A subclass has only one superclass whereas a superclass will have one or more subclasses
In the above figure CAR class is a Super Class which is extended to Sub Class like Compact Car, Mid-Size Car and Large Car.
We can inherit a definition of class into another class with using extend keyword. Below is syntax for it.
class subclass-name extends superclass-name { // // A class body }
Below is the simple code which explains inheritance. The Program has creates a SuperClass called X and a SubClass called Y. We are using extends keyword to a create subclass of X.
// Create superclass. class X { int a, b; void showab() { System.out.println("a and b: " + a + " " + b); } } // Create subclass by extending class X. class Y extends X { int c; void showc() { System.out.println("c: " + c); } void sum() { System.out.println("a+b+c: " + (a + b + c)); } } class SimpleExampleInheritance { public static void main(String args[]) { XsuperClassObj = new X(); YsubClassObj = new Y(); superClassObj.a = 5; superClassObj.b = 10; System.out.println("ContentsuperClassOb: "); superClassObj.showab(); System.out.println(); /* public members of superclass can be accessed by subclass */ subClassObj.a = 1; subClassObj.b = 2; subClassObj.c = 3; System.out.println("Contents subClassOb: "); subClassObj.showab(); subClassObj.showc(); System.out.println(); System.out.println("Sum of a, b and c in subClassObj:"); subClassObj.sum(); } }
The output:
Contents of superClassOb:
a and b: 510
Contents of subClassOb:
a and b: 1 2
c: 3
Sum of a, b and c in subClassOb:
a+b+c: 6
In the above example the subclass Y has all the member of its SuperClass X. So the subClassObj can access a and b and also method showab(). Even though X is a superclass for Y it is completely independent and stand-alone class.
Things not possible with java class Inheritance?
- Private members of superclass are not inherited by subclass and can be accessed indirectly.
- Members having default accessibility in superclass are not inherited by subclasses in other packages as these members only are accessible by names in subclasses within same package as superclass.
- Since initialize and constructors blocks are not members of class they are not inherited by subclass.
- A subclass can extend only a one superclass.
Description
This tutorial will introduce you to Object oriented concepts in Java. Basic definitions and learning with code examples.
This tutorial has subdivided into 7 parts as listed below
- Understanding Inheritance
- Understanding Overrides
- Understanding Polymorphism
- Understanding Abstraction
- Understanding Encapsulation
- Understanding Interfaces
- Understanding Packages
Let us know if you found any error in this tutorial. Your feedback is welcome as always.
Audience
Students seeking a quick overview on Java Programming language and its Object oriented features.
Learning Objectives
Learn Object Oriented concepts in Java language.
Author: Subject Coach
Added on: 10th Mar 2015
You must be logged in as Student to ask a Question.
None just yet!